Top Banner
CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? files dictionaries David Miller ! but there is an optional, ex. cr. circuit-building lab that day! hw9pr3! Looking ahead: No class W, 11/25/09 www.ibiblio.org/e-notes/Life/Game.htm Want more 3d Life?
51

CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Dec 30, 2015

Download

Documents

Laurence Gibson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

CS 5 Today

Today:

Midterm Exam #2M/T 11/16-17/09, in class

Life in 3d?

files dictionaries

David Miller !

but there is an optional, ex. cr. circuit-building lab that day!

hw9pr3!

Looking ahead:

No class W, 11/25/09

www.ibiblio.org/e-notes/Life/Game.htm

Want more 3d Life?

Page 2: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Visuospatial Skills

• We use visuospatial skills in everyday life.

• Most importantly, visuospatial skills are a significant predictor of success in technical fields. Imagine graphics software for instance.

• Power to predict grades in technical courses equals that of math SAT scores (Sorby, 2009).

• Bundled multimedia software and workbook developed at Michigan Tech teaches these skills.

• Multimedia is being implemented at 6 other institutions nationwide including Purdue, Virginia Tech, Penn State – Behrend using a $200,000 NSF grant awarded in 2007.

Three dimensions are better than two – I should

know!

Page 3: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Visuospatial skills can be taught and learned.

Improving these skills leads to improved grades

I have $5,000 to (1) give to you, (2) help you learn these skills, and (3)

improve grades…

Page 4: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

MandelGallery!

Page 5: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.
Page 6: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.
Page 7: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Infernalicious!

also: www.cs.hmc.edu/~jgrasel/

Page 8: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Files

>>> f = file( 'a.txt' )

>>> text = f.read()

>>> text'This is a file.\nLine 2\nLast line!\n'

>>> f.close()

In Python reading files is no problem…

opens the file and calls it f

reads the whole file and calls it f

use text.split() for a list of each raw word

closes the file (closing Python does the same)

use split as needed…for example, text.split( '\n' )

Page 9: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Files

>>> f = file( 'a.txt', 'w' )

>>> print >> f, "New stuff!"

>>> f.close()

In Python writing files is also concise…

opens the file f for writing…

outputs new stuff to the file f

closes the file to be sure it's been saved to

the filesystem…New stuff!

You'll need to close and re-open the file to see the changes…

Page 10: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Lists vs. DictionariesIf I had a dictionary, I guess I could look up

what it was!

Lists are not perfect…

LL[0] L[1]

reference

5 42

Page 11: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Lists vs. Dictionaries

Lists are not perfect…

L[1990] = 'horse'

You can't choose what to name data.

You have to start at 0.

L[0], L[1], …

L[1991] = 'ram'

LL[0] L[1]

reference

5 42

If I had a dictionary, I guess I could look up what it was!

Some operations can be slow for big lists …

if 'rooster' in L:

Page 12: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Dictionaries

In Python a dictionary is a set of key - value pairs.

It's a list where the index can be

any immutable key.

>>> d = {}

>>> d[1990] = 'horse'

>>> d[1991] = 'ram'

>>> d

{1990: 'horse', 1991: 'ram}

>>> d[1991]

'ram'

>>> d[1969]

key error

This seems like the key to dictionaries' value…

creates an empty dictionary, d

1990 is the key'horse' is the value

1991 is the key'ram' is the value

Retrieve data as with lists…

Almost !

Curly! And colony!

Page 13: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

More on dictionaries

Dictionaries have lots of built-in methods, or functions:

>>> d = {1990: 'horse', 1991: ram'}

>>> d.keys()

[ 1990, 1991 ]

>>> d.has_key( 1991 )

True

>>> d.has_key( 1969 )

False

>>> d.pop( 1990 )

'horse'

They don't seem moronic to me!

pop deletes a key and returns

its value

has_key checks if a key is present

keys returns a list of all keys

Page 14: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

There's madness in this method!Methods

Functions

d.has_key( 1991 )

are functions that are called by the data itself!

has_key( 1991, d )

all data must be passed in as

function inputs…

are called on their own…Warning: this

has_key function is for

example purposes only. It does not

exist!

This has_key method is

built-in to all objects of type dictionary.

Page 15: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Word count.

def wordCount( filename = None ): """ creates and returns a list of words """ text = '' if filename == None: print "Enter lots o' text. End with a plain '42' line." while True: nextline = raw_input() if nextline == '42': break text += nextline + ' ‘ else: f = file( filename, 'r' ) text = f.read()

# text is now a bunch of space-separated words

list_of_words = text.split() # split splits a string print "The list of words is", list_of_words num_words = len( list_of_words ) print "There are", num_words, "words."

return list_of_words

Keyboard

File

split

Default inputs…

Page 16: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Vocabulary count!

# see wordCount for the set-up stuff

D = {} # an empty dictionary

for w in list_of_words: # check each word # How could/should we “clean up” w ?

if D.has_key( w ) == False: # w was not there D[w] = 1

else: # d.has_key( w ) == True, so w IS there D[w] += 1

print “There are ", len( D ), "distinct words." return D

NEW word - set to 1

OLD word - add by 1

Page 17: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Vocabularists?

Shakespeare used 31,534 different words and a grand total of 884,647 words counting repetitions (across his works)

http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html

Active vocabulary estimates range from 10,000-60,000.Passive vocabulary estimates are much higher.

Page 18: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Vocabularists?

Shakespeare used 31,534 different words and a grand total of 884,647 words counting repetitions (across his works)

http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html

Many Shakespearean contributions:

Shakespeare

Active vocabulary estimates range from 10,000-60,000.Passive vocabulary estimates are much higher.

Contemporary author in the OED…

which word…

Any guesses?

Page 19: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Vocabularists?

Shakespeare used 31,534 different words and a grand total of 884,647 words counting repetitions (across his works)

http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html

Shakespeare

J. K. Rowling

Active vocabulary estimates range from 10,000-60,000.Passive vocabulary estimates are much higher.

Many Shakespearean contributions:

Page 20: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

A challenge…

def provinceChallenge( prov ): """ prov is a dictionary of Canada's provinces -- the challenge is to name them all! """

while 0 in prov.values(): guess = raw_input("Name a province: ") if prov.has_key( guess ) == False: print 'Try again...' elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...'

print 'Phew!'

prov = { 'BC': 0, 'AB': 0, … }

help?!

All of Canada's provinces are in this dictionary…

Page 21: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

A family dictionar

y?

Page 22: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

A family dictionary…

T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer' :['hugo','bart','lisa','maggie'], 'marge' :['hugo','bart','lisa','maggie']}

keys can be any immutable type

values can be any type at all…

T['abe']

How to get 'selma' from T?

Page 23: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

A functional family?

def favChild( person, Tree ): """ person is a name (a string) Tree is a dictionary of children returns person's "favorite" child """

if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] return None

Who is favored ?

sort has side effects !

Page 24: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

A functional family?

def addChild( person, Tree, jr ): """ adds person's new child to Tree """

if Tree.has_key( person ) == False:

else: # already in the Tree!

For example, >>> addChild( 'lisa', T, 'abejr' )

Page 25: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

“Quiz”Change this code so that it counts

how many times you've guessed each item - as the dictionary value - both for real provinces and for incorrect guesses… Based on favChild (above), write

favGChild to return the first grandchild alphabetically - or return None if there are none.

To consider: Is the fav. child of the fav. child necessarily the

fav. grandchild by this definition?

def provinceChallenge( prov ):

while 0 in prov.values(): guess = raw_input("Guess: ")

if prov.has_key( guess ) == False: print 'Try again...'

elif prov[guess] == 0: print 'Yes!' prov[guess] += 1

else: print 'Already guessed...'

def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0]

return None

def favGChild( person, Tree ):

Name(s):

Page 26: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Change this code so that it tells you how many times you've guessed

the same province…

def provinceChallenge( prov ):

while 0 in prov.values(): guess = raw_input("Guess: ") if prov.has_key( guess ) == False: print 'Try again...' prov[guess] = 1

elif prov[guess] == 0: print 'Yes!' prov[guess] += 1

else: print 'Already guessed...' prov[guess] += 1

=

+=

new item?

existing item?

Page 27: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Based on favChild, write favGChild to return the first grandchild alphabetically - or return None if there are none.

def favGChild( person, Tree ): GC = []

if Tree.has_key( person ): for ch in Tree[person]: if Tree.has_key( ch ): GC += Tree[ ch ]

if GC != []: GC.sort() return GC[0]

return None

def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0]

return None

our list of GChildren

Accumulate!

Page 28: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Name that author… !

Page 29: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Markov Model

The text file:

I like spam. I like toast and spam. I eat ben and jerry's ice cream too.

Technique for modeling any sequence of natural data

Each item depends on only the item immediately before it .

The Model:

1st-order Markov Model

Page 30: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Markov Model

{ 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], … 'ben' :

The text file:

I like spam. I like toast and spam. I eat ben and jerry's ice cream too.

Technique for modeling any sequence of natural data

Each item depends on only the item immediately before it .

The Model:

1st-order Markov Model

Page 31: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Markov Model

{ 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' :

The text file:

I like spam. I like toast and spam. I eat ben and jerry's ice cream too.

Technique for modeling any sequence of natural data

Each item depends on only the item immediately before it .

The Model:

Page 32: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Markov Model

{ 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' : ['like', 'like', 'eat']

The text file:

I like spam. I like toast and spam. I eat ben and jerry's ice cream too.

Technique for modeling any sequence of natural data

Each item depends on only the item immediately before it .

The Model:

How to get to I?

Page 33: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Markov Model

{ 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' : ['like', 'like', 'eat'], '$' : ['I', 'I', 'I'],

The text file:

I like spam. I like toast and spam. I eat ben and jerry's ice cream too.

Technique for modeling any sequence of natural data

Each item depends on only the item immediately before it .

The Model:

sentence-starting string

Page 34: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Generative Markov Model

Technique for modeling any sequence of natural data

Each item depends on only the item immediately before it .

Original text:

A key benefit is that the model can generate feasible data!

I like spam. I like spam. I like toast and jerry's ice cream too.

I like spam. I like toast and spam. I eat ben and jerry's ice cream too.

Page 35: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

How to generate:1) start with the '$' string2) choose a word following '$', at random. Call it w3) choose a word following w, at random. And so on…4) If w ends a sentence, '$' becomes the next word.

I like spam. I like spam. I like toast and jerry's ice cream too.

{ 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' : ['like', 'like', 'eat'], '$' : ['I', 'I', 'I'], …}

The Model:

Page 36: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

WM-SCI

Page 37: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

WMSCI 2005

Markov-generated submission accepted

to WMSCI 2005

http://pdos.csail.mit.edu/scigen/

Page 38: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

WMSCI 2005

Page 39: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

WMSCI 2005

And a Markov presentation!

( in costume… )

Page 40: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Have a great weekend!

Perhaps a strategy for your next Hum

paper?

Farewell video…CS 5 (part 3) + CS 5 (part 2)

Page 41: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Life, revisitedGolly!

http://golly.sourceforge.net/

Page 42: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Representation & Speed

finding if a value is in a list of 10,000 elements…

looking up a key in a dictionary of 10,000 entries

vs.

list dictionary

LL[0] L[1]

reference

5 0

L[9999]

42…

{1988: 'dragon', 1989: 'snake'}

Page 43: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

value-added tts() options…

Page 44: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Problem 1 -- to and beyond!

• Are there stable life configurations?

• Are there oscillating life configurations?

• Are there self-propagating life configurations?

"rocks"

"plants"

"animals"

period 3

period 2

Page 45: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Problem 1 -- to and beyond!

• Are there life configurations that expand forever?

• What is the largest amount of the life universe that can be filled with cells?

• How sophisticated can the structures in the life universe be?

http://www.ibiblio.org/lifepatterns/

• Are all feasible configurations reachable?

Page 46: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

This is but ourselves. No, faith, My uncle! O royal bed of confession Of your rue for leave to nature; to this time I should weep for thy life is rotten before he is. have sworn 't. Or my blood. I have closely sent for nine; and unprofitable,

'Cause somethin' like he left knee and a harp," said he had to the whole school? The shouting and then some strange and Mrs. "Well, I know Hagrid; they spotted handkerchief and get him get rid of course, had a gigantic beet with her," he knew what to all he's

The Senators and the date of a written declaration that Purpose, they shall consist of nine States, shall not, when he shall have such Vacancies. The President pro tempore, in the Desire of a Qualification to the Speaker of the Senate. Article 6. When vacancies by the office upon probable

Name that author… ?

All the sky with the sun in the sun in the church where you're gone Lucy in my eyes. There beneath the girl with an hourglass And then the banker never wears a lot to hold your hand. Can't buy me tight, tight Owww! Love is love I can't hide,Who is the

author? What is the work? What is going on?

Page 47: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Exam on Mon.

Exam topics

Design chapter:

1d loops

2d loops

Hmmm

for & while

for row… for col…

add r2 r2 r1

Basics variables, functions, etc.

You may bring a page of notes, double-sided with anything you would like on it.

I'd suggest (1) reminding yourself about the HW problems and (2) looking at the class "quizzes"…

Page 48: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Change this code so that it tells you how many times you've guessed

the same province…

def provinceChallenge( prov ):

while '0' in prov.values(): guess = raw_input("Guess: ") if prov.has_key( guess ) == False: print 'Try again...'

elif prov[guess] == 0: print 'Yes!' prov[guess] += 1

else: print 'Already guessed...'

• first, for real provinces• then, for incorrect guesses…

Page 49: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Based on favChild, write favGChild to return the first grandchild alphabetically - or return 'no one' if there are

none.

def favGChild( person, Tree ): GC = []

def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] else: return 'no children'

our list of GChildren

Page 50: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.
Page 51: CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Name that author… ?