CS 4 this week Hw #10 due 11/19

CS 4 this week Hw #10 due 11/19

CS 4 this week Hw #10 due 11/19 Building classes... ... vs. using the library hw10pr2 hw10pr3 Connect Four Board class file and dictionary classes files Office hours ~ FRIDAY aft. in LAC dictionaries If I had a dictionary, I guess I could look up what it was! CS 60?

Same as CS5 2 x the work Much harder 2+ languages Racket Java Runtime! (define (whoami n) (if (= n 0) 1 (* n (whoami (- n 1))))) Who is whoami ? public static int whoami(int n) { if (n<2) return 1; else return n * whoami(n-1);

} 2 N O( N ) O( N ) O( 2 ) fast! medium slow... How efficient is whoami ? CS 35? 8-10 libraries Same as CS5

2 x the work Much harder Classes: DIY data design-it-yourself! Class: a user-defined datatype Object: data or a variable whose type is a class OOP! object-oriented Classes: DIY data design-it-yourself! Class: a user-defined datatype Object: data or a variable whose type is a class constructor object

d = Date( 12, 31, 2018 ) d.tomorrow() method d would be named self print(d) inside the Date class... uses repr Method: a function defined in a class called by an object self: in a class, the name of the object calling a method Constructor: the __init__ function for creating a new object repr: the __repr__ function returning a string to print data member: the data in self: self.day, self.month, self.year Why classes? Python has no Connect-four datatype | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | |X| |X|O| | |

|X|O|O|O|X| |O| --------------0 1 2 3 4 5 6 b2 Care for a game? and now we can fix that! Data design (Data Members) What data do we need? (Methods) What capabilities do we want? Not lim ited to 7x6! Our Board object, b row 0 ? str

str str str str str str str str str str str str str

str str str str str str str str str str str str

str str row 1 b.data Board b 7 b.width row 2 6 b.height row 3 row 4 row 5 b. ?

str str str str str str str str col 0 str col 1 str col 2 str

col 3 str str str col 4 col 5 col 6 b. How could we set ? to 'X' and ? to 'O' Data design (Data Members) What data do we need? (Methods) What capabilities do we want? Not lim ited to

7x6! __init__ the "constr uctor" class Board: """ a datatype representing a C4 board with an arbitrary number of rows and cols """ def __init__( self, width, height ): """ the constructor for objects of type Board """ self.width = width self.height = height W = self.width H = self.height self.data = [ [' ']*W for row in range(H) ] __init__ the "constr uctor"

class Board: """ a datatype representing a C4 board with an arbitrary number of rows and cols """ def __init__( self, width, height ): """ the constructor for objects of type Board """ self.width = width self.height = height W = self.width convenient! H = self.height self.data = [ [' ']*W for row in range(H) ] This list comprehension lets us create H independent rows with W independent columns each. __repr__ def __repr__(self): """ this method returns a string representation for an object of type Board """ H = self.height | | | | | | | |

W = self.width | | | | | | | | s = '' | | | | | | | | for r in range( H ): | | | |X| | | | s += '|' | |X| |X|O| | | for c in range( W ): |X|O|O|O|X| |O| --------------s += self.data[r][c] + '|' 0 1 2 3 4 5 6 s += '\n' s += (2*W+1)*'-' # what kind of loop will add the col #'s here? return s class Board: a C4 board col # Quiz

'X' or 'O' def addMove(self, col, ox): """ buggy version! """ shortcut H = self.height for row in range(0,H): if self.data[row][col] != ' ': self.data[row-1][col] = ox (1) Run b2.addMove(3,'O') (2) Bugs! Can you fix them?! Name(s) ___________________________ 0 1 2 3 4 5 6 0| | | | | | | | 1| | | | | | | | 2| | | | | | | | 3 | | | |X| | | | 4 | |X| |X|O| | | |X|O|O|O|X| |O| 5

b2 --------------0 1 2 3 4 5 6 b2.addMoveBUG(3,'O') class Board: a C4 board col # Quiz 'X' or 'O' def addMove(self, col, ox): """ correct version! """ shortcut H = self.height for row in range(0,H): if self.data[row][col] != ' ': self.data[row-1][col] = ox stop the loop! return

stop the function! self.data[H-1][col] = ox it only gets here if the column is empty it sets the bottom spot to the correct checker (1) Run b.addMove(3,'O') asthem?! h e (2) Bugs!th Can you fix g a p is t c e r r o

c the version Try this on the back page first 0 1 2 3 4 5 6 b 0| | | | | | 1| | | | | | 2| | | | | | 3 | | | |X| | 4 | |X| |X|O| |X|O|O|O|X| 5 | | | | | | | | | | |O| --------------0 1 2 3 4 5 6

b2.addMoveBUG(3,'O') Let's understand this allowsMove method b3.allowsMove 4 5 6 | | |X|O| | |O| | | |X|X| | |X| | | |O|O| | |O| | | |O|X| | |O| | |X|X|X| |O|X| |X|O|O|O|X|X|O| --------------0 1 2 3 4 5 6 b.allowsMove(0) == True b.allowsMove(1) == True b.allowsMove(2) == False b.allowsMove(3) == False If col is out-of-bounds or full, return False. If it's in-bounds and not full, return True. b.allowsMove(4) == True b.allowsMove(5) == True

b.allowsMove(6) == False b.allowsMove(7) == False b3 class Board: a C4 board Let's finish this allowsMove method col # b3.allowsMove def allowsMove(self, col): """ True if col is in-bounds + open False otherwise """ H = self.height shortcuts! 0 1 2 3 4 5 6 W = self.width 0 | | |X|O| | |O|

D = self.data 1 | | |X|X| | |X| if 2 | | |O|O| | |O| 3 | | |O|X| | |O| 4 | |X|X|X| |O|X| |X|O|O|O|X|X|O| 5 out of bounds? return False elif return False else: return True col full? --------------0 1 2 3 4 5 6 b.allowsMove(0) == True b.allowsMove(1) == True Allowed!

If col is out-of-bounds or full, return False. If it's in-bounds and not full, return True. b.allowsMove(2) == False b.allowsMove(3) == False b.allowsMove(4) == True b.allowsMove(5) == True b.allowsMove(6) == False b.allowsMove(7) == False b3 hw10pr2: Board class the constructor __init__( self, width, height ) checks if allowed allowsMove( self, col ) places a checker removes a checker outputs a string

checks if any space is left checks if a player has won the game... addMove( self, col, ox ) delMove( self, col ) to write... __repr__( self ) isFull( self ) to write... winsFor( self, ox ) to write... hostGame( self ) to write... Which are similar to others? Which requires the most thought?

winsFor( self, ox ) X O def winsFor(self, ox): """ does ox win? """ H = self.height W = self.width D = self.data b4 b.winsFor('X') or 'O' Watch out for corner cases! for row in range( for col in range( if if if

if ? ! r a i l i fam k o o l his t s e o D >>> b4.winsFor( True >>> b4.winsFor( False >>> b4.winsFor(

False >>> b4.winsFor( True 'X' ) 0 ) '0' ) 'O' ) Why objects and classes? Elegance: Objects hide complexity! if b.winsFor( 'X' ) == True: rem = self.diff( d2 ) % 7 Simple and INVITING -building blocks! CS 4 this week Hw #10 due 11/19 Building classes... ... vs. using the library hw10pr2

hw10pr3 Connect Four Board class files and the dictionary class files Office hours ~ FRIDAY aft. in LAC dictionaries If I had a dictionary, I guess I could look up what it was! CS 4 this week Hw #10 due 11/13 Building classes... ... vs. using the library hw10pr2 hw10pr3

Connect Four Board class files and the dictionary class files Office hours on FRIDAY aft. in LAC dictionaries If I had a dictionary, I guess I could look up what it was! Algorithmic Authorship... ? suppose this text represents my "style" ... r o h t u a m a r g

o r p a d l u ! o ? c e l y w t o s H e m a s s i

h t n i e s o r p w e n "Style" seems like the wrong word here... Algorithmic Authorship... ! suppose this text represents my "style" ... What would be a reasonable first word to start a newlygenerated sentence? What would be a reasonable next word to follow the first?

What would be a reasonable test for sentence-ending? Algorithmic authoring examples... Wanna live while we're cool, so tonight What a feeling to be doing what I wish I know we only met but it ain't hard to be nothing left The story of my life I'm watching her eyes smile you flip your eyes You don't know what makes you got stars, they're in the wire She said, "Can I got a feeling to be a dentist Who's the original human author of each of these? all British... Hint: they're Brit Lit's it! Markov Models Techniques for modeling any sequence of natural data speech, text, sensor data...

1st-order Markov Model (defining property) Each item depends only on the one immediately before it . Lists are sequential containers: element L = [ 47, 5, 47, 42 ] 0 1 3 2 index elements are looked up by their location, or index, starting from 0 ! Dictionaries are arbitrary containers:

e r u ct u r t s a t a d w e n d = { 47: 2, 42: 1 a d

e ) e . . . n s s e a l W new c (A value key } value key

elements (or values) are looked up by a key starting anywhere you want! Keys don't have to be ints! Lists are sequential containers: element L = [ 47, 5, 47, 42 ] 0 1 3 2 index elements are looked up by their location, or index, starting from 0 Dictionaries are arbitrary containers: d = { 47: 2, 42: 1 }

value key value key elements (or values) are looked up by a key starting anywhere you want! Keys don't have to be ints! Dictionaries are arbitrary containers: zd = {'rabbit':1999, 'ox':1997} value key value key elements (or values) are looked up by a key starting anywhere you want! Keys don't have to be ints! 's What's zd ? data here Now I see the key to dictionaries' value

Dictionaries are arbitrary containers: zd = {'rabbit':1999, 'ox':1997} value value key key elements (or values) are looked up by a key starting anywhere you want! Keys don't have to be ints! .. . c a i d o z r a 12-ye Now I see the key to dictionaries' value

Dictionaries are arbitrary containers: z = {'rabbit:[1999,1987,1975,...], 'ox':[1997,1985,1973,...], 'tiger':[1998,2010,...], ... } What type are the keys? What type are the values? Whose keys? z's keys! zi Dictionaries are arbitrary containers: z = {'rabbit:[1999,1987,1975,...], 'ox':[1997,1985,1973,...], 'dragon':[2000,1988,1976,...], ... } Is 'dragon' a key in z? if 'dragon' in z

Is 1969 in z['dragon']? if 1969 in z['dragon'] Dictionaries are in ! Karen v Dad! z = {'rabbit:[1999,1987,1975,...], 'ox':[1997,1985,1973,...], 'tiger':[1998,2010,...], ... } I can't tell you any of the questions -- but I can tell you all of the solutions! Is 'dragon' a key in z? if 'dragon' in z Is 1969 in z['dragon']?

if 1969 in z['dragon'] LoW = [ 'spam', 'spam', 'poptarts', 'spam' ] Oldenborg's menu! d will be... d = {} w will be... for w in LoW: if w not in d: {} {'spam':1} {'spam':2} d[w] = 1 else: d[w] += 1 vc_print(LoW) vc_print("a.txt")

{'poptarts':1, 'spam':2} {'poptarts':1, 'spam':3} final d LoW = [ 'spam', 'spam', 'poptarts', 'spam' ] Oldenborg's menu! d will be... d = {} w will be... for w in LoW: if w not in d: d[w] = 1 w ='spam' {'spam':1} w ='spam' {'spam':2} w ='poptarts'

{'poptarts':1, 'spam':2} else: d[w] += 1 vc_print(LoW) vc_print("a.txt") {} w ='spam' {'poptarts':1, 'spam':3} final d LoW = [ 'spam', 'spam', 'poptarts', 'spam' ] d = {} t e g o t

e r e h but w ? s d r o w y n a d will be... m so {} for w in LoW: if w not in d: Oldenborg's menu! ! S

E FIL {'spam':1} {'spam':2} d[w] = 1 else: d[w] += 1 vc_print(LoW) vc_print("a.txt") {'poptarts':1, 'spam':2} {'poptarts':1, 'spam':3} final d Files... In Python reading files is smooth f = open( 'a.txt' ) opens the file and calls it f

text = f.read() reads the whole file into the string text f.close() closes the file (optional) text 'I like poptarts and 42 and spam.\nWill I LoW = text.split() [ 'I', 'like', 'poptarts', ... ] text.split() returns a list of each "word" def word_count( filename ): f = open( filename ) text = f.read() f.close() file handling LoW = text.split() print("There are",len(LoW),"words")

What if we wanted the number of different words in the file? This would be the author's vocabulary count, instead of the total word count. Vocabulary, anyone? Shakespeare used 31,534 different words -- and a grand total of 884,647 words, counting repetitions across all of his works.... http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html Shakespearean coinages gust besmirch unreal superscript watchdog swagger affined rooky attasked

out-villained successful unsuccessful http://www.pathguy.com/shakeswo.htm http://www.shakespeare-online.com/biography/wordsinvented.html There's also one contemporary British author in the Oxford English Dictionary Who? rd? with what wo Vocabulary, anyone? Shakespeare used 31,534 different words -- and a grand total of 884,647 words, counting repetitions across all of his works.... http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html Shakespearean coinages

gust besmirch unreal superscript watchdog swagger affined rooky attasked out-villained successful unsuccessful http://www.pathguy.com/shakeswo.htm http://www.shakespeare-online.com/biography/wordsinvented.html muggle J. K. Rowling from filename import defaultdict def vocab_count( filename ):

""" vocabulary-counting program """ file handling f = open( filename ) text = f.read() f.close() LoW = text.split() print "There are", len(LoW), "words." word counting d = {} for w in LoW: if w not in d: d[w] = 1 else: d[w] += 1 Tracking the number of occurences of each word with a dictionary, d. Same as before

... print "There are", len(d), "_distinct_ words.\n" return d # return d for later use by other code most/least common? Markov Models can be generative! A key benefit of Markov Models is that they can generate feasible data! Original file: I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? I like spam poptarts! d = create_model('hpwhich.txt') d = create_model('randj.txt') d = create_model('oneD.txt') d = create_model('a.txt') gt(d,250) demo

Markov Models can be generative! A key benefit of Markov Models is that they can generate feasible data! Original file: I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? I like spam poptarts! Generated text: I get spam poptarts! I like poptarts and 42 and spam. I like spam and 42 and 42 and 42 and spam. Will I like poptarts and 42 and poptarts and 42 and poptarts and 42 and 42 and poptarts and spam. I get spam and 42 and 42 and... I agree! demo Our Markov Model Try it! keys Markov Model A dictionary!

What are the keys? What are the values? What are the missing values? What is the '$'? Why do some keys seem missing? values Original file { '$': ['I', 'Will', 'I'], 'I': ['like', 'get', 'like'] 'like': 'poptarts': ['and', 'for'], 'and': ['42', 'spam.', 'poptarts'], '42': ['and'], 'Will': ['I'], 'the':

'spam': ['and', 'poptarts!'], 'get': ['spam'], } 'for': ['the'] dictionary's end Our Markov Model Try it! keys Markov Model A dictionary! What are the keys? What are the values? What are the missing values? What is the '$'? Why do some keys seem missing? values

Original file { '$': ['I', 'Will', 'I'], 'I': ['like', 'get', 'like'] 'like': ['poptarts', 'spam'], 'poptarts': ['and', 'for'], 'and': ['42', 'spam.', 'poptarts'], '42': ['and'], 'Will': ['I'], 'the': ['holidays?'], 'spam': ['and', 'poptarts!'], 'get': ['spam'], } 'for': ['the'] dictionary's end Markov-modeling's algorithm LoW pw nw

['I','like','spam.','I','eat','poptarts!' ] cdi_print(PT2) cdi_print("a.txt") d = {} pw = '$' for nw in LoW: if pw not in d: d[pw] = [nw] else: d[pw] += [nw] pw = ________ d's final form (without quotes) $ : [ I, I ] I : [ like, eat ] like : [ spam. ] eat : [ poptarts! ] Model creation:

1) start with the previous word, pw as '$' 2) for each next word, nw, in the list of words, add it in ... 3) then change pw to nw ... (a) except if nw[-1] was punctuation: change pw to Generating text: 1) start with pw as the '$' string 2) choose a nw that follows pw, at random. 3) print nw, (the comma continues on the same line) 4) pw gets set to either nw or '$' or if nw[-1] was punctuation: change pw to Generating prose? Academic Opportunity! WMSCI WMSCI WMSCI

WMSCI 2005 http://pdos.csail.mit.edu/scigen/ Markov-generated submission accepted to WMSCI 2005 Not a first-order model ... but a third-order model Not a first-order model ... but a third-order model wardrobe? Not a first-order model ... but athird-order third-order model There are no one-sided coins... ge... a p n o o See vide http://www.bartneck.de/2016/10/20/ios-just-got-a-paper-on-nuclear-physics-accepted-at-a-scientific-conference/

Thesis deadlines? Papers due? Have a worry-free weekend! ou y r o f s r e pap r u o y e t i wr n o h

t r! y o P h t u a e Have h still t e r ' u o y class Board: a C4 board

col # Quiz 'X' or 'O' def addMove(self, col, ox): """ buggy version! """ H = self.height for row in range(0,H): if self.data[row][col] != ' ': self.data[row-1][col] = ox (1) Run b.addMove(3,'O') 0 1 2 3 4 5 6 (2) Bugs! Can you fix them?! Name(s) ___________________________ b 0| | | | | | 1| | | | | |

2| | | | | | 3 | | | |X| | 4 | |X| |X|O| |X|O|O|O|X| 5 | | | | | | | | | | |O| --------------0 1 2 3 4 5 6 b2.addMoveBUG(3,'O') class Board: a C4 board col # Quiz 'X' or 'O'

def addMove(self, col, ox): """ buggy version! """ H = self.height for row in range(0,H): if self.data[row][col] != ' ': self.data[row-1][col] = ox stop the loop! return stop the function! self.data[H-1][col] = ox only gets here if the column is empty it sets the bottom spot to the correct checker (1) Run b.addMove(3,'O') asthem?! h e (2) Bugs!th Can you fix g

a p is t c e r r o c the version Try this on the back page first 0 1 2 3 4 5 6 b 0| | | | | | 1| | | | | | 2| | | | | | 3 | | | |X| | 4 | |X| |X|O| |X|O|O|O|X|

5 | | | | | | | | | | |O| --------------0 1 2 3 4 5 6 b2.addMoveBUG(3,'O') class Board: a C4 board col # Let's finish this allowsMove method b3.allowsMove def allowsMove(self, col): """ True if col is in-bounds + open try these

False otherwise """ shortcuts H = self.height 0 1 2 3 4 5 6 W = self.width 0 | | |X|O| | |O| D = self.data 1 | | |X|X| | |X| if return False elif return False else: return True If col is out-of-bounds or full, return False. If it's in-bounds and not full, return True. 2 | | |O|O| | |O| 3 | | |O|X| | |O| 4 | |X|X|X| |O|X| |X|O|O|O|X|X|O| 5 --------------0 1 2 3 4 5 6

b.allowsMove(0) == True b.allowsMove(1) == True b.allowsMove(2) == False b.allowsMove(3) == False b.allowsMove(4) == True b.allowsMove(5) == True b.allowsMove(6) == False b.allowsMove(7) == False b Counting distinct words with a dictionary wd wd wd wd wd WOULD YOU LIKE THEM IN A HOUSE?

WOULD YOU LIKE THEN WITH A MOUSE? LoW I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM-I-AM. d keys values for wd in LoW: if wd not in d: d[wd] = 1 else: d[wd] += 1 def vocab_count( filename ): """ vocabulary-counting program """

f = open( filename ) text = f.read() file handling f.close() LoW = text.split() print "There are", len(LoW), "words." word counting d = {} for w in LoW: if w not in d: d[w] = 1 Tracking the number of occurances of each word with a dictionary, d. else: d[w] += 1 print "There are", len(d), "distinct words.\n" return d # return d for later use by other code

most/least common? Vocabulary, anyone? Shakespeare used 31,534 different words -- and a grand total of 884,647 words -- counting repetitions (across all of his works) http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html Shakespearean coinages gust besmirch unreal superscript watchdog swagger affined rooky attasked out-villained successful

unsuccessful http://www.pathguy.com/shakeswo.htm http://www.shakespeare-online.com/biography/wordsinvented.html There's one contemporary author in the Oxford English Dictionary Who? What word? d = cd('a.txt') Our Markov Model Original file: Markov Model: is a dictionary! What are the keys? What are the values? What are the missing values? What is the '$'?

Why do some keys seem missing? { '$': ['I', 'Will', 'I'], 'I': ['like', 'get', 'like'] 'like': ['poptarts', 'spam'], 'poptarts': ['and', 'for'], 'and': '42': ['and'], 'Will': ['I'], 'the': 'spam': ['and', 'poptarts!'], 'get': ['spam'], 'for': ['the'] } d = cd('a.txt') Our Markov Model Original file: Markov Model: is a dictionary!

What are the keys? What are the values? What are the missing values? What is the '$'? Why do some keys seem missing? { '$': ['I', 'Will', 'I'], 'I': ['like', 'get', 'like'] 'like': ['poptarts', 'spam'], 'poptarts': ['and', 'for'], 'and': '42': ['and'], 'Will': ['I'], 'the': 'spam': ['and', 'poptarts!'], 'get': ['spam'], 'for': ['the'] } Model creation:

1) start with the prevwd as '$' 2) for each nextwd in the list of words, add it in ... 3) then change nextwd to prevwd or '$'... if nextwd [-1] is punctuation. d = {} d['$'] = ['I'] d['I'] = d['like'] = . d['I'] += . . Our Markov Model Original file: Markov Model: What are the keys? What are the

values? What are the missing values? What is the '$'? Why do some keys seem missing? { '$': ['I', 'Will', 'I'], 'I': ['like', 'get', 'like'] 'like': ['poptarts', 'spam'], 'poptarts': ['and', 'for'], 'and': '42': ['and'], 'Will': ['I'], 'the': 'spam': ['and', 'poptarts!'], 'get': ['spam'], 'for': ['the'] A dictionary! } Our Markov Model

Original file: I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? I like spam poptarts! Markov Model: { '$': ['I', 'Will', 'I'], 'I': ['like', 'get', 'like'] 'like': ['poptarts', 'spam'], 'poptarts': ['and', 'for'], 'and': ['42', 'spam.', 'poptarts'], '42': ['and'], 'Will': ['I'], 'the': ['holidays?'], 'spam': ['and', 'poptarts!'], 'get': ['spam'], } 'for': ['the'] A !

y r a n dictio def createDictionary( filename ): """ creates a 1st-order M.Model """ f = open( filename ) text = f.read() f.close() LoW = text.split() d = {} prevwd = '$' for nextwd in LoW: if prevwd not in d: d[prevwd] = else: d[prevwd] += Model creation s u o

i v e r p e se e h t r o f e d i l s ! a e d i "key"

We want the KEY to be prevwd. We want the VALUE to be the list of words following prevwd. reset variables appropriately here be sure not to forget to check if the sentence has ended! return d Model creation: 1) start with the prevwd as '$' 2) for each nextwd in the list of words, add it in ... 3) then change nextwd to prevwd or '$'... if nextwd [-1] is punctuation. hw10pr3 $ : [ I, I ] I : [ like, eat ] like : [ spam ] eat : [ poptarts ] pw nw

I like spam. I eat d = {} pw = for nw in LoW: if pw not in d: d[pw] = else: d[pw] += poptarts! def createDictionary( filename ): """ creates a 1st-order M.Model """ f = open( filename ) text = f.read() f.close() LoW = text.split()

Model creation d = {} prevwd = '$' for nextwd in LoW: if prevwd not in d: d[prevwd] = else: d[prevwd] += We want the KEY to be prevwd. We want the VALUE to be the list of words following prevwd. winsFor( self, ox ) X O def winsFor(self, ox): """ does ox win? """ H = self.height W = self.width D = self.data

b b.winsFor('X') or 'O' Watch out for corner cases! class Board: a C4 board col # Quiz 'X' or 'O' def addMove(self, col, ox): """ buggy version! """ H = self.height for row in range(0,H): if self.data[row][col] != ' ':

self.data[row-1][col] = ox self.data[H][col] = ox (1) Run b.addMove(3,'O') 0 1 2 3 4 5 6 (2) Bugs! Can you fix them?! Try this on the back page first 0| | | | | | 1| | | | | | 2| | | | | | 3 | | | |X| | 4 | |X| |X|O| |X|O|O|O|X| 5 | | | | | | | | | | |O| --------------0 1 2 3 4 5 6

b class Board: a C4 board col # def allowsMove(self, col): """ True if col is in-bounds and open False otherwise """ H = self.height W = self.width D = self.data if Finish this allowsMove method If col is out-of-bounds or full, return False. If it's in-bounds and not full, return True. Quiz, p.2

class Board: a C4 board col # Let's finish this allowsMove method b3.allowsMove def allowsMove(self, col): """ True if col is in-bounds + open False otherwise """ H = self.height 0 1 2 3 4 5 6 W = self.width 0 | | |X|O| | |O| D = self.data 1 | | |X|X| | |X| if 2 | | |O|O| | |O| 3 | | |O|X| | |O| 4 | |X|X|X| |O|X|

|X|O|O|O|X|X|O| 5 --------------0 1 2 3 4 5 6 b.allowsMove(0) == True b.allowsMove(1) == True b.allowsMove(2) == False b.allowsMove(3) == False If it's in-bounds and not full, return True. If col is out-of-bounds or full, return False. b.allowsMove(4) == True b.allowsMove(5) == True b.allowsMove(6) == False b.allowsMove(7) == False b Dictiontaries don't seem moronic to me! More on dictionaries Strings can be keys, too!

>>> d2 = {'pig': 1995, 'rat': 1996} >>> 'pig' in d2 >>> 'cat' in d2 True False >>> len(d2) 2 in checks if a key is present len () returns the # of keys >>> d2.keys() [ 'pig', 'rat' ] d2.keys() returns a list of all keys >>> d2.values()

[ 1996, 1995 ] >>> d2.items() d2.values () returns a list of all values [ ('rat', 1996), ('pig', 1995) ] d2.items () returns a list of all key, value pairs hw10pr2: Board class the constructor __init__( self, width, height ) checks if allowed allowsMove( self, col ) places a checker removes a checker outputs a string checks if any space is left checks if a player has won

the game... addMove( self, col, ox ) delMove( self, col ) to write... __repr__( self ) isFull( self ) to write... winsFor( self, ox ) to write... hostGame( self ) to write... Which are similar to others? Which requires the most thought? winsFor( self, ox )

X O def winsFor(self, ox): """ does ox win? """ H = self.height W = self.width D = self.data b b.winsFor('X') or 'O' Watch out for corner cases! Why objects and classes? Elegance: Objects hide complexity! if b.winsFor( 'X' ) == True: if d.isBefore( d2 ) == True:

Simple and INVITING -- building blocks! encapsulating lots of complexity!! def isBefore(self, d2): """ Returns true if self is before d2 """ if self.year < d2.year: return True if self.month < d2.month and self.year == d2.year: return True if self.day < d2.day and d2.month == self.month and \ self.year == d2.year: return True return False Why objects and classes? Elegance: Objects hide complexity! if b.winsFor( 'X' ) == True: if d.isBefore( d2 ) == True: Simple and INVITING -- building blocks! encapsulating lots of complexity!! or maybe not so much!!!

def isBefore(self, d2): """ Returns true if self is before d2 """ return [self.year, self.month, self.day] < [d2.year, d2.month, d2.day] winsFor( self, ox ) X O def winsFor(self, ox): """ does ox win? """ H = self.height W = self.width b b.winsFor( 'O' ) or 'X' Watch out for corner cases! class Board: a C4 board

col # this addMove method does not work 'X' or 'O' Quiz What is each line doing? def addMove(self, col, ox): H = self.height for row in range( H ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[H][col] = ox What are the bugs!? 0 1 2 3 4 5 6 0| | | | | | | | 1| | | | | | | | 2| | | | | | | | 3 | | | |X| | | | 4 | |X| |X|O| | |

|X|O|O|O|X| |O| 5 --------------0 1 2 3 4 5 6 def allowsMove(self, col): Write allowsMove to return True if col is a valid move both in bounds and with room False otherwise. winsFor( self, ox ) X O def winsFor(self, ox): """ does ox win? """ H = self.height W = self.width b b.winsFor('X') or 'O'

Watch out for corner cases! This seems like the key to dictionaries' value Dictionaries A dictionary is a set of key - value pairs: >>> d = {} creates an empty dictionary, d >>> d[1994] = 'dog' 1994 is the key 'dog' is the value >>> d[1995] = 'pig' 1995 is the key 'pig' is the value >>> d {1995:'dog', 1994:'pig'}

Curly! And colony! >>> d[1994] 'dog' >>> d[1969] key error It's like a list, but the index can be any immutable key. def wc( filename ): """ our old word-counting program """ f = open( filename ) file handling text = f.read() f.close() LoW = text.split() print "There are", len(LoW), "words." d = {} for w in LoW: if w not in d: d[w] = 1 else:

d[w] += 1 Model creation word counting How do we change d's VALUES into the list of the words following each KEY ? A challenge PROV = { 'BC': 0, } key value All of Canada's provinces are in this dictionary 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 guess not in PROV:

wasn't a key at all print 'Try again...' elif prov[guess] == 0: newly guessed key: value was 0 print 'Yes!' prov[guess] += 1 else: repeated guess print 'Already guessed...' prov[guess] += 1 print 'Phew!' help! Markov Models Techniques for modeling any sequence of natural data speech, text, sensor-data in time series... Each item depends only on the item immediately before it . 1st-order Markov Model The text file:

The model: { 'like' : ['poptarts','spam'], 'and' : ['42', 'spam.', 'poptarts'], 'get' : ['spam'], 'poptarts': 'I' : 'Will' : How to get to Will? Markov Models The text file: The model: { 'like' : 'and' : 'get' : 'poptarts': 'I'

: 'Will' : '$' : Use '$' to represent the sentence-starting string. ['poptarts','spam'], ['42', 'spam.', 'poptarts'], ['spam'], ['and','for'], ['like','get','like'], ['I'], ['I', 'Will', 'I'] class Board: a C4 board col # this addMove method does not work

'X' or 'O' What is each line doing? def addMove(self, col, ox): What are the bugs!? H = self.height for row in range( H ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[H][col] = ox Original board: 0 1 2 3 4 5 6 0| | | | | | | | 1| | | | | | | | 2| | | | | | | | 3 | | | |X| | | | 4 | |X| |X|O| | | |X|O|O|O|X| |O| 5

--------------0 1 2 3 4 5 6 def allowsMove(self, col): Write allowsMove to return True if col is a valid move both in bounds and with room False otherwise. class Board: Quiz a C4 board col # 'X' or 'O' def addMove(self, col, ox): H = self.height for row in range( H ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[H][col] = ox

Step through this buggy addMove method. What is each line doing? Can you spot the problem(s) here? 0 1 0 1 2 3 4 5 6 2 3 4 5 def allowsMove(self, col): Write allowsMove to return True if col is a valid move; False otherwise. Hint: check as little as possible! class Board: Quiz

a C4 board col # 'X' or 'O' def addMove(self, col, ox): H = self.height for row in range( H ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[H][col] = ox Step through this buggy addMove method. What is each line doing? Can you spot the problem(s) here? 0 1 0 1 2 3 4 5 6 2

3 4 5 def allowsMove(self, col): Write allowsMove to return True if col is a valid move; False otherwise. Hint: check as little as possible! winsFor( self, ox ) X O def winsFor(self, ox): """ does ox win? """ H = self.height W = self.width b b.winsFor( 'O' ) or 'X' Watch out for corner cases!

Another challenge? def wc( filename ): """ our old word-counting program """ f = open( filename ) file handling text = f.read() f.close() LoW = text.split() print "There are", len(LoW), "words." d = {} for w in LoW: if w not in d: d[w] = 1 else: d[w] += 1 Model creation word counting How do we change d's VALUES into the list of the words following each

KEY ? Designing classes (Data Members) What data do we need? Not limited to 7x6! (Methods) What are capabilities do we want? Connect Four: __repr__ def __repr__(self): """ this method returns a string representation for an object of type Board """ s = '' for row in range( 6 ): s += '|' for col in range( 7 ): s += self.data[row][col] + '|'

s += '\n' To change? To add? return s which row is row 0, row 1, and so on? Quiz class Board: def allowsMove(self, col): Write allowsMove to return True if col is a valid move; False otherwise. Hint: check as little as possible! a C4 board col #

'X' or 'O' def addMove(self, col, ox): H = self.height for row in range( H ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[H][col] = ox Step through this imperfect addMove method. What is each line doing? Can you spot any potential problems here...? winsFor( self, ox ) X O def winsFor(self, ox): """ does ox win? """ H = self.height W = self.width b

b.winsFor( 'O' ) or 'X' Watch out for corner cases! def wc( filename ): """ word-counting program """ f = open( filename ) file handling text = f.read() f.close() LoW = text.split() print "There are", len(LoW), "words." word counting d = {} for w in LoW: if w not in d: d[w] = 1 Tracking the number of occurences of each word with a dictionary, d.

else: d[w] += 1 print "There are", len(d), "distinct words.\n" return d # this way we can use d later! Another challenge? Markov Models The text file: Techniques for modeling any sequence of natural data speech, text, sensor-data in time series... Each item depends only on the item immediately before it . I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? I like spam poptarts! 1st-order Markov

Model { 'like' : ['poptarts'], 'and' : ['42', 'spam.', 'poptarts'], 'get' : ['spam'], 'poptarts': The model: 'I' : 'Will' : How to get to Will? Markov Models The text file: Techniques for modeling any sequence of natural data speech, text, sensor-data in time series... Each item depends only on the item immediately before it .

I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? I like spam poptarts! { 'like' : 'and' : 'get' : 'poptarts': The model: 'I' : 'Will' : '$' : We'll use '$' as the sentence-starting string. 1st-order Markov Model

['poptarts'], ['42', 'spam.', 'poptarts'], ['spam'], ['and','for'], ['like','get','like'], ['I'], ['I', 'Will', 'I'] Markov Models The text file: The model: Techniques for modeling any sequence of natural data speech, text, sensor-data in time series... Each item depends only on the item immediately before it . I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? Mmmm, spam poptarts! { 'like' 'I' 'get'

'and' : ['poptarts'], : ['like', 'get'], : ['spam.'], : 1st-order Markov Model Markov Model Technique for modeling any sequence of natural data Each item depends on only the item immediately before it . The text file: The Model: 1st-order Markov Model I like spam. I like toast and spam. I eat ben and jerry's ice cream too.

{ 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : Markov Model Technique for modeling any sequence of natural data Each item depends on only the item immediately before it . The text file: The Model: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. { 'toast': 'and' : 'like' : 'ben' : 'I' : ['and'],

['spam.', "jerry's"], ['spam.', 'toast'], ['and'], Markov Model Technique for modeling any sequence of natural data Each item depends on only the item immediately before it . The text file: The Model: How to get to I? I like spam. I like toast and spam. I eat ben and jerry's ice cream too. { 'toast': 'and' : 'like' : 'ben' : 'I' : ['and'], ['spam.', "jerry's"],

['spam.', 'toast'], ['and'], ['like', 'like', 'eat'] Markov Model Technique for modeling any sequence of natural data Each item depends on only the item immediately before it . The text file: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. { 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], The Model: 'I' : ['like', 'like', 'eat'], '$' : ['I', 'I', 'I'], sentence-starting string 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 print 'Guessed:', prov[guess], 'times' elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 print 'Guessed:', prov[guess], 'times' else: print 'Already guessed...' prov[guess] += 1 print 'Guessed:', prov[guess], 'times' Based on favChild, write favGChild to return the first grandchild alphabetically - or return None if there are none. def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort()

return Kids[0] return None def favGChild( person, Tree ): our list of GChildren 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 Connect Four: Board b list data str

str str str str str str str str What is the name of the method that will print this data? the object b str str

str | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X| |O| --------------0 1 2 3 4 5 6 int width int height A family dictionary? 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 T['abe'] How to get 'selma' from T? values can be any type at all 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 ! A functional family? For example,

>>> addChild( 'lisa', T, 'abejr' ) def addChild( person, Tree, jr ): """ adds person's new child to Tree """ if Tree.has_key( person ) == False: else: # already in the Tree! Quiz Name(s): Change this code so that it prints how many times you've guessed each item, both for real provinces and for incorrect guesses 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 Based on favChild (above), write favGChild to return the first grandchild alphabetically - or return None if there are none. def favGChild( person, Tree ): 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.

Many Shakespearean contributions: Shakespeare Contemporary author in the OED which word Any guesses? 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. Many Shakespearean contributions: Shakespeare J. K. Rowling Infernalicious! also: www.cs.hmc.edu/~jgrasel/

Golly! Life, revisited http://golly.sourceforge.net/ Methods There's madness in this method! d.has_key( 1991 ) are functions that are called by the data itself! Functions has_key( 1991, d ) are called on their own all data must be passed in as function inputs Warning: this has_key function is for example

purposes only. It does not exist! Representation & Speed list dictionary {1988: 'dragon', 1989: 'snake'} reference L 5 0 L[0] L[1] 42 L[9999]

finding if a value is in a list of 10,000 elements vs. looking up a key in a dictionary of 10,000 entries value-added tts() options Problem 1 -- to and beyond! Are there stable life configurations? Are there oscillating life configurations? period 3 "rocks" "plants " period 2 Are there self-propagating life configurations?

"animals " 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? Are all feasible configurations reachable? How sophisticated can the structures in the life universe be? http://www.ibiblio.org/lifepatterns/ Name that author ? '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

Who is the author? What is the 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 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, work? What is going on? 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 Exam on Mon. Exam topics 1d loops for & while 2d loops for row for col Hmmm add r2 r2 r1 Basics variables, functions, etc.

Design chapter: 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" Change this code so that it tells you how many times you've guessed the same province first, for real provinces then, for incorrect guesses 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...' Based on favChild, write favGChild to return the first grandchild alphabetically - or return 'no one' if there are none. def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] else: return 'no children' def favGChild( person, Tree ): our list of GChildren GC = [] Name that author ? Visuospatial Skills Three dimensions are better than two

I should know! 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. If I had a dictionary, I guess I could look up what it was! Dictionaries Lists are not perfect You can't choose what to name data. L[0], L[1], You have to start at 0.

L[1990] = 'horse' L[1991] = 'ram' Some operations can be slow for big lists if 'rooster' in L: reference L 5 42 L[0] L[1] Vocabulary, anyone? Shakespeare used 31,534 different words -- and a grand total of 884,647 words -- counting repetitions (across all of his works) http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html Shakespearean coinages:

J. K. Rowling In search of elegance... One solution: def isBefore(self, d2): """ Returns true if self is before d2 """ if self.year < d2.year: return True if self.month < d2.month and self.year == d2.year: return True if self.day < d2.day and d2.month == self.month and \ self.year == d2.year: return True return False def isAfter(self, d2): """ Returns true if self is after d2 """ if self.year > d2.year: return True if self.month > d2.month and self.year == d2.year: return True if self.day > d2.day and d2.month == self.month and \ self.year == d2.year: return True

return False A more elegant solution def isBefore(self, d2): """ Returns true if self is before d2 """ if self.year < d2.year: return True if self.month < d2.month and self.year == d2.year: return True if self.day < d2.day and d2.month == self.month and \ self.year == d2.year: return True return False def isAfter(self, d2): """ Returns true if self is after d2 """ return d2.isBefore(self) 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 MandelGallery!

CS 5 this week Building classes... ... vs. using the library hw10pr2 hw10pr3 Connect Four Board class file and dictionary classes files dictionaries If I had a dictionary, I guess I could look up what it was! Classes: DIY data design-it-yourself! Class: a user-defined datatype

Object: data or a variable whose type is a class constructor object d = Date( 11, 11, 2010 ) d.tomorrow() method d would be named print d self inside the Date uses repr class... Method: a function defined in a class called by an object self: in a class, the name of the object calling a method Constructor: the __init__ function for creating a new object repr: the __repr__ function returning a string to print Designing classes (Data Members) What data do we need? Not limited to 7x6! (Methods) What are capabilities do we want?

Connect Four: Board b 66 int width 44 int height str str str str str ?

str str str str str str str str str str str str str

str str str str str str columns How could we set the ? to 'X' rows list data an object, b Connect Four:

constructor class Board: """ a datatype representing a C4 board with an arbitrary number of rows and cols """ def __init__( self, width, height ): """ the constructor for objects of type Board """ self.width = width self.height = height W = self.width H = self.height self.data = [ [' ']*W for row in range(H) ] List comprehensions let us create self.height independent rows with self.width independent columns each. Connect Four: __repr__ def __repr__(self): """ this method returns a string representation

for an object of type Board """ s = '' for row in range( 6 ): s += '|' for col in range( 7 ): s += self.data[row][col] + '|' s += '\n' To change? To add? return s which row is row 0, row 1, and so on? Quiz class Board: def allowsMove(self, col):

Write allowsMove to return True if col is a valid move; False otherwise. Hint: check as little as possible! a C4 board col # 'X' or 'O' def addMove(self, col, ox): H = self.height for row in range( H ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[H][col] = ox Step through this imperfect addMove method. What is each line doing? Can you spot any potential problems here...?

C4 Board class: hw10pr2 the constructor __init__( self, width, height ) checks if allowed allowsMove( self, col ) places a checker removes a checker outputs a string checks if any space is left checks if a player has won the game... addMove( self, col, ox ) delMove( self, col ) to write...

__repr__( self ) isFull( self ) to write... winsFor( self, ox ) to write... hostGame( self ) to write... Which are similar to others? Which requires the most thought? winsFor( self, ox ) X O def winsFor(self, ox): """ does ox win? """ H = self.height W = self.width

b b.winsFor( 'O' ) or 'X' Watch out for corner cases! Why objects and classes? Elegance: Objects hide complexity! if b.winsFor( 'X' ) == True: if d.isBefore( d2 ) == True: Simple and INVITING -- building blocks! encapsulating lots of complexity!! or maybe not so much!!! def isBefore(self, d2): """ Returns true if self is before d2 """ return [self.year, self.month, self.day] < [d2.year, d2.month, d2.day] In Python reading files is no problem

Files >>> f = file( 'a.txt' ) constructor and methods! opens the file and calls it f >>> text = f.read() reads the whole file and calls it f >>> f.close() closes the file (optional) >>> text all the text (as one big string) 'I like poptarts and 42 and spam.\nWill I >>> LoW = text.split() [ 'I', 'like', 'poptarts', ... ] text.split() returns a list of each "word"

This seems like the key to dictionaries' value Dictionaries In Python a dictionary is a set of key - value pairs. >>> d = {} creates an empty dictionary, d >>> d[1992] = 'monkey' 1992 is the key 'monkey' is the value >>> d[1993] = 'rooster' >>> d 1993 is the key 'rooster' is the value {1992:'monkey', 1993:'rooster'} Curly! And colony!

>>> d[1991] 'ram' Retrieve data like lists >>> d[1969] key error Almost ! It's a list where the index can be any immutable key. More on dictionaries They don't seem moronic to me! Dictionaries have lots of built-in methods, or functions: >>> d = {1992:'monkey', 1993:'rooster'} >>> 1992 in d True in checks if a key is present

>>> 1969 in d False >>> len(d) len returns the # of keys 2 >>> d.keys() [ 1992, 1993 ] d.keys returns a list of all keys d.values returns a list of all values d.items returns a list of all key/val. pairs def wc( filename ): """ word-counting program """ f = open( filename ) file handling text = f.read() f.close() LoW = text.split() print "There are", len(LoW), "words."

word counting What if we wanted the number of different words in the file? This would be the author's vocabulary size, instead of the word count. def wc( filename ): """ word-counting program """ f = open( filename ) file handling text = f.read() f.close() LoW = text.split() print "There are", len(LoW), "words." word counting d = {} for w in LoW: if w not in d: d[w] = 1

Tracking the number of occurences of each word with a dictionary, d. else: d[w] += 1 print "There are", len(d), "distinct words.\n" return d # this way we can use d later! 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. Many Shakespearean contributions: Shakespeare One contemporary author in the Oxford Eng. Dictionary

which word? A dictionary challenge PROV = { 'BC': 0, } key All of Canada's provinces are in this dictionary value 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 guess not in PROV: wasn't a key at all print 'Try again...' elif prov[guess] == 0: newly guessed key: value was 0 print 'Yes!' prov[guess] += 1 else: print 'Already guessed...'

repeated guess prov[guess] += 1 print 'Phew!' help?! Another challenge? Markov Models The text file: Techniques for modeling any sequence of natural data speech, text, sensor-data in time series... Each item depends only on the item immediately before it . I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? I like spam poptarts! 1st-order Markov Model

{ 'like' : ['poptarts'], 'and' : ['42', 'spam.', 'poptarts'], 'get' : ['spam'], 'poptarts': The model: 'I' : 'Will' : How to get to Will? Markov Models are generative! A key benefit of Markov Models is that they can generate feasible data! Original file: Generated text: I get spam poptarts! I like poptarts and 42 and spam. I like spam and 42 and 42 and 42 and spam. Will I like poptarts and 42 and poptarts and 42 and poptarts and 42 and 42 and poptarts and spam. I get spam and 42 and 42 and...

Model creation: 1) start with the prevword as '$' 2) for each word w, put w in prevword's list or create a new list with just w! 3) change prevword to word or '$' as appropriate... if w[-1] is punctuation. Generating text: 1) start with the '$' string 2) choose a word following '$', at random. Call it w 3) choose a word following w, at random. And so on 4) If w ends a sentence, '$' becomes the next word. if w[-1] is punctuation. more sophisticated than a first-order model! Counting distinct words with a dictionary WOULD YOU LIKE THEM IN A HOUSE? WOULD YOU LIKE THEN WITH A MOUSE? I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE.

I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM-I-AM. Which book? Which author? Counting distinct words with a dictionary WOULD YOU LIKE THEM IN A HOUSE? WOULD YOU LIKE THEN WITH A MOUSE? I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM-I-AM. Counting distinct words with a dictionary wd wd wd wd wd

WOULD YOU LIKE THEM IN A HOUSE? wd wd wd wd wd LoW WOULD YOU LIKE THEN WITH A MOUSE? wd wd wd wd wd

wd wd wd wd wd I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. d keys values for wd in LoW: if wd not in d: d[wd] = 1 else:

d[wd] += 1 Opportunities... ... across space (the 5Cs) and across time... ! Counting distinct words with a dictionary wd wd wd wd wd WOULD YOU LIKE THEM IN A HOUSE? WOULD YOU LIKE THEN WITH A MOUSE? LoW

I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM-I-AM. d keys values for w in LoW: if w not in d: d[w] = 1 else: d[w] += 1

Recently Viewed Presentations

  • Shame - Razor Planet

    Shame - Razor Planet

    New Message HSP (Aaron, E., 1998, p. 62-63) Please don't make me handle more than I can. When you do this I hurt all over. Don't blame me for how I am. I was born this way and this is...
  • Witness to Evolution Regents Biology 2006-2007 Witness to

    Witness to Evolution Regents Biology 2006-2007 Witness to

    Witness to Evolution Witness to Evolution Peppered Moth 2 types: dark vs. light Peppered moth: Evolution in action Peppered moth Why did the population change? early 1800s = pre-industrial England low pollution lichen on trees = light colored bark late...
  • Managerial Economics &amp; Business Strategy

    Managerial Economics & Business Strategy

    Managerial Economics & Business Strategy Chapter 2 Market Forces: Demand and Supply Market Equilibrium Balancing supply and demand QxS = Qxd Interaction of supply and demand determines the equilibrium price Price Restrictions Price Ceilings The maximum legal price that can...
  • 4-h OFFICER&#x27;S mANUAL

    4-h OFFICER'S mANUAL

    Sergeant At Arms. Check on room arrangements for each meeting and make sure proper club flags and banners are displayed and stored. Stand in front of the room while President calls the meeting to order and ask members to stand...
  • Assessment of Pupil Achievement

    Assessment of Pupil Achievement

    RME assignment. The assignment for the RME unit will normally be a plan for a small-scale educational enquiry. If you are near to completing your five units, this assignment can be used to develop a plan for your dissertation study.
  • Cross-cultural Neuropsychology Talk

    Cross-cultural Neuropsychology Talk

    By Behavior Language Family Heritage Behaviors Language- Spanish Religion- Catholic or Christian Family Children Friends Cooperation Personalismo Modesty Respect & Formality Hospitality Enjoyment of Living Typical Educational Background Often Limited Usually in Spanish Many are illiterate in Spanish and English...
  • Presentation Skills Type the authors name and date

    Presentation Skills Type the authors name and date

    Aims and Objectives. To assist students prepare and deliver a presentation as part of their Extended Project Qualification (EPQ) To give students an understanding of: Using appropriate resources. Presentation structure. Presentation skills. To assist students prepare and deliver a presentation...
  • Lecture #22 Date

    Lecture #22 Date

    Lecture #22 Date _____ Chapter 51 ~ Behavioral Biology Behavior Ethology~ study of animal behavior Causation: •proximate~ physiological & genetic mechanisms of behavior •ultimate~ evolutionary significance of behavior Sign stimulus~ external sensory stimulus Fixed action pattern (FAP)~ sequence of acts;...