






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Solutions to the CS 1110 final exam questions related to Python classes and objects, including object folders, class folders, local and global variables, class attributes, superclass definitions, method overrides, and attribute overrides.
Typology: Exams
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Last Name: First Name: Cornell NetID, all caps:
1 class A(): 2 x = 1 3 4 def init(self, n): 5 self.y = n 6 A.x += 1 7 8 def p(self): 9 print(self.y) 10 self.y += 3 11 self.r() 12 13 def r(self): 14 self.y += 2 15 print(self.y) 16 17 class B(A): 18 x = 10 19 20 def init(self, n): 21 super().init(n) 22 sum = self.y + B.x 23 self.m = sum 24 25 def r(self): 26 self.y += self.x 27 print(self.m) 28 29 a = A(1) 30 b = B(2)
an object folder is created when Python exe- cutes line
a class folder is created when Python executes line
an object attribute is created on line
a class attribute is created on line
a superclass de nition begins on line
a class method de nition begins on line
an attribute de nition that overrides another begins on line
a method de nition that overrides another be- gins on line
a local variable is created on line
a global variable is created on line
Solution: object folder created: 29 or 30 method override: 20 or 25 class folder created: 1 or 17 local variable: 22 object attribute created: 5 or 23 global variable: 1, 17, 29, or 30 class attribute created: 2 or 18 superclass begins: 1 class method begins: 4, 8, 13, 20, 25 attribute override: 18
This code is copied from the previous page with two additional lines of code. It runs error-free. 1 class A(): 2 x = 1 3 4 def init(self, n): 5 self.y = n 6 A.x += 1 7 8 def p(self): 9 print(self.y) 10 self.y += 3 11 self.r() 12 13 def r(self): 14 self.y += 2 15 print(self.y) 16 17 class B(A): 18 x = 10 19 20 def init(self, n): 21 super().init(n) 22 sum = self.y + B.x 23 self.m = sum 24 25 def r(self): 26 self.y += self.x 27 print(self.m) 28 29 a = A(1) 30 b = B(2) 31 a.p() 32 b.p()
(b) [4 points] What will be printed when Python executes line 31? Solution: 1 6 (c) [4 points] What will be printed when Python executes line 32? Solution: 2 12
The same code has been copied to this page for your convenience: class MenuItem(): """An instance represents an item on a menu.""" def init(self, name, is_veggie, price): """A new menu item called name with 3 attributes: name: a non-empty str, e.g. 'Chicken Noodle Soup' is_veggie: a Bool indicating vegetarian or not price: an int > 0 """ self.name = name self.is_veggie = is_veggie assert price > 0 self.price = price
class LunchItem(MenuItem): """An instance represents an item that can also be served at lunch""" def init(self, name, is_veggie, price, lunch_price): """A menu item with one additional attribute: lunch_price: an int > 0 and <= 10""" super().init(name, is_veggie, price) assert lunch_price > 0 assert lunch_price <= 10 self.lunch_price = lunch_price (d) [8 points] For Loops. Make effective use of a for-loop to write the body of the function audit menu according to its speci cation. def audit_menu(the_menu): """Performs an audit of each LunchItem on the_menu, making sure that each lunch_price is never more than 10 dollars. A lunch_price of 11 dollars is changed to 9. An item whose lunch_price is more than 11 is too expensive to be offered at lunch; it must be replaced with a new, equivalent MenuItem (that has no lunch price). Items that are not LunchItems are unchanged. Modifies the_menu; does not create or return a new menu/list the_menu: possibly empty list of MenuItem """ Solution:
for i in list(range(len(the_menu))): item = the_menu[i] if isinstance(item, LunchItem): if item.lunch_price == 11: item.lunch_price = 9 elif item.lunch_price > 11: newItem = MenuItem(item.name, item.is_veggie, item.price) the_menu[i] = newItem
def after_at(s): """Returns a list of every non-empty sequence of non-space characters that follows an @ in s.
The elements should be ordered by occurrence in s, and there should be no repeats.
Pre: s is a string, possible empty. """ temp = s.split('@') if len(temp) == 1: # There were no @s in s. return [] afters_list = temp[1:] # Drop stuff before 1st @. See table at bottom of page.
Solution: Code for testing your own implementation: http://www.cs.cornell.edu/courses/cs1110/2018sp/exams/final/2018_spring_string_processing.py
outlist = [] for item in afters_list: space_pos = item.find(' ') if space_pos == -1: if item != '' and item not in outlist: outlist.append(item) elif space_pos > 0: follower = item[:space_pos] if follower not in outlist: outlist.append(follower)
Alternate solution by Kevin Cook, with some variable-name changes to match the above: outlist = [] for item in afters_list: i = 0 follower = '' while i < len(item) and item[i] != ' ': follower += item[i] i += 1 if follower not in outlist and len(follower) > 0: outlist.append(follower) return outlist
b. If you made the mistake of removing from after_list in a for loop, you used after_list as your accumulator, so you will still get this point.
The other mistake we saw was using strip(). Strip() would get rid of the empty spaces, but also “\n”, “\t”, etc, which we would like to keep in this case. If you are appending string.strip() to the output_list, you are possibly adding an empty string to the output_list, and will lose the point for rubric item 7.
1 def fib(n): 2 sum = 0 3 if n == 0 or n == 1: 4 return 1 5 6 sum += fib(n-1) 7 sum += fib(n-2) 8 return sum 9 10 x = fib(2)
Solution:
i b <= splitter > splitter
Examples: Before After splitter b i b 0 [16, -4, 22] 1 [-4, 22, 16] or [-4, 16, 22] 0 [-10, -20, 15] 2 [-10, -20, 15] or [-20, -10, 15] 0 [-30, -50, -60] 3 any ordering of b works -4 [10, 20, 30] 0 any ordering of b works
The code must maintain the following invariant.
i k b <= splitter > splitter ???
In words, b[0..i-1] are all less than or equal to splitter; b[i..k-1] are all greater than splitter; b[k..len(b)-1] have not yet been processed. Solution: This invariant was inspired by Section 2.2 of Kernighan and Pike, The Practice of Programming (1999). (a) [2 points] According to the invariant, should the initialization be i=1?