################# ## Reading Code # ################# ################################################# # DO NOT TYPE READING PROBLEMS INTO PYTHON!!! ### ################################################# print("*****************\n\nREADING PROBLEM 1") def G(x, b): if x == b: return 0 return 1 + G(x+1,b) print("G({},{}) = {}".format(3,100, G(3,100))) print("G({},{}) = {}".format(100,100, G(100,100))) print("G({},{}) = {}".format(98,100, G(98,100))) #print("G({},{}) = {}".format(105,100, G(100,100))) print("*****************\n\nREADING PROBLEM 2") def H(x): if x < 0: r = -1 else: r = 10 * H(x - 2) return r print("H({}) = {}".format(0, H(0))) print("H({}) = {}".format(-3, H(-3))) print("H({}) = {}".format(4, H(4))) print("H({}) = {}".format(5, H(5))) print("H({}) = {}".format(20, H(20))) print("*****************\n\nREADING PROBLEM 3") # This program ALMOST returns the sum of the cubes between # a and b (inclusive). Fill in the base case(s) appropriately. def F(a,b): return a**3 + F(a+1, b-1) + b**3 #print("F({},{}) = {}".format(2, 5, F(2,5))) print("*****************\n\nREADING PROBLEM 4") class MyList(list): def count(self): return list.count(self, 1) def append(self, x): if x != 0: list.append(self, x) M = MyList() L = list() M.append(1) L.append(1) M.append(0) L.append(0) M.append(1) L.append(1) M.append(4) L.append(4) print(M.count()) print(L.count(1)) print(M) print(L) print("*****************\n\nREADING PROBLEM 5") # Class used in next few problems, and below class Record: def __init__(self, name, age): self.name = name.lower().capitalize() if age <= 18: self.status = 'minor' elif age <= 65: self.status = 'adult' else: self.status = 'old' def getName(self): return self.name def getStatus(self): return self.status def step(self): if self.status == 'minor': self.status = 'adult' elif self.status == 'adult': self.status = 'old' def __str__(self): return self.name + ": " + self.status R = Record('rYaN', 57) T = Record('TaSHA', 8) print(R) print(T) R.step() T.step() print(R) print(T) R.step() T.step() print(R) print(T) print("*****************\n\nREADING PROBLEM 6") # Class used here and below class NYC_Record(Record): bList = ['Bronx', 'Brooklyn', 'Manhattan', 'Queens', 'Staten Island'] def __init__(self, name, age, borough): Record.__init__(self, name, age) borough = borough.lower().capitalize() k = 0 ln = len(NYC_Record.bList) while k < ln: if borough == NYC_Record.bList[k]: self.bor = k break k = k + 1 if k == ln: self.bor = 0 def move(self): self.bor = (self.bor + 1) % 5 def getBor(self): return NYC_Record.bList[self.bor] def __str__(self): initStr = Record.__str__(self) return initStr + " from " + self.getBor() A = NYC_Record('Abe', 45, 'queens') B = NYC_Record('Bob', 75, 'jersey') print(A) print(B) print('\n') A.move() B.move() A.step() B.step() print(A) print(B) print('\n') A.move() B.move() A.step() B.step() print(A) print(B) print("*****************\n\nREADING PROBLEM 7") class XX: def __init__(self, a, b): self.x = a self.y = b def increment(self): self.x = self.x + 10 self.y = self.y + 10 def display(self): print(self.x, self.y) A = XX(3,4) B = XX(13,14) C = A A.increment() A.display() B.display() C.display() print(A == B) print(A == C) print(B == C) ################ # Writing Code # ################ ''' PROGRAM 1 Write a recursive function that takes a positive integer n as input. The function outputs the sum of the cubes of 1, 2, ..., to n. For example, on input 3, it should output 32. The function may not use: Loops, lists, sets, or dictionaries. Write it so that it does some error checking: While it can take any number as input, for any number besides a positive integer, it returns 0. ''' ''' PROGRAM 2 Do the last program again, but now take two inputs a and b, returning the sum of the cubes inbetween a and b (including a and b). For example, on inputs -2 and 3, return 27. Consider various ways to do this ... ''' ''' PROGRAM 3 Write a recursive function that takes a string as input and returns the number of vowels. There is a good way and a bad way! (like with Binary Search) ''' ''' PROGRAM 3.5 Write a recursive function that takes a string as input and returns True if the string alternates '0' then '1', and False otherwise. For example F('0101010') return True, while F('010110') returns False. No loops allowed. ''' ''' PROGRAM 4 Use Tkinter to create a button with the word "ON" written in it. It toggles between "ON" and "OFF" when the button is clicked. ''' ''' PROGRAM 5 Use Tkinter to create an Entry widget, which when clicked on prints its contents to the interpreter console. ''' ''' PROGRAM 6 Use Tkinter to have window open with a disk, which when clicked on prints "Clicked!" to the console. ''' ''' PROGRAM 7 Use Tkinter to create two Entry widgets and a Submit button. The user can enter two words in the Entries and then hit submit. If the words are the same (capitizalization irrelevant), then the GUI writes 'Good', and otherwise 'Bad' ''' ''' PROGRAM 8 Write a server-client where the client can send a word, and the server sends back how many vowels are in the word. ''' ''' PROGRAM 9 Write the definition of a class "xx" which is a child of the class XX above. The class xx should have the following properties: - It should override the increment method so that each variable is incremented by 5 instead of 10. - Also write a new method decrement such that if we apply increment and then decrement, the internal values will be unchanged. Draw a UML diagram of the classes XX and xx, with an appropriate arrow. ''' ''' PROGRAM 10 Consider the class Record above. Write a program that repeatedly prompts the user for a pair of inputs: name and age. For each pair of input it creates a Record. Create a set with a Record object for each pair. Question: What happens if two records are created with the same initial date, say (`Sean', 36) and ('Sean, 36)? (i.e. will the set have two record objects or one, in the set? ''' ''' PROGRAM 11 Do the last question again using dictionaries. ''' ''' PROGRAM 12 Write a class definition VowelList which models the list of English vowels: A, E, I, O, U. The class keeps track of the ``current letter'', which will be one of the 5 vowels in the list. The following are the exact requirements for the class: -When initialized it takes no inputs, but initializes the current letter to be A. -It has the method get: Takes no inputs and returns the current vowel. -It has the method shift: Takes no inputs. Changes the current letter to be one letter to the left (in the alphabetic order: A, E, I, O, U). If the current letter is A, then the new current letter becomes U. -It has the method reset: Takes no inputs. Makes the current letter A again. As an example, consider the following code; once the class is written, it should print out as indicated by the comments. V = VowelList() print(V.get()) # Should print: A V.shift() print(V.get()) # Should print: U V.shift() print(V.get()) # Should print: O V.reset() print(V.get()) # Should print: A ''' ''' PROGRAM 13 Write a tkinter program that creates a Button with a 5 written on it. Every time the button is left-clicked the number decreases by 1. The number decreases with each click, till it gets to 0. Once at 0, clicking on the Button does not change the value. Import tkinter for this program, but do not important anything else. ''' ''' PROGRAM 14 Consider the following partially written code for the two classes Cls1 and Cls2. class Cls1: def __init__(self, a): self.a = a # Put your code here ... class Cls2(Cls1): def __init__(self, a, b): Cls1.__init__(self, a) self.b = b # Put your code here ... You will add methods to these classes so that you get the desired operation. You may not add any internal data (i.e. you can only use self.a and self.b). You may only add the following 2 methods: getValue and decrement, with the following requirements: - You can add a getValue method to one or both of Cls1 and Cls2. - You can add a decrement method to exactly one of the classes (but not both). After adding those methods, in the following code, each print statement should print what is in the comments that follow it. c1 = Cls1(12) print(c1.getValue()) # should print: 12 c2 = Cls2(8,4) print(c2.getValue()) # should print: 8 4 c1.decrement(2) c2.decrement(2) print(c1.getValue()) # should print: 10 print(c2.getValue()) # should print: 6 4 c1.decrement(1) c2.decrement(3) print(c1.getValue()) # should print: 9 print(c2.getValue()) # should print: 3 4 '''