Compsci 6/101, Fall 2011, Lab 4

This lab has the following parts:

Turn in this page for your group

Reasoning about Conditionals

  1. To which of the following is this expression equivalent:

    not ((x > y) and (y <= 3))
    1. (x > y) and (y <= 3)
    2. (x > y) or (y <= 3)
    3. (x < y) or (y >= 3)
    4. (x <= y) or (y > 3)
    5. (x <= y) and (y > 3)
  2. A year is a leap year when it has 366 days instead of 365 days. In the international Gregorian calendar a year is a leap year according to the following, correct but perhaps poorly worded, rules as obtained from http://www.timeanddate.com/date/leapyear.html
    1. Every year divisible by 4 is a leap year
    2. But every year divisible by 100 is NOT a leap year
    3. Unless the year is also divisible by 400, then it is still a leap year

    This means 1800, 1900, and 2100 are not leap years but 2000 and 2004 are leap years.

    Which of the following implementations of a method is_leap returns true if year is a leap year and false otherwise. Circle those that are correct. If a method is not correct, provide a value for year for which it returns the wrong value.

    def is_leap1 (year):
      if year % 400 == 0: return True
      if year % 100 == 0: return False
      if year %   4 == 0: return True
      return False
    
    
    def is_leap2 (year):
      return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)
    
    
    def is_leap3 (year):
      if year % 100 == 0:                  return False
      if year % 400 == 0 or year % 4 == 0: return True 
      return False
          
  3. Complete the function all_same that returns true only if all three of the given values are the same. For example, a call to all_same with the values 3, 128, 255 should return False; while the values 128, 128, 128 should return True.
  4. def all_same (value1, value2, value3):
    
    
    
    

More Songs

Write three separate programs that each print out the lyrics to the following common children's songs below. Your output of the song lyrics should be as similar as possible to what appears on the websites, including line breaks and blank lines. Although you do not need to write out the numbers (i.e., ninety-nine), it is okay to write 99 instead.

To print a single verse of each song, use as many functions as necessary to minimize simple repetition in your print statements. Once you can correctly print any single verse of the song (using parameters to change what specifically is printed), write a loop to print the entire song by repeatedly calling your single verse function with different values generated within the loop.

  1. 99 Bottles of Beer on the Wall
    Both the kind of drink on the wall (a String) and the initial number of bottles of it on the wall (an int) should be passed as parameters to a single function that prints the whole song.

    Note that the program should use corect grammar so that it does not print plural "bottles" when there is only one bottle left (as shown below).

    1 bottle of sarsaparilla on the wall
  2. He's Got the Whole World in His Hands
    Use the list below as a parameter to a single function that prints the whole song.
    objects = [ "the whole world",
                "the itty bitty baby",
                "a-you and me brother",
                "a-you and me sister", 
                "the whole world" ]
  3. This is the Song That Never Ends
    You cannot complete this song with a definite loop (i.e., from range or a list), but it is easily done with an indefinite loop or even with no loop at all!