ICS 31 -- Winter 2013 -- Quiz 4


    1. For each of the shaded portions of the code below, indicate what type belongs in that space. Choose from int, float, str, bool, Restaurant, or list.  (It doesn't matter that the text under the shading is barely visible.)


      def nonvowels(s: str) -> str:
      ''' Return a string containing the nonvowels in s
      '''
      result = ''
      for c in s:
      if not is_vowel(c):
      result += c
      return result
      assert(nonvowels("")=="")
      assert(nonvowels("eieiO")=="")
      Answer Feedback:
      In nonvowels: str, bool, str.


      In nonvowels assertions: bool, str


    2. For each of the shaded portions of the code below, indicate what type belongs in that space. Choose from int, float, str, bool, Restaurant, or list.


      def cheapest(L: list) -> Restaurant: 
      ''' Return the Restaurant with the lowest price, from the parameter
      '''
      if L == []:
      return None
      L.sort(key=Restaurant_price)
      return L[0]
      assert(cheapest(RL) == R10)
      assert(cheapest([R24, R25, R26]) == R24)
      Answer Feedback:
      In cheapest: list, Restaurant


      In cheapest assertions: list, Restaurant, Restaurant
  1. Below is some code to draw a variety of figures using turtle graphics. However, you don't need to know any turtle graphics operations to answer this question. (Some function definitions below are missing or incomplete. None of the missing details is necessary to answer this question.)

    def draw_them_all():
    ''' Draw a variety of figures. The question asks you to
    count the total number of eyes drawn.
    '''
    draw_eye(200,200)
    draw_Martian_face(0,0)
    draw_Martian_face(100,100)

    def draw_Martian_face(x: int, y: int):
    ''' Draw, centered at (x, y), a Martian face with three eyes.
    '''
    draw_head(x, y)
    draw_nose(x, y)
    draw_mouth(x, y-50)
    draw_eye(x-50, y+20)
    draw_eye(x, y+20)
    draw_eye(x+50, y+20)

    def draw_eye(x: int; y: int) -> None:
    ''' Draw an eye centered at (x, y)
    '''
    [body omitted]

    draw_them_all()

    1. If we call the function draw_them_all once, how many times in total does an eye get drawn?

      Answer Feedback:
      7

    2. Circle (or indicate some other way) the last call to draw_eye that gets executed in a complete call to draw_them_all.

      Answer Feedback:
      The last call to draw_eye in draw_Martian_face.
  2. The cost of framing a picture depends on the material used for the frame and the size of the picture.

    1. In the function below, fill in the blanks (with one string, number, operator, or identifier [variable/function/method name] per blank according to the description given.
      def price_per_inch(material: str) -> float:
      ''' If parameter is 'maple', return 2.95; if it's 'laquer',
      return 3.50; if it's 'aluminum', return 1.25; otherwise
      return 0
      '''
      if ___________ == ___________:
      return 2.95
      elif ___________ == ___________:
      return 3.50
      elif ___________ == ___________:
      return 1.25
      else:
      return ___________
      Answer Feedback:
      def price_per_inch(material: str) -> float:
      ''' If parameter is 'maple', return 2.95; if it's 'laquer',
      return 3.50; if it's 'aluminum', return 1.25; otherwise
      return 0
      '''
      if material == 'maple':
      return 2.95
      elif material == 'laquer':
      return 3.50
      elif material == 'aluminum':
      return 1.25
      else:
      return 0
      assert(price_per_inch('bamboo')==0)
      assert(price_per_inch('laquer')==3.50)
    2. Below is a function header and docstring describing a task. First, write two assert statements that give examples of calling this function and the correct results for those examples. Then, complete the definition according to the docstring and parameter types given.
      def material_needed(height: float, width: float) -> float:
      ''' Given the height and width of a picture in inches), return
      the number of inches of framing material required (for all
      four sides of the picture, of course).
      '''
      Answer Feedback:
      def material_needed(height: float, width: float) -> float:
      ''' Given the height and width of a picture in inches), return
      the number of inches of framing material required (for all
      four sides of the picture, of course).
      '''
      return (height * 2) + (width * 2)
      assert(material_needed(10, 5) == 30)
      assert(material_needed(25, 30) == 110)

    3. For the function described below, first write two assert statements as examples/tests of its operation, then complete the function definition according to the description. Where appropriate, call the functions you defined above.


      def frame_cost(height: float, width: float, material: str) -> float:
      ''' Return the cost of framing a picture whose size and framing
      material are specified by the parameters
      '''
      Answer Feedback:
      def frame_cost(height: float, width: float, material: str) -> float:
      ''' Return the cost of framing a picture whose size and framing
      material are specified by the parameters
      '''
      return price_per_inch(material) * material_needed(height, width)
      assert(frame_cost(10, 5, 'laquer') == 30 * 3.50)
      assert(frame_cost(25, 30, 'maple') == 110 * 2.95)

      A solution that doesn't call the other two functions correctly (and instead duplicates the computation that those two functions do) would not get full credit.