Homework #5

Due Date: May 14, 2009 Points: 100

Written Exercises

  1. (5 points each) Consider this very simple function:
    def cube(x):
        answer = x * x * x
        return answer
    1. What does this function do?
    2. Show how a program could use this function to print the value of y3, assuming y is a variable.
    3. Here is a fragment of a program that uses this function:
      answer = 4
      result = cube(3)
      print answer, result
      The output from this fragment is 4 27. Explain why the output is not 27 27, even though cube seems to change the value of answer to 27.
    [text, §6.8, Discussion problem 5]
  2. (15 points) Here is a statement with lots of nesting:
    if a > b:
        if b > c:
            print "First"
            print "Second"
    elif b > c:
        if a >= c:
            print "Third"
        elif a < b:
            print "Fourth"
            print "Fifth"
        if a == b:
            print "Sixth"
            print "Seventh"
    Eliminate the nesting by combining conditions appropriately, using “and” and “or” as needed.

Programming Exercises

Remember to turn in your error logs and refinement files.

  1. (20 points) The body mass index (BMI) is calculated as a person’s weight (in pounds) times 720, divided by the square of the person’s height (in inches). A BMI in the range 19–25, inclusive, is considered healthy for most people (as always, consult your doctor to determine if your BMI is considered healthy for you). Write a program that calculates a person’s BMI and prints a message telling them whether they are above, within, or below the healthy range given above. Your program is to take as input a single line with weight first, a comma, and then height. Please turn in the program in the file bmi.py.
    [text, §7.7, Programming Exercises problem 5, modified]
  2. (20 points) Write a function to determine whether a year, given as the argument, is a leap year. A year is a leap year if it is divisible by 4, unless it is 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. Please turn in the program in the file leap.py.
    [text, §7.7, Programming Exercises problem 5, modified]
  3. (30 points) The greatest common divisor (GCD) of two integers m and n is the largest number that divides evenly into both of them. For example, the GCD of 12 and 15 is 3, and of 16 and 40 is 8. Write a program that asks the user to enter two integers, then calculates and displays their GCD. Please turn in your program in the file gcd.py.
    Hint: The classic algorithm for computing the GCD, known as Euclid’s Algorithm, goes as follows. Let m and n be variables containing the two numbers. Divide m by n. Save the divisor (that is, n) in m, and save the remainder in n. If n is 0, stop; m contains the GCD. Otherwise, repeat the process, starting with the division of m by n.

Extra Credit (Programming)

Remember to turn in your error logs and the refinement file.

  1. (20 points) The National Weather Service computes the windchill index using the following formula:

    35.74 + 0.6215T − 35.75(V0.16) + 0.4275T(V0.16)

    where T is the temperature in degrees Fahrenheit and V is the wind speed in miles per hour.
    Write a program that prints a nicely formatted table of windchill values. Rows should represent wind speed for 0 to 50 in 5 mph increments, and the columns should represent temperatures from −20 to +60 in 10-degree increments. Your program must use the above formula to compute the entries in the table. Please turn in your program in the file chill.py.
    [text, §8.7, Programming Exercises problem 2, modified]