Software Design Spring 2008 For today you should have: 1) read Chapter 16 2) prepared for a quiz For next time: 1) read Chapter 17 2) work on Homework 6 Exam 1 results -------------- Total points = 42 35+ 10 Here is the distribution of scores. 30-34 8 Not bad overall, but I think the exam was 25-29 9 a little too hard for where we are. 20-24 3 That's ok -- I will treat it as a learning 20- 5 exam more than an evaluative exam. Page 1: sequences, immutable, check out, update, search, recursive, base case, frame, parameters, comment. Page 2: Cartalk puzzler Three important things to keep in mind: 1) draw a picture 2) draw a picture 3) draw a &$#(*! picture Also, pencil? Two ways you might approach a problem like this: Plan A: bottom up, with a dose of generalization Start with def is_palindrome(s): return s[::-1] == s Then write a more general version: def is_palindrome(i, start, len): s = str(i)[start:start+len] return s[::-1] == s And then check the Puzzler conditions: def check(i): return (is_palindrome(i, 2, 4) and is_palindrome(i+1, 1, 5) and is_palindrome(i+2, 1, 4) and is_palindrome(i+3, 0, 6)) for i in range(100000, 999999-3+1): if check(i): print i Plan B: top down, also known as wishful thinking Write check() first, inventing helper functions as you go, then implement the helper functions. Page 3: def partition(ts): d = {} for t in ts: c = t.color if c in d: d[c].append(t) else: d[c] = [t] return d OR def partition(ts): d = {} for t in ts: d[t] = t.color return invert_dict(d) # see Section 11.4 Page 4: dict{} is a syntax error word.strip().lower() has no net effect (semantic) read_wordlist doesn't return anything (semantic or runtime) in check_rotation, rotate_word should be rotate_words (runtime) in check_rotations a) return value from check_rotation is unused (semantic), OR b) rot is used before assigned (runtime) the filename words.txt should be in quotes (syntax or runtime) In some cases, the classification is ambiguous. For example, the nature of the error might be semantic, but it might eventually cause a runtime error. In those cases I accepted either answer. Classes ------- Big dose of new vocabulary: class, instance, instantiate, object, constructor, attribute LEARN IT, SPEAK IT, MAKE IT A PART OF YOU. A class is a user-defined type. class Point: pass # later there will be method definitions here class Rectangle: pass Creating a class defines a function* with the same name: blank = Point() schmectangle = Rectangle() These functions are called constructors. Invoking a constuctor creates an instance of the class. In casual speech, object and instance are used interchangeably. Attributes ---------- Objects have attributes (emphasis on the first syllable) An attribute is like a variable that belongs to an object. Try this: from TurtleWorld import * world = TurtleWorld() bob = Turtle(world) print bob.x, bob.y print bob.color bob.color = 'blue' print bob.color What color is bob? You can add a new attribute to an object at any time! bob.new_feature = 'whatever you want' print bob.new_feature UML object diagrams ------------------- Download http://wb/sd/code/rectangle_lumpy.py Draw a stack diagram, then run it to see if you're right. import Lumpy lumpy = Lumpy.Lumpy() lumpy.make_reference() class Rectangle: pass class Point: pass def find_center(box): corner = box.corner p = Point() p.x = box.corner.x + box.width/2 p.y = box.corner.y + box.height/2 lumpy.object_diagram() return p box = Rectangle() box.width = 100 box.height = 200 box.corner = Point() box.corner.x = 10 box.corner.y = 20 center = find_center(box)