CS 5 “Black” Assignment 10 on the web! – Lab and reading due at normal times (this Sunday at 11:59 PM) – All the rest is due the following Thursday (Nov. 20) at 11:59 PM – Some choices! Markov text generation 3D video game Lempel-Ziv (+20 points!) Connect 4 AI (+20 points!) Exam 2 – Monday, November 17 – Covers everything through this Wednesday’s lectures – Emphasis on material since last exam, but recursion is fair game! – One 8.5 x 11 sheet (front and back) permitted – If you are asked to program in Hmmm, we’ll provide you the full instruction set – Practice problems now on the CS 5 website Yay! Who are you!?
Got ? Send to subscribe cs5-1-l
return vs print def foo1(): print 42 def foo2() return 42 def bar() x = foo1() y = foo2() x += 1 y += 42 This is good foo for thought! que es eso?
True Story
k th Order Markov Processes Andrey Markov Training File: “I like spam. I like toast and spam. I eat ben and jerry’s ice cream too.” First order Markov Dictionary: I : like, like, eat like : spam, toast spam. : I, I and : spam, jerry’s
k th Order Markov Processes Andrey Markov Training File: “I like spam. I like toast and spam. I eat ben and jerry’s ice cream too.” First order Markov Dictionary: I : like, like, eat like : spam, toast spam. : I, I and : spam, jerry’s MORE ENTRIES… Generating “random” text: “I like spam. I like spam.” “I eat ben and spam. I like toast and jerry's ice cream too."
k th Order Markov Processes Andrey Markov Training File: “I like spam. I like toast and spam. I eat ben and jerry’s ice cream too.” First order Markov Dictionary: I : like, like, eat like : spam, toast spam. : I, I and : spam, jerry’s MORE ENTRIES… Second order Markov Dictionary: I like : spam., toast like spam. : I spam. I : like, eat
k th Order Markov Processes Andrey Markov Training File: Wikipedia essay on Huffman Compression First order Markov sentences generated… “Huffman was a source symbol.” “Huffman became a known as a character in a particular symbol frequencies agree with those used for each possible value of Engineering.”
k th Order Markov Processes Training File: Wikipedia essay on Huffman Compression Second order Markov sentences generated… “Huffman coding is such a code is not produced by Huffman's algorithm.” “Huffman was able to design the most common characters using shorter strings of bits than are used for lossless data compression.”
Thinking Rationally class Rational: def _ _init_ _(self, num, denom): self.numerator = num if denom == 0: print “Invalid Denominator!” else: self.denominator = denom def isZero(self): return self.numerator == 0 >>> myNum1 = Rational(1, 3) >>> myNum2 = Rational(0, 6) Why is this code so self ish? numerator = 1 denominator = 3 numerator = 0 denominator = 6 myNum1 myNum2 The “constructor” Rational is a class and MyNum1 and MyNum2 are “objects” or “instances” of the Rational class
Thinking Rationally class Rational: def _ _init_ _(self, num, denom): self.numerator = num if denom == 0: print “Invalid Denominator!” else: self.denominator = denom def isZero(self): return self.numerator == 0 >>> myNum1 = Rational(1, 3) >>> myNum2 = Rational(0, 6) >>> myNum1.isZero() False >>> myNum2.isZero() Why is this code so self ish? numerator = 1 denominator = 3 numerator = 0 denominator = 6 myNum1 myNum2 The “constructor”
Thinking Rationally class Rational: def _ _init_ _(self, num, denom): self.numerator = num self.denominator = denom def _ _repr_ _ (self): return str(self.numerator) + “/” + str(self.denominator) >>> myNum1 = Rational(1, 3) >>> myNum1 1/3 >>> print “This is my number:”+str(myNum1) equivalent to myNum1._ _repr()_ _
Thinking Rationally class Rational: def _ _init_ _(self, num, denom): self.numerator = num self.denominator = denom def equals(self, OtherNum): return self.numerator * OtherNum.denominator == self.denominator * OtherNum.numerator >>> myNum1 = Rational(1, 3) >>> myNum2 = Rational(2, 6) >>> myNum1.equals(myNum2) True Cross multiplication!
Thinking Rationally class Rational: def _ _init_ _(self, num, denom): self.numerator = num self.denominator = denom def _ _eq_ _(self, OtherNum): return self.numerator * OtherNum.denominator == self.denominator * OtherNum.numerator >>> myNum1 = Rational(1, 3) >>> myNum2 = Rational(2, 6) >>> myNum1 == myNum2 True myNum1._ _eq_ _(myNum2)
Overloaded Operator Naming + __add__ - __sub__ * __mul__ / __div__ // __floordiv__ % __mod__ ** __pow__ + __pos__ - __neg__ __abs__ __int__ __float__ __complex__ == __eq__ != __ne__ <= __le__ >= __ge__ < __lt__ > __gt__ >>> myNum = Rational(9, 2) >>> myNum.int() Barf! >>> int(myNum) 4 def __int__(self): return self.numerator/self.denominator Very __int__eresting!
Rationals Revisited class Rational: def _ _init_ _(self, num, denom): self.numerator = num self.denominator = denom def _ _repr_ _ (self): return str(self.numerator) + “/” + str(self.denominator) def _ _eq_ _(self, other): return self.numerator * other.denominator == self.denominator * other.numerator def _ _add_ _(self, other): ```Returns a new Rational number that is the sum of self and other’’’ Worksheet >>> myNum1 = Rational(1, 3) >>> myNum2 = Rational(3, 5) >>> myNum3 = myNum1 + myNum2 >>> myNum3 14/15
Rationals Revisited class Rational: def _ _init_ _(self, num, denom): self.numerator = num self.denominator = denom def _ _add_ _(self, other): newDenominator = self.denominator * other.denominator newNumerator = self.numerator*other.denominator + other.numerator*self.denominator return Rational(newNumerator, newDenominator) >>> myNum1 = Rational(1, 3) >>> myNum2 = Rational(3, 5) >>> myNum3 = myNum1 + myNum2 >>> myNum3 14/15
A Vector Class >>> tuple1 = (1, 0, 2) >>> tuple2 = (1, 0, 2) >>> tuple1 + tuple2 (1, 0, 2, 1, 0, 2) >>> u = Vector(tuple1) >>> v = Vector(tuple2) >>> u (1, 0, 2) >>> u == v True >>> u+v (2, 0, 4) >>> u*v 5 Worksheet Dot product!
A Vector Class class Vector: def __init__(self, inputTuple): self.x = inputTuple[0] self.y = inputTuple[1] self.z = inputTuple[2] def __eq__(self, other): return self.x == other.x and self.y == other.y and self.z == other.z def __add__(self, other): return Vector((self.x + other.x, self.y + other.y, self.z + other.z)) def __repr__(self): return "("+str(self.x)+","+str(self.y)+","+str(self.z)+")" def __mul__(self, other): return (self.x * other.x) + (self.y * other.y) + (self.z * other.z)
A Self-Counting Class class Person: population = 0 def __init__(self, InputName, ID): self.name = InputName self.ID = ID population = population+1 >>> Person.population 0 >>> P1 = Person(“Matt”, 42) >>> P2 = Person(“Sarah”, 27) >>> P1.ID 42 >>> Person.population 2
Named Arguments def foo(ID, Name=“Student”, Food=“Spam”): foo(42) foo(43, “Nick”) foo(44, “Matt”, “Pop Tarts”) foo(Food = “Pizza”, ID = 45) foo(Name = “Meera”, Food = “M&Ms”, ID=46) foo(Food = “Spam”, Name =“Kylie”) This is cool!
3D Graphics with vPython! >>> from visual import * >>> mybox1 = box(pos=(0,0,0),length=1,height=1, width=1,color=(1,0,0)) >>> mybox2 = box(pos=(3, 0, 0),length=1,height=1, width=1,color=(0,1,0)) Draws the box (cube) and returns a reference to that box! What does that reference look like and what can we do with it? Notice the named arguments!
3D Graphics with vPython! >>> from visual import * >>> mybox1 = box(pos=(0,0,0),length=1,height=1, width=1,color=(1,0,0)) THIS IS A NAMED ARGUMENT - SETTING A VECTOR that is the center of the box! What does that reference look like and what can we do with it? Demo!
Rotating Cube from visual import * # imports the graphics module! def spinbox(): myBox = box(pos=(0,0,0),length=1,height=1,width=1,color=(1,0,0)) while True: scene.autoscale = False # Turn off weird autozoom rate(30) # Limit the animation rate to 30 fps myBox.rotate(angle=pi/100,axis=(1,0,0),origin=(0,0,0)) Demo! spinbox.py
from visual import * import random def spinboxes(): boxList = [] for boxNumber in range(0, 10): x = random.randint(-5, 5) y = random.randint(-5, 5) z = random.randint(-5, 5) red = random.random() # random number between 0 and 1 green = random.random()# random number between 0 and 1 blue = random.random() # random number between 0 and 1 newBox = box(pos=(x, y, z), length=1, height=1, width=1, color=(red, green, blue)) boxList.append(newBox) while True: for myBox in boxList: scene.autoscale = False # Turn off weird autozoom myBox.rotate(angle=pi/100, axis=(1, 0, 0), origin=(0, 0, 0)) Lots of Rotating Cubes! Demo! spinboxes.py What is this program doing?
Iteration + Recursion = ;^) fractalCube(level, size, x, y, z) fractalCube(0, 9, 0, 0, 0) fractalCube(1, 9, 0, 0, 0) fractalCube(2, 9, 0, 0, 0) size = Notice how the centers are offset!
Iteration + Recursion = ;^) def fractalCube(level, size, x, y, z): ```Returns a list of boxes comprising the fractal cube of given size centered at x, y, z’’’ if level == 0: return ??? else: No more than 7 lines of code here! box(pos=(0,0,0), length=1, height=1, width=1, color=(1, 0, 0) )
Iteration + Recursion = ;^) def fractalCube(level, size, x, y, z): ```Returns a list of boxes comprising the fractal cube of given size centered at x, y, z’’’ if level == 0: return [box(pos=(x,y,z), length=size, height=size, …)] else: newsize = size/3.0 cubeList = [] for newx in [x-newsize, x+newsize]: for newy in [y-newsize, y+newsize]: for newz in [z-newsize, z+newsize]: cubeList = cubeList + fractalCube(level-1,newsize, newx, newy, newz) return cubeList Demo! fractalcube.py
Rotate those cubes! myCubes = fractalcubes(3, 50, 0, 0, 0) while True: for cube in myCubes: scene.autoscale = False # No weird autozoom cube.rotate(angle=pi/100, axis=(1,0,0), origin=(0,0,0)) myCubes = fractalcubes(3, 50, 0, 0, 0) while True: for cube in myCubes: scene.autoscale = False # No weird autozoom cube.rotate(angle=pi/100, axis=(1,0,0), origin=cube.pos)) Demo! rotate.py
Jiggling cubes! import random myCubes = fractalcubes(3, 50, 0, 0, 0) while True: for cube in myCubes: scene.autoscale = False # No weird autozoom cube.pos = cube.pos + (random.random(),random.random(),random.random()) Returns a random number between 0 and 1 Will this jiggle?
Jiggling cubes! import random myCubes = fractalcubes(3, 50, 0, 0, 0) while True: for cube in myCubes: scene.autoscale = False # No weird autozoom cube.pos = cube.pos + (random.random()-0.5, random.random()-0.5, random.random()-0.5) I gotta see this! Demo! jiggles.py
Classes with Class! >>> myBlob = blob(0, 0, 0) >>> myBlob.move(0.1, 0, 0) class blob: def __init__(self, x, y, z): self.sphere1 = ellipsoid(pos=(x-0.25,y,z)) self.sphere2 = ellipsoid(pos=(x+0.25,y,z)) self.sphere3 = ellipsoid(pos=(x,y+0.25,z)) self.spheres=[self.sphere1, self.sphere2, self.sphere3] def move(shiftx, shifty, shiftz): worksheet
Classes with Class! >>> myBlob = blob(0, 0, 0) >>> myBlob.move(0.1, 0, 0) class blob: def __init__(self, x, y, z): self.sphere1 = ellipsoid(pos=(x-0.25,y,z)) self.sphere2 = ellipsoid(pos=(x+0.25,y,z)) self.sphere3 = ellipsoid(pos=(x,y+0.25,z)) self.spheres=[self.sphere1, self.sphere2, self.sphere3] def move(shiftx, shifty, shiftz): for sphere in self.spheres: sphere.pos += (shiftx, shifty, shiftz)
Balls and Walls from visual import * def main(): redball = sphere(pos = (0, 0, 0), radius = 1, color=color.red) rightwall = box(pos = (5, 0, 0), length = 1, height = 10, width = …) leftwall = box(pos = (-5, 0, 0), length = 1, height = 10, width = …) travel = vector(0.05, 0, 0) # vector, victor! wallshift = vector(0, 0, 0.2) # roger, roger! while True: scene.autoscale = False rate(60) if scene.kb.keys: # key pressed? key = scene.kb.getkey() # get the key that was pressed! if key == "f": travel = -1*travel if key == "d": rightwall.pos = rightwall.pos + wallshift if key == "s": rightwall.pos = rightwall.pos - wallshift if (redball.pos.x > 3.75 or redball.pos.x < -3.75): travel = -1*travel # NEGATING A VECTOR! redball.pos = redball.pos+travel Demo! bounce.py
More on vPython Also keyboard and mouse control functions and much, much more!
On the homework… Lab 1 Reading Connect4 Board One of… –Markov text generation –Your own video game – Lempel-Ziv (+20 points!) – Connect 4 Player with AI (+20 points!) You should understand the ideas behind doing each of these, even if you don ’ t actually program them.
Worksheet Challenge! Define Foo and Bar such that python has the following behavior… >>> Foo[0] == Bar True >>> Bar[0] == Foo True >>> Foo[0][0][0][0][0] == Foo True That ’ s TOO weird!!