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.

Slides:



Advertisements
Similar presentations
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Advertisements

Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
XSL November 4, Unit 6. Default sorting is based on text However, we can also sort on numbers, more successfully than last class We use the data-type.
Classes 2 COMPSCI 105 SS 2015 Principles of Computer Science.
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Object-Oriented Programming in Python Goldwasser and Letscher Chapter 6 Defining Our Own Classes Terry Scott University of Northern Colorado 2007 Prentice.
Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
April 7, 2009 While we teach, we learn. ~Seneca. April 7, 2009 Bring Class Notes on Thursday, 4/9 Test 3  Thursday, 4/16 Covers: Text sections 4.1, 4.2,
Doing The Drafting Thing IE 1225 R. Lindeke. See next slide for variations from text! Lets begin with 1-2 “Box Part” – with some Variants & Dimensions!
 2002 Prentice Hall. All rights reserved. 1 Chapter 8 – Customizing Classes Outline 8.1 Introduction 8.2 Customizing String Representation: Method __str__.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
ABC’s of PowerPoint (Office 2007) Part 1: Basic Vocabulary Part 2: Cursors Part 3: Insert Your Text Part 4: Insert Your Pictures Part 5: Basic Tools &
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Games and Simulations O-O Programming in Java The Walker School
Guide to Programming with Python
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due April 6 at 11:55pm (last day of classes) Next tutorial is on April 6 (Monday)
SE 313 – Computer Graphics Lecture 3: Analytical Geometry and Linear Algebra Lecturer: Gazihan Alankuş Please look at the last three slides for assignments.
Classes 3 COMPSCI 105 S Principles of Computer Science.
Python Programming Chapter 14: Classes and Methods Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
A whole new class of programming CS 5 today HW10: Due Sun, Nov 15 Pr0: Ariane 5 Reading Pr1/Lab:the Date class Pr2 Connect4Board Pr3 Connect4Player (extra.
Data Abstraction UW CSE 140 Winter What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
1. MORE TRIG (AND BASIC VECTORS) 2. INHERITANCE (CHAPTER 9) Lecture 10 (Last one!)
Now String theory To Do: Lists This stuff hurts my brane. when you learn string theory from google images… Goal: Thinking like a machine You should now.
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 2) More complex procedures using parameters,
CS61A Lecture 15 Object-Oriented Programming, Mutable Data Structures Jom Magrotker UC Berkeley EECS July 12, 2012.
60 Questions (review for final exam) CSC 161: The Art of Programming Prof. Henry Kautz 12/7/
By the end of this session you should be able to...
Computer Science 112 Fundamentals of Programming II Graphics Programming.
If statements while loop for loop
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Classes 1 COMPSCI 105 S Principles of Computer Science.
More On Classes UW CSE 160 Spring Classes define objects What are objects we've seen? 2.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
EECS 110 Projects 1&2 Juan Li – Project 1 Ning Xia – Project 2.
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
Lists, Tuples, Dictionaries, … + lots of computer work for the programmer's work! T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer'
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
The CS 5 Herald Goodwill Gesture Goes Awry Claremont (AP) : Seven rooms were damaged in a Harvey Mudd College dormitory Tuesday evening after a misguided.
Comparing & Ordering Fractions. Please do not be these people!
Creating visual interfaces in python
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Ionut Trestian Northwestern University
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
Lossless Decomposition and Huffman Codes Sophia Soohoo CS 157B.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
EECS 110: Lec 13: Dictionaries Aleksandar Kuzmanovic Northwestern University
Data Abstraction UW CSE 160 Spring What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Compression and Huffman Coding. Compression Reducing the memory required to store some information. Lossless compression vs lossy compression Lossless.
Event Binding Make something to react when something happens to it, like pressing a key, it’s called event binding. Events: things that occur while a program.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Sprites (Images) and Sounds
COMPSCI 107 Computer Science Fundamentals
George Mason University
Classes and Objects – digging deeper
COMPSCI 107 Computer Science Fundamentals
Week 8 Classes and Objects
Mathematical operators overload
CS-104 Final Exam Review Victor Norman.
Tutorial Lecture for EE562 Artificial Intelligence for Engineers
The CS 5 Times-Picayune Claremont Penguin Takes Olympics Gold
This week's classes Connect 4 aiMove
This week's classes Connect 4 Player
The CS 5 Black Post Penguins Invade Dormitory
Chapter 10 Algorithms.
CSE 8A Lecture 6 Reading for next class:
Ionut Trestian Northwestern University
Northwestern University
Terry Scott University of Northern Colorado 2007 Prentice Hall
Presentation transcript:

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!!