Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ====== CATEGORIES FOR BMI =======
- # === DO NOT CHANGE THIS CODE V ===
- # first value - from value (if None, then should be treated as 0)
- # second value - to value (if None, then should be treted as 100)
- # third value - category label
- bmi_categories_threshold = [
- (None, 15, "Very severely underweight"),
- (15, 16, "Severely underweight"),
- (16, 18.5, "Underweight"),
- (18.5, 25, "Normal (healthy weight)"),
- (25, 30, "Overweight"),
- (30, 35, "Moderately obese"),
- (35, 40, "Severely obese"),
- (40, None, "Very severely obese")
- ]
- # === DO NOT CHANGE THIS CODE ^ ===
- # function for calculating BMI value
- def calculate_bmi(w, h):
- """
- Parameters
- ----------
- w : float
- Weight of the patient (in kg)
- h : str
- Height of the patient (in meters)
- Returns
- -------
- bmi : float
- Calculated value for BMI based on equation: weight / height ^2
- """
- return w / (h ** 2)
- # function for getting label based on bmi value
- def get_bmi_category(bmi_value):
- """
- Parameters
- ----------
- bmi_value : float
- Calculated value for BMI
- Returns
- -------
- output_labels : list
- List with string labels for BMI category for input value
- """
- output_labels = []
- for value_from_, value_to_, label in bmi_categories_threshold:
- if value_from_ is None:
- value_from_ = 0
- else:
- value_from_ = value_from_
- if value_to_ is None:
- value_to_ = 100 # example maximum value
- else:
- value_to_ = value_to_
- if bmi_value >= value_from_ and bmi_value <= value_to_:
- output_labels.append(label)
- return output_labels
- # patients to test
- # first value name, second is weight, third is height
- example_patients = [
- ("Jan Szpan", 70, 1.8),
- ("Jan Kowalski", 80, 1.9),
- ("Jan Janowicz", 150, 1.7),
- ("Arnold Boczek", 120, 2)
- ]
- # function for preparing results for patients
- def get_bmi_category_for_patients(patients):
- """
- Parameters
- ----------
- patients : list
- List of patients in the following format for each list item:
- (string_value_for_name, weight, height)
- Returns
- -------
- results : list
- List with results for each list item:
- (bmi_value, bmi_category)
- """
- results = []
- for patient in patients:
- bmi_value = round(calculate_bmi(patient[1], patient[2]), 2)
- bmi_category = str(get_bmi_category(bmi_value))
- results.append((bmi_value, bmi_category))
- print(">> Patient", patient[0])
- print("\tBMI value:", bmi_value, "\tCategory:", bmi_category)
- return results
- results = get_bmi_category_for_patients(example_patients)
- # ====== TESTING PROCEDURE ========
- # === DO NOT CHANGE THIS CODE V ===
- # expected results
- expected_results = [
- (21.6, "['Normal (healthy weight)']"),
- (22.16, "['Normal (healthy weight)']"),
- (51.9, "['Very severely obese']"),
- (30, "['Overweight', 'Moderately obese']")
- ]
- # comparing expected results and results acquired
- for index, (expected_result, result) in enumerate(zip(expected_results, results)):
- assert result[0] == expected_result[0]
- assert result[1] == expected_result[1]
- # === DO NOT CHANGE THIS CODE ^ ===
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement