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
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
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 (a) [2 points] Write a python assignment statement that stores in variable item1 the ID of a new MenuItem object whose name is \Tofu Curry", a vegetarian dish costing 24 dollars. Solution: item1 = MenuItem("Tofu Curry", True, 24) (b) [2 points] Write a python assignment statement that stores in variable item2 the ID of a new LunchItem object whose name is \Hamburger", a non-vegetarian dish that costs 12 dollars, but only 8 dollars at lunch. Solution: item2 = LunchItem("Hamburger", False, 12, 8)
(c) [2 points] Class Invariants. Lunch should never cost more than 10 dollars. The init method prevents this. Write a line of python that shows how this invariant can still be broken. You may use any of the global variables you created from the previous parts. Solution: item2.lunch price = 16
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
Some further notes on the string processing question by TA Nancy Gu (with minor edits from Prof. Lee): The correct implementation for this problem involves creating an output_list and adding elements to the output_list. Each element x in after_list is a string that can have one of 4 possible formats:
A common mistake we have seen is removing items as you go through the list, for example:
for x in after_list: if x == “”: after_list.remove(x) This would not work because when you remove items from a list you shift it an element ahead, which will cause the iteration to skip an element. For example, the following code x = [1, 2, 2, 3, 4] for i in x: if i == 2: x.remove(i) print(x) prints [1, 2, 3, 4] even though the intention is to print [1, 3, 4].
Another common mistake is assigning values to a variable in the loop and expecting it to modify the values in the underlying list. For example: after_list = [“hello”, “world”] for x in after_list: x = “new value” print(after_list) This would still print [“hello”, “world”] because the assignment to x of the string “new value” does not change anything in after_list. In the for-loop, x is a variable that is assigned a value taken from after_list. The assignment to x of “new value” only changes the value of that variable within that iteration of the loop, because at the next iteration, x is reset to be the next item in after_list.
A finer-grained explanation of the rubric items
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.
count_from: the number you're counting down from [int] count_by: the amount you're counting down by [int > 0]
Examples:
coundown_by_n(16, 5) should print: 16 11 6 1
coundown_by_n(21, 7) should print: 21 14 7 0
"""
Solution:
Note that count from can start negative. while (count_from >= 0): print(count_from) count_from -= count_by
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?
Consider the following header and speci cation of a non-method function. def requires(c, other_label): """Returns True if Course with label other_label must be taken before c, False otherwise.
Pre: c is a Course. other_label is a non-empty string."""
Example intended operation: suppose c1 is a Course with label 'CS1110' and empty prereqs list; c2 is a Course with label 'CS2110' and prereqs list [c1]; c3 is a Course with label 'CS2800' and prereqs list [c1]; c4 is a Course with label 'CS3110' and prereqs list [c2, c3]
Then, all of the following should evaluate to True: And all of the following should evaluate to False: requires(c4, 'CS2800') requires(c4, 'CS2110') requires(c4, 'CS1110')
requires(c1, 'CS2800') requires(c3, 'CS2800') requires(c4, 'randomstring')
While a majority of the lines are correct, there is at least one error in the proposed implemen- tation below. For each error, circle it, write down/explain the correct version, and draw a line between the circle and the corresponding correction. Responses where the correction is wrong may not receive any credit.
Solution: