Recitation Homework 5 - PowerPoint PPT Presentation

About This Presentation
Title:

Recitation Homework 5

Description:

Recitation Homework 5 Ionut Trestian Northwestern University – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 34
Provided by: Ion101
Category:

less

Transcript and Presenter's Notes

Title: Recitation Homework 5


1
Recitation Homework 5
  • Ionut Trestian
  • Northwestern University

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

3
Problem 1 -- Life
Grid World
red cells are alive
Evolutionary rules
  • Everything depends on a cells 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
4
Problem 1 -- Life
Grid World
red cells are alive
Evolutionary rules
  • Everything depends on a cells 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
5
Problem 1 -- Life
Grid World
red cells are alive
Evolutionary rules
  • Everything depends on a cells 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
6
Problem 1 -- Life
Grid World
red cells are alive
Evolutionary rules
  • Everything depends on a cells 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)

Keep going!
white cells are empty
life out there...
7
Problem 1 -- Creating Life
updateNextLife(oldB, newB)
new generation or "board"
old generation or "board"
0
1
2
3
4
5
0
1
2
3
4
5
0
0
1
1
2
2
3
3
4
4
5
5
8
Problem 1 -- Creating Life
updateNextLife(oldB, newB)
new generation or "board"
old generation or "board"
0
1
2
3
4
5
0
1
2
3
4
5
0
0
1
1
2
2
3
3
4
4
5
5
9
Problem 1 -- Details
updateNextLife(oldB, newB)
new generation or "board"
old 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/life.htm
l
life out there...
10
Problem 1 Main Loop
def life( width, height ) """ will become
John Conway's Game of Life... """ B
createBoard(width, height) csplot.showAndClick
InIdle(B) while True run
forever csplot.show(B) show current
B time.sleep(0.25) pause a bit
oldB B B createBoard( width,
height ) updateNextLife( oldB, B )
gets a new board
11
Problem 1 Main Loop
def life( width, height ) """ will become
John Conway's Game of Life... """ B
createBoard(width, height) csplot.showAndClick
InIdle(B) while True run
forever csplot.show(B) show current
B time.sleep(0.25) pause a bit
oldB B B createBoard( width,
height ) updateNextLife( oldB, B )
gets a new board
Update MUTATES the list B
Why not just have update RETURN a new list?
(i.e., why bother with mutation at all?)
12
Problem 2 - Markov Text Generation
Technique for modeling any sequence of natural
data
1st-order Markov Model
Each item depends on only the item immediately
before it .
I like spam. I like toast and spam. I eat ben
and jerry's ice cream too.
The text file
For each word, keep track of the words that can
follow it (and how often)
The Model
I like, like, eat like spam, toast spam.
I, I, I toast and eat ben and spam, jerry's
ben and jerry's ice ice cream cream
too. too.
  • We can repeat wordsto indicate frequency
  • indicates beginningof a sentence

13
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 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, '' becomes the next
word.
14
HW5 Pr 2 Need to be able to
Read text from a file Compute and store the
model Generate the new text
15
Reading Files
In Python reading files is no problem
gtgtgt f file( 'a.txt' ) gtgtgt text f.read() gtgtgt
text 'This is a file.\nLine 2\nLast
line!\n' gtgtgt f.close()
16
Files
In Python reading files is no problem
gtgtgt f file( 'a.txt' ) gtgtgt text f.read() gtgtgt
text 'This is a file.\nLine 2\nLast
line!\n' gtgtgt f.close()
opens the file and calls it f
reads the whole file and calls it text
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)
17
String Manupulation
gtgtgt text 'This is a file.\nLine 2\nLast
line!\n' gtgtgt print text This is a file. Line
2 Last line! gtgtgt text.split() 'This', 'is',
'a', 'file.', 'Line', '2', 'Last', 'line!' gtgtgt
text 'This is a file.\nLine 2\nLast line!\n' gtgtgt
lines text.split('\n') gtgtgt lines 'This is a
file.', 'Line 2', 'Last line!', ''
Returns a list of the words in the
string(splitting at spaces, tabs and newlines)
Returns a list of the lines in the
string(splitting at newlines)
18
HW5 Pr 2 Need to be able to
Read text from a file Compute and store the
model Generate the new text
19
Lists vs. Dictionaries
Lists are not perfect
reference
42
5
L
L0
L1
20
Lists vs. Dictionaries
Lists are not perfect
reference
42
5
You can't choose what to name data.
L
L0
L1
L0, L1,
21
Lists vs. Dictionaries
Lists are not perfect
reference
42
5
You can't choose what to name data.
L
L0
L1
L0, L1,
You have to start at 0.
L1988 'dragon'
L1989 'snake'
22
Lists vs. Dictionaries
Lists are not perfect
reference
42
5
You can't choose what to name data.
L
L0
L1
L0, L1,
You have to start at 0.
L1988 'dragon'
L1989 'snake'
Some operations can be slow for big lists
if 'dragon' in L
23
Lists vs. Dictionaries
In Python a dictionary is a set of key - value
pairs.
gtgtgt d gtgtgt d1988 'dragon' gtgtgt d1989
'snake' gtgtgt d 1988 'dragon', 1989 'snake' gtgtgt
d1988 'dragon' gtgtgt d1987 key error
It's a list where the index can be any
immutable-type key.
24
Lists vs. Dictionaries
In Python a dictionary is a set of key - value
pairs.
gtgtgt d gtgtgt d1988 'dragon' gtgtgt d1989
'snake' gtgtgt d 1988 'dragon', 1989 'snake' gtgtgt
d1988 'dragon' gtgtgt d1987 key error
creates an empty dictionary, d
1988 is the key
'dragon' is the value
1989 is the key
'snake' is the value
Anyone seen this before?
Retrieve data as with lists
or almost !
It's a list where the index can be any
immutable-type key.
25
More on dictionaries
Dictionaries have lots of built-in methods
gtgtgt d 1988 'dragon', 1989 'snake' gtgtgt
d.keys() 1989, 1988 gtgtgt d.has_key( 1988
) True gtgtgt d.has_key( 1969 ) False gtgtgt d.pop(
1988 ) 'dragon'
get all keys
check if a key is present
delete a key (and its value)
26
Markov Model
Technique for modeling any sequence of natural
data
Each item depends on only the item immediately
before it .
I like spam. I like toast and spam. I eat ben
and jerry's ice cream too.
The text file
'toast' 'and', 'and' 'spam.',
"jerry's", 'like' 'spam.', 'toast',
'ben' 'and', 'I' 'like', 'like',
'eat', '' 'I', 'I', 'I',
The Model
27
Extra credit Problem 3
printBumps( 4, '', '' )  

 
printSquare( 3, '' )  
printRect( 4, 6, '' )  
 
printTriangle( 3, '_at_', True )   _at_ _at_ _at_ _at_ _at_
_at_   printTriangle( 3, '_at_', False )   _at_ _at_ _at_ _at_
_at_ _at_  
28
Extra credit Problem 3
printCrazyStripedDiamond( 7, '.', '', 2, 1 )  
. . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . .    
printStripedDiamond( 7, '.', '' )   .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . .    
printDiamond( 3, '' )  
 
29
EC Problem 4 A program that reads
Flesch Index (FI) FI 206.835 - 84.6
numSyls/numWords - 1.015 numWords/numSents numS
yls is the total number of syllables in the text
numWords is the total number of words in the
text numSents is the total number of sentences
in the text flesch() function
30
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) sylla
bles(oneword)
31
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 (punctuation-stripped) 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.
32
Extra Credit Problem 5 Matrix Multiplication
  • Gaussian elimination - another name for the
    process of using row operations in order 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?

33
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
Write a Comment
User Comments (0)
About PowerShow.com