CMU 15-112 Fall 2017: Fundamentals of Programming and Computer Science
Homework 11 (Due Friday, 10-Nov, at 10pm)
- This hw is SOLO. See the syllabus for details.
- No starter code this week!
- You should make use of this week's linter: cs112_f17_week11_linter.py
- When you are ready, submit hw11.py to autolab. This week you may use up to 5 submissions. Only your last submission will be graded.
- Please note that this homework is due on Friday, not Saturday. This is to avoid conflicting the assignment with Hack-112. In exchange, the homework should take less time than usual!
- Do not hardcode the test cases in your solutions.
- Attend a TA-Led Optional Lecture [20 pts]
Some of you may want to use modules or algorithms outside of what we've learned in class on the term project. In support of this, you should attend at least one of the hour-long TA-led optional lectures listed below to get 20 points on the assignment. Even if you do not intend to use any of these modules or algorithms in your project, you should still attend a session; we would recommend Data Structures, APIs, and Dev Ops + Github as generally useful topics.
Mon 4:30pm PH A18B Multiplayer with Sockets Mon 5:30pm MM A14 Pygame Mon 6:30pm MM A14 Game AI Mon 7:30pm MM A14 Data Structures Mon 8:30pm DH A302 The Browser as a Canvas Tue 4:30pm GHC 4102 APIs Tue 5:30pm GHC 5222 Android Tue 6:30pm Wean 5312 Graph Theory Tue 7:30pm DH 2210 Machine Learning Tue 8:30pm MM A14 Audio Wed 4:30pm GHC 5222 Databases Wed 8:30pm DH 1212 Open CV Thu 4:30pm DH A302 Web Scraping Thu 6:30pm PH 100 3D Things Thu 8:30pm GHC 4307 Dev Ops + Github
- Gate class and subclasses [40 pts] [autograded]
Write the classes required to make the following test function work properly.import types def getLocalMethods(clss): # This is a helper function for the test function below. # It returns a sorted list of the names of the methods # defined in a class. result = [ ] for var in clss.__dict__: val = clss.__dict__[var] if (isinstance(val, types.FunctionType)): result.append(var) return sorted(result) def testGateClasses(): print("Testing Gate Classes... ", end="") # require methods be written in appropriate classes assert(getLocalMethods(Gate) == ['__init__', '__str__', 'numberOfInputs', 'setInput']) assert(getLocalMethods(AndGate) == ['getOutput']) assert(getLocalMethods(OrGate) == ['getOutput']) assert(getLocalMethods(NotGate) == ['getOutput', 'numberOfInputs']) # make a simple And gate and1 = AndGate() assert(type(and1) == AndGate) assert(isinstance(and1, Gate) == True) assert(and1.numberOfInputs() == 2) and1.setInput(0, True) and1.setInput(1, False) # Hint: to get the name of the class given an object obj, # you can do this: type(obj).__name__ # You might do this in the Gate.__str__ method... assert(str(and1) == "And(True,False)") assert(and1.getOutput() == False) and1.setInput(1, True) # now both inputs are True assert(and1.getOutput() == True) assert(str(and1) == "And(True,True)") # make a simple Or gate or1 = OrGate() assert(type(or1) == OrGate) assert(isinstance(or1, Gate) == True) assert(or1.numberOfInputs() == 2) or1.setInput(0, False) or1.setInput(1, False) assert(or1.getOutput() == False) assert(str(or1) == "Or(False,False)") or1.setInput(1, True) assert(or1.getOutput() == True) assert(str(or1) == "Or(False,True)") # make a simple Not gate not1 = NotGate() assert(type(not1) == NotGate) assert(isinstance(not1, Gate) == True) assert(not1.numberOfInputs() == 1) not1.setInput(0, False) assert(not1.getOutput() == True) assert(str(not1) == "Not(False)") not1.setInput(0, True) assert(not1.getOutput() == False) assert(str(not1) == "Not(True)") print("Passed!") testGateClasses() - ComplexNumber class [40 pts] [autograded]
Write the ComplexNumber class to make the following test function work properly. Do not use the builtin complex numbers in Python, but rather only use integers.def testComplexNumberClass(): print("Testing ComplexNumber class... ", end="") # Do not use the builtin complex numbers in Python! # Only use integers! c1 = ComplexNumber(1, 2) assert(str(c1) == "1+2i") assert(c1.realPart() == 1) assert(c1.imaginaryPart() == 2) c2 = ComplexNumber(3) assert(str(c2) == "3+0i") # default imaginary part is 0 assert(c2.realPart() == 3) assert(c2.imaginaryPart() == 0) c3 = ComplexNumber() assert(str(c3) == "0+0i") # default real part is also 0 assert(c3.realPart() == 0) assert(c3.imaginaryPart() == 0) # Here we see that the constructor for a ComplexNumber # can take another ComplexNumber, which it duplicates c4 = ComplexNumber(c1) assert(str(c4) == "1+2i") assert(c4.realPart() == 1) assert(c4.imaginaryPart() == 2) assert((c1 == c4) == True) assert((c1 == c2) == False) assert((c1 == "Yikes!") == False) # don't crash here assert((c2 == 3) == True) s = set() assert(c1 not in s) s.add(c1) assert(c1 in s) assert(c4 in s) assert(c2 not in s) assert(ComplexNumber.getZero() == 0) assert(isinstance(ComplexNumber.getZero(), ComplexNumber)) assert(ComplexNumber.getZero() == ComplexNumber()) # This next one is the tricky part -- there should be one and # only one instance of ComplexNumber that is ever returned # every time you call ComplexNumber.getZero(): assert(ComplexNumber.getZero() is ComplexNumber.getZero()) # Hint: you might want to store the singleton instance # of the zero in a class attribute (which you should # initialize to None in the class definition, and then # update the first time you call getZero()). print("Passed!") testComplexNumberClass()