Homework 1

Due: October 15, 2020
Points: 100


In the given examples, what you type is in red and the program prints what is in black. Your program output should look exactly like the output in the examples, except that what you type won’t be in red, and the results may differ in the last few digits.

  1. (20 points) Currently, the U.S. tax code imposes a graduated income tax with deductions and credits. Occasionally, someone will propose a “flat tax,” which charges a fixed percent of income and has no deductions or credits. The figure most often quoted is 17%. Please write a program that asks the user to enter an income as an integer (no commas or periods!) and prints the amount of tax that would be paid on that income.

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

    Examples:

    
    What is the income? 124598
    The tax on income of 124598 is 21181.66
    
    
    What is the income? -250
    The tax on income of -250 is -42.5
    
    
    What is the income?  124,598
    Error – invalid input entered; put in an integer
    

  2. (35 points) Write a program to calculate the volume V of a right circular cone given the radius r of its base and height h using this formula:
    V = π r2h/3
    Prompt the user for the radius and height, one per line. Both must be real numbers; they need not be integers.

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

    Examples:

    
    Radius of base of cone: 1
    Height of cone: 5
    The volume of a cone with radius 1.0 and height 5.0 is 5.235987755982989
    
    
    Radius of base of cone: 2.3
    Height of cone: 3.4
    The volume of a cone with radius 2.3 and height 3.4 is 18.834895155822
    
    
    Radius of base of cone:  1/5
    Error – radius must be a real number
    
    
    Radius of base of cone:  1
    Height of cone:  3*4
    Error – height must be a real number
    

  3. (45 points) In the SI model of disease spread, i(t) is the proportion of the population that is infected at time t and β is the average number of people each infected person spreads the disease to each day. The equation modeling the spread is:
    i(t) = i(0)eβt/(1 − i(0) + i(0)eβt)
    where i(0) is the proportion of the population initially infected. Assume each infected person spreads the disease to 1 uninfected person every 5 days, and initially 0.1% of the population is infected. The population is constant. Write a program that takes an integer as input, and computes the proportion of the population that is infected after that many days.

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

    Examples:

    
    Number of days:  30
    After 30 days, the proportion of the population infected is 0.287664368675717
    
    
    Number of days: 40
    After 40 days, the proportion of the population infected is 0.7489923252324207
    
    
    Number of days: 10.3
    You must enter an integer number of days
    

Extra Credit

  1. (25 points) These build upon your program for the SI model, above.
    1. (10 points) Print the proportion of the population infected for each day, up to and including the number of days entered as input.

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

      Example:

      
      Number of days: 10
      1 	 0.0012211323960795992
      2 	 0.00149109134209279
      3 	 0.0018206220327889482
      4 	 0.0022228167755574827
      5 	 0.0027136190661283567
      6 	 0.0033124316939081035
      7 	 0.00404284825698398
      8 	 0.004933530020258307
      9 	 0.006019252361985584
      10 	 0.007342146711703001
      

    2. (15 points) Now prompt for a second input. You are to print the proportion of the population infected on every day that is a multiple of the second input, and the last day (even if it isn’t). Be careful — print the last day’s proportion only once, even if it is a multiple of the second input!

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

      Example:

      
      Number of days: 180
      How often to print (first, last always printed): 15
      15 	 0.019709372957867413
      30 	 0.287664368675717
      45 	 0.8902449144669539
      60 	 0.9938993778932189
      75 	 0.9996944969424588
      90 	 0.9999847854817202
      105 	 0.999999242502787
      120 	 0.9999999622864073
      135 	 0.9999999981223507
      150 	 0.9999999999065173
      165 	 0.9999999999953458
      180 	 0.9999999999997683
      


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
MHI 289I, Programming for Health Informatics
Version of October 1, 2020 at 11:38AM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh