ICS 31 -- Winter 2013 -- Quiz 3

  1. For each of these sequences of statements, what does Python print?

    1. def triple(n: int) -> int:
      ''' Return three times the parameter
      '''
      return n * 3

      print('Four')
      print(triple(5))
      print('Score')
      print(triple(2) + triple(10))
      print(triple(3), triple(100))
      Answer Feedback:
      Four
      15
      Score
      36
      9 300

      [Each of these items should be on its own line, except for 9 and 300. EEE may not format them correctly, however.]
    2. def print_n_copies(n: int, s: str): 
      ''' Print specified number of copies of string
      '''
      print(n * s)
      return

      print('Heads')
      print_n_copies(4, 'Flip')
      print('Tails')
      Answer Feedback:
      Heads
      FlipFlipFlipFlip
      Tails
      [The correct formatting has three lines, one per item. EEE may not show it that way.]
  2. Assume you have the following definitions:

    a = 5
    Animal = namedtuple('Animal', 'name species age weight')
    L = ['Tinker', 'Evers', 'Chance']

    What is the data type of each of the following expressions? Choose from int, float, bool, str, list, or Animal.

    1. a * 10 
      Answer Feedback:
      int
    2. 3.14159
      Answer Feedback:
      float
    3. 'rhinoceros'
      Answer Feedback:
      str
    4. 12 * (5 + 1)
      Answer Feedback:
      int
    5. len('Hippopotamus')
      Answer Feedback:
      int
    6. Animal('Roger', 'rhinoceros', 45, 1500)
      Answer Feedback:
      Animal
    7. Animal('Roger', 'rhinoceros', 45, 1500).age
      Answer Feedback:
      int
    8. [2, 4, 6, 8, 10]
      Answer Feedback:
      list
    9. L
      Answer Feedback:
      list
    10. L[1]
      Answer Feedback:
      str
  3. For each of these sequences of statements, what does Python print?

    1. s = 'Duck'
      t = 'Duck'
      print('Donald')
      if s >= t:
      print('Huey')
      print('Dewey')
      print('Louie')
      Answer Feedback:
      Donald
      Huey
      Dewey
      Louie
    2. a = 10
      b = 20
      if a >= b:
      print('Huey')
      else:
      print('Dewey')
      print('Louie')
      Answer Feedback:
      Dewey
      Louie
    3. p = 'Mickey'
      q = 'Minnie'
      print('Mouse')
      if p == q:
      print('Goofy')
      print('Pluto')
      Answer Feedback:
      Mouse
      Pluto
    4. L = ['Huey', 'Dewey', 'Louie']
      print('Nephews')
      for duck in L:
      print(duck)
      print('Uncle Donald')
      Answer Feedback:
      Nephews
      Huey
      Dewey
      Louie
      Uncle Donald
    5. L = [2000, 2004, 2008, 2012]
      print('Election years')
      for i in range(4):
      print(i, "--", L[i])
      print('Remember to vote!')
      Answer Feedback:
      Election years
      0 -- 2000
      1 -- 2004
      2 -- 2008
      3 -- 2012
      Remember to vote!
  4.  

    Write a function called abbr that takes two non-empty strings (i.e., you can assume each string is at least one character long; you don't have to check).  It should return a two-character abbreviation constructed of the first character of each of the input strings.  For example,

    abbr('University', 'California') 

    returns 'UC'. Your definition should follow the design recipe by including: (a) type specifications for the parameters and return value, (b) a docstring comment, and (c) at least two example calls with the expected answers that can be run as tests; these can be print statements or assert statements.

     

    Answer Feedback:
    def abbr(s1: str, s2: str) -> str:
    ''' Return abbreviation made from first char of each parameter
    '''
    return s1[0] + s2[0]
    print(abbr('University', 'California'), 'should be UC')
    print(abbr('Los', 'Angeles'), 'should be LA')
  5. Why is it a good idea to avoid duplicate code in our programs?

    Answer Feedback:

    • Duplicate code makes the program longer than it needs to be.
      Unnecessarily longer code is more cluttered and harder for
      people to read and undersand.

    • If you need to change code that appears more than once, you
      have to find all the appearances and change all of them, or your
      program becomes an inconsistent mess.


    Lab Assignment 2 addressed this issue. It's important to read the explanatory material in the lab assignments.
  6. In this Python code:

    def double(n: int) -> int:
    ''' Return twice the parameter value '''
    return 2 * n
    print(double(13), "should be 26")

    identify each of the following:

    1. function name (in definition)
    2. function definition
    3. function call (of the function double)
    4. argument to a call to double
    5. definition of a parameter in double
    6. use of a parameter in double
    7. return type specification
    8. docstring comment ("purpose statement")

    (On a paper exam, you could circle portions of the code and draw arrows. Electronically, just copy the list and, after each item, copy the text that applies.)

    Answer Feedback:
    -- function name (in definition):  double [first occurrence]
    -- function definition: def line and next two lines
    -- function call (of the function double): double(13)
    -- argument to a call to double: 13
    -- parameter definition in double: (n: int)
    -- parameter use in double: 2 * n
    -- return type specification: -> int
    -- docstring comment ("purpose statement"): triple-quoted string [line 2]
  7. Write a function called least_expensive_dish that takes a list of restaurants and returns the name of the least expensive dish on the list.

    There are two approaches to this; either one is fine. The first approach, as in Lab 3, uses the sort method on lists with the key= argument and the name of a function like this one:

       def Restaurant_price(r: Restaurant) -> float:
    ''' Return the value of the price attribute of the parameter
    '''

    (You don't have to define this function; assume it already exists.)

    The second approach just goes through the list with a loop, keeping track of the lowest-priced item it has found.

    Answer Feedback:
    def least_expensive_dish(L: list) -> str:
    ''' Return the name of the least expensive dish in the list
    '''
    L.sort(key=Restaurant_price)
    return L[0].dish

    def least_expensive_dish2(L: list) -> str:
    ''' Return the name of the least expensive dish in the list
    '''
    lowest_price_so_far = L[0].price
    lowest_dish_so_far = L[0].dish
    for r in L[1:]:
    if r.price < lowest_price_so_far:
    lowest_price_so_far = r.price
    lowest_dish_so_far = r.dish
    return lowest_dish_so_far