Professional Documents
Culture Documents
Question 1
numbers = [2, 3, 2, 7, 4, 3]
tot = 0
for i in numbers:
if is_even(i): # assume is_even(i) is a function that is
# complete and correct (and available for use)
# is_even(i) returns True if i is even
tot = tot + i
tot
Question 2
def foo1(x):
return x/2
foo2(6, 5)
Page 1 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
Question 3
The Beverage data below (similar to the one you saw in Worksheet 4) records information about
a beverage offered at the cafe, including:
Even though this data definition will run with no errors, there are some errors in the data design.
Identify the line number where each error is found and describe how you would fix each error.
For example, if you believe a line should be removed, write its number and SHOULD BE
REMOVED after it (capitalization does not matter) (i.e., Line 7 should be removed or LINE 7
SHOULD BE REMOVED).
Page 2 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
Question 4
Time to work on a classic RPG game! Design a data type for a character in a game. A
character’s information is comprised of:
● Its name
● Its race (one of: human, elf, dwarf, ogre)
● Its health points (between 0 and 300)
● Its mana points (between 0 and 300)
@typecheck
# Template based on Enumeration (4 cases)
def fn_for_race(r: race) -> ...:
if r == Race.HUMAN:
return …
elif r == Race.ELF:
return …
elif r == Race.DWARF:
return ...
elif r == Race.OGRE:
return ...
Page 3 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
Page 4 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
Page 5 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
# List[Property]
# interp. a list of properties in the agency’s inventory
L0 = []
L1 = [P1, P2, P3]
for p in lop:
acc = ...(fn_for_property(p), acc)
return ...(acc)
Page 6 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
Question 5
The data definitions on the previous two pages are for a Real Estate agency that deals in
private homes. Each property has a type (apartment or house), square feet, year built, price,
and a status to indicate whether or not an offer is pending on it.
The following function examines a list of properties to determine whether or not it contains a
house that costs at most $1000000 and is of a given footage or greater. Complete the helper
functions and add new ones if needed. You can assume has_cheap_house_min_footage() is
complete and correct.
Be sure to follow the HtDF recipe. You are allowed to assume that the tests for all functions in
this question are present, complete, and correct.
@typecheck
def has_cheap_house_min_footage(lop: List[Property], sf: int) -> bool:
"""
Returns True if there is at least one house of footage
equal or greater than sf that costs at most one
million dollars in the list; False otherwise.
"""
# return True # stub
return False
# Complete the helper functions below and add new functions if needed
# Be sure to follow the FULL HtDF recipe
Page 7 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
@typecheck
def is_property_house(p: Property) -> bool:
"""
Returns True if p is a house; False otherwise.
"""
return True # stub
@typecheck
def has_min_footage(p: Property, square_footage: int) -> bool:
"""
Returns True if p's square footage (i.e., size) is at
least square_footage large; False otherwise.
"""
return True # stub
Page 8 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
@typecheck
def costs_at_most_one_million(p: Property) -> bool:
"""
Returns True if p costs one million dollars or less;
False otherwise.
"""
return True # stub
# You are allowed to assume that all tests required for the
# functions above are present, complete, and correct
Page 9 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
Question 6
Consider the Real Estate agency data definitions given earlier.
We want to design a function to return all apartments built after a given year and below a given
price.
Here are some functions which may or may not prove useful. It is possible that your answer will
not call every helper function listed in this question. You cannot write any other function for this
question. Only use what you are given.
You do NOT need to provide a complete implementation of the helpers. You only need to
complete find_apartments_by_year_and_price(). You do not need to provide tests for
find_apartments_by_year_and_price().
@typecheck
def find_all_apartments(lop: List[Property]) -> List[Property]:
"""
Returns all apartments in lop.
"""
return [] # stub
@typecheck
def is_property_apartment(p: Property) -> bool:
"""
Returns True if p is an apartment; False otherwise.
"""
# return True # stub
@typecheck
def is_apartment(t: Type) -> bool:
"""
Returns True if t represents an apartment; False
otherwise.
"""
# return True # stub
Page 10 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
@typecheck
def find_properties_in_budget(lop: List[Property], p: int) ->
List[Property]:
"""
Returns all properties with price equal to or below p.
"""
return [] # stub
@typecheck
def is_property_in_budget(p: Property, pr: int) -> bool:
"""
Returns True if the property p has price equal to or
below pr; False otherwise.
"""
# return True # stub
@typecheck
def find_properties_built_after_year(lop: List[Property], y: int) ->
List[Property]:
"""
Returns all properties built strictly after year y.
"""
return [] # stub
@typecheck
def is_property_built_after_year(p: Property, y: int) -> bool:
"""
Returns True if the property p was built strictly
after year y; False otherwise.
"""
# return True # stub
Page 11 of 12
No Unauthorized Distribution of Materials
2021W1 Midterm
@typecheck
def find_apartments_by_year_and_price(lop: List[Property], y: int, p:
int) -> List[Property]:
"""
Returns a list of all apartments built after a given
year (y) and below a given price (p).
"""
# return [] # stub
Page 12 of 12
No Unauthorized Distribution of Materials