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")=="")
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)
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()
If we call the function draw_them_all once, how many times in total does an eye get drawn?
Circle (or indicate some other way) the last call to draw_eye that gets executed in a complete call to draw_them_all.
The cost of framing a picture depends on the material used for the frame and the size of the picture.
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 ___________
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)
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).
'''
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)
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
'''
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)