Recitation Homework 5 Northwestern University The Game of

  • Slides: 33
Download presentation
Recitation Homework 5 Northwestern University

Recitation Homework 5 Northwestern University

The Game of Life: History • Created by John Horton Conway, a British Mathematician

The Game of Life: History • Created by John Horton Conway, a British Mathematician • Inspired by a problem presented by John Von Neumann: – Build a hypothetical machine that can build copies of itself • First presented in 1970, Scientific American

Problem 1 -- “Life” Grid World red cells are alive Evolutionary rules • Everything

Problem 1 -- “Life” Grid World red cells are alive Evolutionary rules • Everything depends on a cell’s eight neighbors • Exactly 3 neighbors give birth to a new, live cell! • Exactly 2 or 3 neighbors keep an existing cell alive • Any other number of neighbors kill the central cell (or keep it dead) white cells are empty

Problem 1 -- Life Grid World red cells are alive Evolutionary rules • Everything

Problem 1 -- Life Grid World red cells are alive Evolutionary rules • Everything depends on a cell’s eight neighbors • Exactly 3 neighbors give birth to a new, live cell! • Exactly 2 or 3 neighbors keep an existing cell alive • Any other number of neighbors kill the central cell (or keep it dead) white cells are empty

Problem 1 -- Life Grid World red cells are alive Evolutionary rules • Everything

Problem 1 -- Life Grid World red cells are alive Evolutionary rules • Everything depends on a cell’s eight neighbors • Exactly 3 neighbors give birth to a new, live cell! • Exactly 2 or 3 neighbors keep an existing cell alive • Any other number of neighbors kill the central cell (or keep it dead) white cells are empty

Problem 1 -- Life Grid World red cells are alive Evolutionary rules • Everything

Problem 1 -- Life Grid World red cells are alive Evolutionary rules • Everything depends on a cell’s eight neighbors • Exactly 3 neighbors give birth to a new, live cell! • Exactly 2 or 3 neighbors keep an existing cell alive • Any other number of neighbors kill the central cell (or keep it dead) white cells are empty Keep going! life out there. . .

Problem 1 -- Creating Life update. Next. Life(old. B, new. B) new generation or

Problem 1 -- Creating Life update. Next. Life(old. B, new. B) new generation or "board" old generation or "board" 0 1 2 3 4 0 5 0 0 1 1 2 2 3 3 4 4 5 5 1 2 3 4 5

Problem 1 -- Creating Life update. Next. Life(old. B, new. B) new generation or

Problem 1 -- Creating Life update. Next. Life(old. B, new. B) new generation or "board" old generation or "board" 0 1 2 3 4 0 5 0 0 1 1 2 2 3 3 4 4 5 5 1 2 3 4 5

Problem 1 -- Details update. Next. Life(old. B, new. B) old generation or "board"

Problem 1 -- Details update. Next. Life(old. B, new. B) old generation or "board" new generation or "board" For each generation… • 0 represents an empty cell • 1 represents a living cell • outermost edge should always be left empty (even if there are 3 neighbors) • compute all cells based on their previous neighbors http: //www. math. com/students/wonders/life. html life out there. . .

Problem 1 – Main Loop def life( width, height ): """ will become John

Problem 1 – Main Loop def life( width, height ): """ will become John Conway's Game of Life. . . """ B = create. Board(width, height) csplot. show. And. Click. In. Idle(B) while True: # run forever csplot. show(B) # show current B time. sleep(0. 25) # pause a bit old. B = B B = create. Board( width, height ) update. Next. Life( old. B, B ) # gets a new board

Problem 1 – Main Loop def life( width, height ): """ will become John

Problem 1 – Main Loop def life( width, height ): """ will become John Conway's Game of Life. . . """ B = create. Board(width, height) csplot. show. And. Click. In. Idle(B) while True: # run forever csplot. show(B) # show current B time. sleep(0. 25) # pause a bit old. B = B B = create. Board( width, height ) update. Next. Life( old. B, B ) # gets a new board Update MUTATES the list B

Problem 2 - Markov Text Generation Technique for modeling any sequence of natural data

Problem 2 - Markov Text Generation Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. The text file: 1 st-order Markov Model I like spam. I like toast and spam. I eat ben and jerry's ice cream too. For each word, keep track of the words that can follow it (and how often) The Model: I: like, eat like: spam, toast spam. : $ $: I, I, I toast: and eat: ben and: spam, jerry's ben: and jerry's: ice: cream: too. : $ • We can repeat words to indicate frequency • $ indicates beginning of a sentence

Generative Markov Model Technique for modeling any sequence of natural data Each item depends

Generative Markov Model Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. A key benefit is that the model can generate feasible data! I like spam. I like toast and jerry's ice cream too. 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, a random choice among words following'$' becomes the next word.

HW 5 Pr 2: Need to be able to… Read text from a file

HW 5 Pr 2: Need to be able to… Read text from a file Compute and store the model Generate the new text

Reading Files In Python reading files is no problem… >>> f = open( 'a.

Reading Files In Python reading files is no problem… >>> f = open( 'a. txt' ) >>> text = f. read() >>> text 'This is a file. n. Line 2n. Last line!n' >>> f. close()

Files In Python reading files is no problem… >>> f = open( 'a. txt'

Files In Python reading files is no problem… >>> f = open( 'a. txt' ) opens the file and calls it f >>> text = f. read() reads the whole file and calls it text >>> text 'This is a file. n. Line 2n. Last line!n' >>> f. close() text is a single string containing all the text in the file But how to process the text from here…? closes the file (closing Python does the same)

String Manupulation >>> text 'This is a file. n. Line 2n. Last line!n' >>>

String Manupulation >>> text 'This is a file. n. Line 2n. Last line!n' >>> print text This is a file. Line 2 Returns a list of the words in the string Last line! (splitting at spaces, tabs and newlines) >>> text. split() ['This', 'a', 'file. ', 'Line', '2', 'Last', 'line!'] >>> text 'This is a file. n. Line 2n. Last line!n' >>> lines = text. split('n') >>> lines ['This is a file. ', 'Line 2', 'Last line!', ''] Returns a list of the lines in the string (splitting at newlines)

HW 5 Pr 2: Need to be able to… Read text from a file

HW 5 Pr 2: Need to be able to… Read text from a file Compute and store the model Generate the new text

Lists vs. Dictionaries Lists are not perfect… reference L 5 42 L[0] L[1]

Lists vs. Dictionaries Lists are not perfect… reference L 5 42 L[0] L[1]

Lists vs. Dictionaries Lists are not perfect… You can't choose what to name data.

Lists vs. Dictionaries Lists are not perfect… You can't choose what to name data. L[0], L[1], … reference L 5 42 L[0] L[1]

Lists vs. Dictionaries Lists are not perfect… You can't choose what to name data.

Lists vs. Dictionaries Lists are not perfect… You can't choose what to name data. L[0], L[1], … You have to start at 0. L[1988] = 'dragon' L[1989] = 'snake' reference L 5 42 L[0] L[1]

Lists vs. Dictionaries Lists are not perfect… You can't choose what to name data.

Lists vs. Dictionaries Lists are not perfect… You can't choose what to name data. L[0], L[1], … You have to start at 0. L[1988] = 'dragon' L[1989] = 'snake' Some operations can be slow for big lists … if 'dragon' in L: reference L 5 42 L[0] L[1]

Lists vs. Dictionaries In Python a dictionary is a set of key - value

Lists vs. Dictionaries In Python a dictionary is a set of key - value pairs. >>> d = {} >>> d[1988] = 'dragon' >>> d[1989] = 'snake' >>> d {1988: 'dragon', 1989: 'snake'} >>> d[1988] 'dragon' >>> d[1987] key error It's a list where the index can be any immutable-type key.

Lists vs. Dictionaries In Python a dictionary is a set of key - value

Lists vs. Dictionaries In Python a dictionary is a set of key - value pairs. >>> d = {} creates an empty dictionary, d >>> d[1988] = 'dragon' >>> d[1989] = 'snake' 1988 is the key 'dragon' is the value 1989 is the key 'snake' is the value >>> d {1988: 'dragon', 1989: 'snake'} >>> d[1988] 'dragon' >>> d[1987] Retrieve data as with lists… key error or almost ! It's a list where the index can be any immutable-type key.

More on dictionaries Dictionaries have lots of built-in methods: >>> d = {1988: 'dragon',

More on dictionaries Dictionaries have lots of built-in methods: >>> d = {1988: 'dragon', 1989: 'snake'} >>> d. keys() [ 1989, 1988 ] get all keys >>> 1988 in d True >>> 1969 in d check if a key is present False >>> d. pop( 1988 ) 'dragon' delete a key (and its value)

Markov Model Technique for modeling any sequence of natural data Each item depends on

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'], ['like', 'eat'], ['I', 'I'],

Extra credit Problem 3 print. Square( 3, '$' ) print. Triangle( 3, '@', True

Extra credit Problem 3 print. Square( 3, '$' ) print. Triangle( 3, '@', True ) $$$ $$$ @ @@ @@@ print. Rect( 4, 6, '%' ) print. Triangle( 3, '@', False ) %%%% %%%% @@@ @@ @ print. Bumps( 4, '%', '#' ) % # % %% ## # % %% %%% ### ## # % %% %%%% #### ## #

Extra credit Problem 3 print. Diamond( 3, '&' ) & && && & print.

Extra credit Problem 3 print. Diamond( 3, '&' ) & && && & print. Striped. Diamond( 7, '. ', '%' ). . %. %. . print. Crazy. Striped. Diamond( 7, '. ', '%', 2, 1 ). . . %. %. .

EC Problem 4 A program that reads Flesch Index (FI) FI = 206. 835

EC Problem 4 A program that reads Flesch Index (FI) FI = 206. 835 - 84. 6 * num. Syls/num. Words - 1. 015 * num. Words/num. Sents num. Syls is the total number of syllables in the text num. Words is the total number of words in the text num. Sents is the total number of sentences in the text flesch() function

Extra Credit Problem 4 flesch() function Welcome to the text readability calculator! Your options

Extra Credit Problem 4 flesch() function Welcome to the text readability calculator! Your options include: (1) Count sentences (2) Count words (3) Count syllables in one word (4) Calculate readability (9) Quit What option would you like? sentences(text) words(text) syllables(oneword)

Extra Credit Problem 4 Split Remove punctuation We will say that a sentence has

Extra Credit Problem 4 Split Remove punctuation We will say that a sentence has occurred any time that one of its raw words ends in a period. question mark ? or exclamation point ! Note that this means that a plain period, question mark, or exclamation point counts as a sentence. A vowel is a capital or lowercase a, e, i, o, u, or y. A syllable occurs in a punctuation-stripped word whenever: Rule 1: a vowel is at the start of a word Rule 2: a vowel follows a consonant in a word Rule 3: there is one exception: if a lone vowel e or E is at the end of a (punctuationstripped) word, then that vowel does not count as a syllable. Rule 4: finally, everything that is a word must always count as having at least one syllable.

Extra Credit Problem 5 Matrix Multiplication - Gaussian elimination - another name for the

Extra Credit Problem 5 Matrix Multiplication - Gaussian elimination - another name for the process of using row operations in orde to bring a matrix to reduced-row-echelon form. (1) Enter the size and values of an array (2) Print the array (3) Multiply an array row by a constant (4) Add one row into another (5) Add a multiple of one row to another (6) Solve! (7) Invert! [This is extra. . . ] (9) Quit Which choice would you like?

Extra Credit Problem 5 Matrix Multiplication for col in range(len(A)): # do the appropriate

Extra Credit Problem 5 Matrix Multiplication for col in range(len(A)): # do the appropriate thing here for row in range(len(A)): # do the appropriate thing here when row != col