################# ## Reading Code # ################# print("*****************\n\nREADING PROBLEM 1") # What does the following code print def F(L): if L[2] > 7: L[2] = 7 else: L[2] = 0 return L[0] def G(x): x = x + 10 if x < 20: return x else: return 20 S = [4,5,9,5] x = 600 print("F return:", F(S)) print("G return:",G(x)) print("x =",x," and S =",S) print("Composition:", G(F(S))) print("x =",x," and S =",S) print("*****************\n\nREADING PROBLEM 2") L = [-3,-1]*1000 D = {} S = set() for x in L: S.add(x) if not x in D: D[x] = -1 else: D[x] = D[x] - 1 print(L[998]) print(D) print(S) print("*****************\n\nREADING PROBLEM 3") class Silly: def __init__(self,x, X): self.v = x self.w = x + 2*X def __str__(self): return "v = " + str(self.v) + "; and w = " + str(self.w) def change(self,a,B): self.v = 2*a self.w = self.v + B s = [] for (a,b) in [(2,4),(3,3),(0,1)]: next = Silly(a,b) s.append(next) def printList(L): for l in L: print(l) printList(s) s[1].change(1, 3) s[2].change(0.5, 0) printList(s) print("*****************\n\nREADING PROBLEM 4") 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 __add__(self, other): return XX(max(self.x, other.x), min(self.y,other.y)) def __str__(self): return "x: {} , y: {}".format(self.x, self.y) A = XX(3,4) B = XX(13,14) C = A D = C + B A.increment() print(A) print(B) print(C) print(D) print(A == B) print(A == C) print(B == C) print("NOW DO ADD A METHOD FOR EQUALITY CHECKING") print("*****************\n\nREADING PROBLEM 5") # USES CLASS XX ABOVE. def F(num, s, lt, cl): num = num + 20 s = 'A NEW String' lt[2] = num num = 0 cl.increment() return num x = 50 s = 'A string' myList = ['one', 8, 'two', 'three', 100] myClass = XX(100, 200) z = F(x, s, myList, myClass) print("x:",x) print("z:",z) print("s:",s) print("myList:", myList) print("myClass: ", end = '') print(myClass) print("*****************\n\nREADING PROBLEM 6") # USES CLASS XX ABOVE. def F(num, s, lt, cl): num = num + 20 s = 'A NEW String' lt = [3,4,5] cl = XX(100, 50) lt[0] = 'NEW' cl.increment() return num x = 50 s = 'A string' myList = ['one', 8, 'two', 'three', 100] myClass = XX(100, 200) z = F(x, s, myList, myClass) print("x:",x) print("z:",z) print("s:",s) print("myList:", myList) print("myClass: ", end = '') print(myClass) print("*****************\n\nREADING PROBLEM 7") # 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 8") # 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) ################ # Writing Code # ################ # 1 # Write a program that prompts the user # for one numerical inputs (r) # and prints out the EXACT and APPROXIMATE area # of the circle with radius r. For the exact, # use the string "pi" to represent the number pi. # 2 # Do the last program again, but do it so # that it complains if the radius is not # a valid number, and keeps asking the user # for input till a valid number is input. # 3 # Write a program that takes input from the # user until "Enter" is hit with no number. # The program outputs the value of the # largest and smallest number inputed. # 4 # Write a program that takes input from the # user until "Enter" is hit input # Assume each input is an integer and at least one # input is zero. # Output the following statistics: # - How many negative numbers? # - How many zeros? # - How many positive numbers? # Note: Do this using list methods and NO loops. # 5 # Write a program (use for-loops) that creates a list # of 26 tuples, where each tuple is # the pair (i,j) where "i" is a number # between 1 and 26 (inclusive) and # "j" is letter i in the alphabet, i.e. # it should be (1,'a'), (2, 'b'), and so on. # 6 # Write a program that takes a sentence # as user input and then prints out the # number of 4-letter words in the sentence. # 7 # Write a class for a classroom object # which keeps track of: # - the teacher # - all students enrolled in the course # - which students are present # - which students are absent # It should have methods for modifying the data and # aquiring the data. ''' PROGRAM 8 Write a function ListDict which takes a list of integers as input. It returns a dictionary in which there is a key for each item in the list. The corresponding value is the number of times that integer appears in the list, unless it appears more than 3 times, in which case its value should just be the sting "MANY". Example: ListDict([3,4,3,4,5,4,4,4]) should return the dictionary: { 3:2, 4:'MANY', 5:1 } ''' ''' PROGRAM 9 Write the definition of a class "Positive" with the following operation: - When constructed it takes no arguments. - There is one method "add" which accepts one integer argument. - There is one method "display" which prints out all the numbers inputed with the add method which were positive, printing each number only once. '''