CPSC_103_Midterm_2021W1_Solution-1

.pdf

School

University of British Columbia *

*We aren’t endorsed by this school

Course

103

Subject

Accounting

Date

Apr 3, 2024

Type

pdf

Pages

12

Uploaded by AgentBatPerson1076

Report
2021W1 Midterm Answer Key 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 8 Question 2 def foo1(x): return x/2 def foo2(x, y): y = 20 x == y x = foo1(y) y = 3 return x foo2(6, 5) 10 Page 1 of 12 No Unauthorized Distribution of Materials
2021W1 Midterm Answer Key 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: Its name, as it appears in the menu Its price Whether or not it is dairy-free 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). 01. from typing import NamedTuple 02. Beverage = NamedTuple( 'Beverage' , [( 'name' , str ), 03. ( 'price' , float ), 04. ( 'dairy_free' , str )]) 05. 06. #interp. a beverage available in the cafe’s menu, 07. # including its name, price, and whether or not it is dairy free. 08. 09. BLACK_COFFEE = Beverage( "Black coffee" , 2.50 , "Yes" ) 10. EARL_GRAY = Beverage( "Earl Gray Tea" , 3.25 , "Yes" ) 11. LONDON_FOG = Beverage( "London Fog" , 4.75 , "No" ) 12. 13. # template based on compound (3 fields) 14. @typecheck 15. def fn_for_drink (b: Beverage) -> ... : 16. return ... (b . name, 17. b . price) Line 3: an interval comment is needed to show that price cannot be a negative number Line 4: dairy_free should be a bool, not a str Lines 9-11: The “Yes” should be changed to True and the “No” should be False Line 15: It should be fn_for_beverage , not fn_for_drink Line 18: Missing a b.dairy_free in the data template. Page 2 of 12 No Unauthorized Distribution of Materials
2021W1 Midterm Answer Key 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) The data definition for race is already given to you: from enum import Enum Race = Enum("Race", ["HUMAN", "ELF", "DWARF", "OGRE"]) # interp. The possible races for a character in the game # examples are redundant for enumerations @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 Answer Key from typing import NamedTuple Character = NamedTuple(‘Character’, [(‘name’, str), (‘race’, Race), (‘health’, int) # in range [0,300] (‘mana’, int) # in range [0,300]]) # interp. information about a character in the RPG game, # including name (name), race (race), health points # (health), and mana points (mana). CH1 = Character(“Raghard”, Race.Human, 150, 100) CH2 = Character(“Wargo”, Race.Ogre, 0, 300) CH3 = Character(“Oggy”, Race.Dwarf, 300, 0) # template based on compound (4 fields) and reference rule @typecheck def fn_for_character(c: Character) -> ...: return ...(c.name, fn_for_race(c.race), c.health c.mana) Page 4 of 12 No Unauthorized Distribution of Materials
2021W1 Midterm Answer Key Data Definitions Used in Questions 5 and 6 Type = Enum("Type", ["APARTMENT", "HOUSE"]) # interp. Property type. It can be an apartment or a house. # Examples are redundant for enumerations # template based on Enumeration @typecheck def fn_for_type(t: Type) -> ...: if t == Type.APARTMENT: return ... elif t == Type.HOUSE: return ... Property = NamedTuple("Property", [("type", Type), ("square_feet", int), # in range (0,...) ("year", int), ("price", int), ("pending", bool)]) # interp. information about a property, including its type # (apartment or house), footage, the year it was built, # price and whether or not it has a pending offer. P1 = Property(Type.APARTMENT, 625, 2020, 450000, False) P2 = Property(Type.HOUSE, 1100, 1980, 1350000, False) P3 = Property(Type.HOUSE, 925, 1995, 1100000, True) # template based on Compound and reference rule @typecheck def fn_for_property(p: Property) -> ...: return ...(fn_for_type(p.type), p.square_feet, p.year, p.price, p.pending) Page 5 of 12 No Unauthorized Distribution of Materials
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help