Introduction to Computer Science
CompSci 101 : Spring 2014

Strings and Conditionals

For the exercises below, first try using paper, pencil, discussion, and thinking (no computer) to solve them, before using the Python Interpreter to check your answers.

Slicing and Dicing

Assume these four string values and associated variables for the following exercises:

a = "computational thinking"
b = "duke university"
c = "python code"

Use the following String operators to form each of the words below.

Examples
  1. "honk"

    This is created from one slice, one catenation, one index.

    c[3:6] + b[2]

    The value of c[3:6] is hon because the slice starts at the character whose index is 3 which is "h" and goes up to, but does not include the character whose index is 6. The value of b[2] is k because it is the single character whose index is 2. Concatenating these strings together makes "honk".

  2. "puking"

    This is created from two slices and one catenation.

    a[3:5] + a[18:]

    The value of a[3:5] is "pu". Note that the value of a[18:] starts at the character at index 18 (the 19th spot) and goes until the end of the string, i.e., king.

Exercises
  1. "docking"

     

  2. "cocoa"

     

  3. "thought"

     

  4. "ratatatat"

     

  5. "duke is cool"

     

  6. "diversity nation"

     

  7. "money honey"

 

Conditionals

  1. Which choice(s) assigns the correct, i.e., higher value, for higher speeds to the variable fee. If one is not correct, explain what value is finally assigned to fee.
    1. if speed > 35: fee = 20
      if speed > 50: fee = 40
      if speed > 75: fee = 60
    2.  

    3. if speed > 75: fee = 60
      if speed > 50: fee = 40
      if speed > 35: fee = 20
    4.  

    5. if 35 < speed <= 50: fee = 20
      if 50 < speed <= 75: fee = 40
      if 75 < speed: fee = 60

     

  2. To which of the following is this expression equivalent? If one is not correct, give values for x and y that were True in the original, but not in the revised version.
    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)
  3.  

  4. 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 timeanddate.com:
    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 functions correctly identitfies a leap year according to the rules above. If a function is not correct, provide a value for year that is misidentified.

    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