''' CSI 32, Fall 2018 Review for midterm exam ''' ################# ## Reading Code # ################# ################################################# # DO NOT TYPE READING PROBLEMS INTO PYTHON!!! ### ################################################# print("*****************\n\nREADING PROBLEM 0") c = 0 for x in range(2,100): y = x - 10 while y <= 0: c = c + 1 y = y + 1 print("c:",c) print("*****************\n\nREADING PROBLEM 1") L = [3, 9, 8, 3] M = list(L) X = L X[-1] = "X" M[-1] = "M" print("L: ", L) print("M: ", M) print("X: ", X) print("*****************\n\nREADING PROBLEM 2") 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 3") # 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 4") # 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 5") 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") # Draw a UML for the following classes. class A: def __init__(self): self.a = 10.8 def m1(self): return "Hello" class B: def __init__(self): self.b = 5 self.s = "BCC" self.X = A() self.Y = C() def m1(self): return self.b class C: def __init__(self): self.c = [1,3,5] def f(self): return self.c[0] def g(self, x): return x + self.f() ################ # Writing Code # ################ ''' PROGRAM 0 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 1 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. ''' ''' PROGRAM 2 Repeatedly take user input. Assume it is always an integer. It takes input till the user enters 0, then it will print out results as follows: - For each positive integer that was entered, it prints out the number of times it was inputed since its negation was entered. For example: If the user entered a hundred 2's then a single -2, followed by seven 2's, the printout should indicate seven for the number of 2's. ''' ''' PROGRAM 3 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 data, say (`Sean', 36) and ('Sean', 36)? (i.e. will the set have two record objects or one, in the set? ''' ''' PROGRAM 4 Do the last question again using dictionaries. ''' ''' PROGRAM 5 Write a program that repeatedly prompts the user for a pairs of inputs: name and age; till the user just inputs nothing. Then it prints out the following information: - For each status category (i.e. minor, adult, or old) it lists all the people in that category. - For each name, it indicates how many people have that name. ''' ''' PROGRAM 6 Write a program that takes input from the user until "Enter" is hit. Assume that each input is a word. Print out the number of words whose first and last letter are the same. ''' ''' PROGRAM 7 Write a function that takes a list of numbers as input and returns the following sum: The first number in the list, plus the first two numbers in the list, plus the first three numbers in the list, and so on till we add in all the numbers in the list. ''' ''' PROGRAM 8 Write a class definition Stg which will model a string whose characters can be modified. When initialized it takes no inputs and if printed, it would just be the empty string It has the following methods: add: Takes one string input (which you can assume is a single character); after this method is called, printing the object should print the same string with that character added at the end. change: takes two inputs- a string (single caharacter), and a number, and changes the character at the given index to the given character. get: takes one integer input and returns the the character at that position,and returns "Out of bounds" if not in bounds. ''' ''' PROGRAM 9 Below we define a class called Bit. Write 2 functions: flipBits: Takes one list as input, where each element of the list is a Bit. The function should return nothing, but change the given list, so that the value of each Bit object (i.e. its self.v) is changed from 0 to 1 or 1 to 0. printBitList: Takes one list as input, where each element of the list is a Bit. The function prints out the list of Bit object values as a single string, with a newline at the end. class Bit: def __init__(self, v = 0): self.v = v def flip(self): self.v = (self.v + 1) % 2 def __str__(self): return str(self.v) '''