Homework 2

Due: October 29, 2025
Points: 100


In the given examples, what you type is in red and the program prints what is in black. The symbol means a carriage return (return, enter).

Your program output should look like the output in the examples, except that what you type won’t be in red.

  1. (50 points) The file “medical_records.csv” contains 1000 medical records, each consisting of the comma-separated data in the following order: For this assignment, you are to read the file, and then ask the user to enter a lower age limit and an upper age limit. Your first prompt should be “Age from . . . ” and your next prompt should be “Age up to and including . . . ”, as in the examples below.

    For all records of people between those ages (inclusive,) print the following statistics:

    If either age is not a positive integer, print the error message “Please enter a positive integer”. If the user enters the ages so that the “from” age is greater than the “up to and including” age, print the message “You put the bigger value in ’from’ . . . reversing them” and reverse them.

    To turn in: Please turn in the program in the file med1.py.

    Example 1:

    
    Age from . . . 20↵
    Age up to and including . . . 30↵
    For ages 20-30, there are 48 males and 51 females:
    Height: minimum 145, mean 159.66, maximum 178
    Weight: minimum 5, mean 77.79, maximum 161
    Systolic blood pressure: minimum 110, mean 134.32, maximum 160
    Diastolic blood pressure: minimum 70, mean 87.23, maximum 105
    Disease categories:
    Cancer: 9
    Diabetes: 8
    Healthy: 13
    Heart Disease: 23
    Hypertension: 12
    Nephrology: 11
    Stroke: 23
    

    Example 2:

    
    Age from . . . 70↵
    Age up to and including . . . 60↵
    You put the bigger value in ’from’ . . . reversing them
    For ages 60-70, there are 48 males and 48 females:
    Height: minimum 145, mean 160.34, maximum 178
    Weight: minimum 6, mean 85.44, maximum 166
    Systolic blood pressure: minimum 110, mean 135.83, maximum 159
    Diastolic blood pressure: minimum 70, mean 87.88, maximum 105
    Disease categories:
    Cancer: 12
    Diabetes: 18
    Healthy: 10
    Heart Disease: 9
    Hypertension: 17
    Nephrology: 16
    Stroke: 14
    

    Example 3:

    
    Age from . . . -3↵
    Please enter a positive integer
    

    Example4:

    
    Age from . . . 3↵
    Age up to and including . . . hello↵
    Please enter a positive integer
    

  2. (30 points) Write a recursive function to print out the depth of lists. For example, [ 3, [ 4, 5 ], [ 6, [ 7, [ 8 ] ] ] ] has depth 4 because 8 is in a list that’s an element in a list that is itself an element in list, which is the the element of the main list.

    Make sure your function is recursive! Also, you only need to write the function; you do not need to write anything that calls the function.

    To turn in: Please call your function listdepth, your file listdepth.py, and submit it to Canvas

    Example 1:

    
    listdepth( [ 3, [ 4, 5 ], [ 6, [ 7, [ 8 ] ] ] ])
    4
    
    Example 2:
    
    listdepth( [ [ [ [ [ [ [ ] ] ] ] ] ] ])
    7
    
    Example 3:
    
    listdepth([ "hello [ there ]" ])
    1
    
    Example 4:
    
    listdepth(5)
    Should never get here – called listdepth on non-list
    

  3. (20 points) The program ftok.py should convert temperatures in Fahrenheit to temperatures in Kelvin — recall the formula is:
    k =
    5 / 9
    (f − 32) + 273.15
    where k is the temperature as Kelvin and f is the temperature in Fahrenheit. It is to print out the temperatures as decimal numbers, rounded to 2 decimal places if they are not integers, and to reject any input if it converts to a negative Kelvin temperature. But it doesn’t work — indeed, it won’t even run to completion!

    Find the bug(s) and fix it (them). Put in your comments what you changed.

    Hint: The function to round a number x to two places is round(x, 2).

    To turn in: Please turn in the program in the file ftok-fixed.py.

    Example 1, from the correctly working program:

    
    Enter temperature in degrees Fahrenheit> 100↵
    100.0 Fahrenheit is 310.93 Kelvin
    
    Example 2, from the correctly working program:
    
    Enter temperature in degrees Fahrenheit> -459.67↵
    -459.67 Fahrenheit is 0.0 Kelvin
    
    Example 3, from the correctly working program:
    
    Enter temperature in degrees Fahrenheit> -600↵
    That temperature is below 0 Kelvin, which is impossible
    


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
MHI 289I, Programming in Health Informatics
Version of October 19, 2025 at 10:35PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh