Homework 2 Revision 1

Due: October 30, 2020
Points: 100


Question 2 has been revised to not require recursion, as we will not have covered it.

In the examples, input is shown in red, output in black, and the character “↵” means “return” or “enter”.

  1. (30 points) Write a function to determine whether a year, given as input, is a leap year. A year is a leap year if it is evenly divisible by 4, unless it is evenly divisible by 100 and not 400. So 2000 was a leap year, but 2100 and 2200 will not be. Then write a program that asks the user to enter a year and uses the function you wrote to determine whether the year is a leap year. The program then prints the result.

    Your program must give an error message and exit if the user enters anything other than a positive integer.

    Here is sample output. Each is from a separate run of the program.

    
    Year> 2020↵
    2020 is a leap year
    
    
    Year> 2000↵
    2000 is a leap year
    
    
    Year> 1900↵
    1900 is not a leap year
    
    
    Year> hello↵
    You must enter the year as a positive integer
    
    
    Year> -12↵
    You must enter the year as a positive integer
    
    To turn in: Please turn in the program in the file leap.py.

  2. (40 points) A string is said to be abcdearian if the letters in it, regardless of case, are in dictionary order. So, for example, “almost” and “effort” are abcdearian, and “willow” and “computer” are not.

    1. Write a recursive function called isabcde(s) that returns True if s contains a string that is abcdearian, and False otherwise. The function must ignore any non-letter characters in s, and treat all alphabetic characters as lower case. Note the function must be recursive, and so must call itself.

    2. Write a program that reads a string and uses the function to determine whether the string is abcdearian. The program is to loop until the user types an end of file (control-D), or another exception occurs.

      Here is sample output:

      
      The string? heLlo↵
      heLlo is not abcdearian
      The string? aLmost↵
      aLmost is abcdearian
      The string? w3i$l0l!ow↵
      w3i$l0l!ow is not abcdearian
      The string? e3f$f0o!rt↵
      e3f$f0o!rt is abcdearian
      The string? cOmpuTer↵
      cOmpuTer is not abcdearian
      The string? ABcDE↵
      ABcDE is abcdearian
      The string? control-D
      
    To turn in: Please turn in the function and the program in the file abcde.py.

  3. (30 points) Write a program that prompts the user for a list of numbers, one per line. When the user enters “done”, print the maximum, minimum, and mean (average) of the numbers entered.If the user enters anything other than a number and “done”, give an error message.

    Your program must give an error message and exit if the user enters anything other than an integer.

    Here is sample output. Each is from a separate run of the program.

    
    Enter number> 13↵
    Enter number> 12↵
    Enter number> 11↵
    Enter number> 10↵
    Enter number> 9↵
    Enter number> xyzzy↵
    Not a number
    Enter number> done↵
    The maximum is 13
    The mean is 11.00
    The minimum is 9
    

    
    Enter number> -75↵
    Enter number> -135↵
    Enter number> 32↵
    Enter number> 127↵
    Enter number> 21↵
    Enter number> 138↵
    Enter number> 56↵
    Enter number> done↵
    The maximum is 138
    The mean is 23.43
    The minimum is -135
    

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

Extra Credit

  1. (30 points) Take the program you wrote for problem 3. In addition to what that prints, have it print out the variance of the numbers entered.

    Hint: You do not have to save all the numbers entered to do this. You can use the formula

    \frac{1}{n}\left(\sum_{i=1}^{n} x_i^2\right) - \mu^2
    where n is the number of numbers entered and μ is their mean (average).

    Here is sample output. Each is from a separate run of the program.

    
    Enter number> 13↵
    Enter number> 12↵
    Enter number> 11↵
    Enter number> 10↵
    Enter number> 9↵
    Enter number> done↵
    The maximum is 13
    The mean is 11.00
    The minimum is 9
    The variance is 2.00
    

    
    Enter number> -75↵
    Enter number> -135↵
    Enter number> 32↵
    Enter number> 127↵
    Enter number> 21↵
    Enter number> 138↵
    Enter number> x↵
    Not a number
    Enter number> 56↵
    Enter number> done↵
    The maximum is 138
    The mean is 23.43
    The minimum is -135
    The variance is 8540.24
    


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 21, 2020 at 11:56AM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh