Title: IS 313 Today
1IS 313 Today
Strings, lists, and loops beyond 1d !
What's ahead?
10/7 Lec 5 today, now!
10/8 homework 3 due
10/14 Lec 6 "dictionaries"
10/15 homework 4 due
HMC Fall Break
http//www.asciimation.co.nz/
10/28 Lec 7 classes objects
11/29 homework 5 due
I see I'm not the only one loopy around here
HMC standings!?
2An image alternative
3ASCII Art
Print 4 rows
for row in range(3) print 4
print automaticallyadds a newline
Creates line to print
Output
4A closer look
for row in range(3) print ''4
Output
5No string !
for row in range(3) print ''4
For this problem, string multiplication is not
allowed.
Output
6Without string multiplication
Print 3 rows
for row in range(3) for col in range(4)
print ''
nested loops!
print 4 chars on each line
(columns)
Output ?
7Without string multiplication
Print 3 rows
for row in range(3) for col in range(4)
print '',
nested loops!
print 4 chars on each line
(columns)
Comma suppresses newline
Output ?
8Without string multiplication
Print 3 rows
for row in range(3) for col in range(4)
print '', print
nested loops!
print 4 chars on each line
(columns)
Comma suppresses newline
plain print produces newline
tracking cols and rows!
9Tracking rows and columns
for row in range(3) for col in range(4)
print '', print
Output
10Variations!
def rectangle( width, height ) """ prints a
rectangle of """
for row in range( ) for col in
range( ) print '', print
11"Quiz"
Fill in each block of code so that it will print
the lines above it.
0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6
0 1 2 3 4 5 6 1 2 3 4 5 6 7 2 3 4 5 6 7 8
for row in range( 3 ) for col in range( 7
) print ____________, print
for row in range( 3 ) for col in range( 7
) print ____________, print
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2
for row in range( 3 ) for col in range( 7
) print _____________, print
for row in range( 3 ) for col in range( 7
) print ______________, print
12Beyond boxes
What code would print this ASCII triangle ?
with height rows and width columns
1
cols
2
3
4
5
6
7
rows
for row in range(1,7)
1
2
for col in range( )
3
4
5
6
13A video alternative ?!?
http//www.asciimation.co.nz/
14A simpler web interface
HTML
HyperText Markup Language
ltbgtand the beautifullt/bgt ltbrgt
ltigtTower of Pisalt/igt
15A simpler web interface
HTML
HyperText Markup Language
ltbgtand the beautifullt/bgt ltbrgt
ltigtTower of Pisalt/igt
CGI
Common Gateway Interface HTML forms
ltform action"test.cgi"gt ltinput type"text"
name"message" /gt lt/formgt
16A simpler web interface
HTML
HyperText Markup Language
ltbgtand the beautifullt/bgt ltbrgt
ltigtTower of Pisalt/igt
CGI
Common Gateway Interface HTML forms
ltform action"test.cgi"gt ltinput type"text"
name"message" /gt lt/formgt
CSS
AJAX
Javascript
Others!
17Web resources on the Web
www.w3schools.com
every technology - and acronym - you'd ever want
to know
started by
Tim Berners-Lee
18This week
Get the pure-Python webserver and run it.
Make sure the example "application" works
Change the example "application" so that you
web-implement one of the ASCII art problems
Checklist
- be sure that you take in the inputs you need
- watch out for conversions between ints and
strings!
- make small changes and test as you go
This is not webdesign!
19Strings vs. language
humans
ideas / meaning
language / words / phrases
strings
numbers / bits
computers
20Turing Test
How could a computer demonstrate intelligence?
100,000 Loebner prize for passing the Turing
test, held each year in the London Museum of
Science
Alan Turing
Hugh Loebner
Confederates
www.abenteuermedien.de/jabberwock/index.php
21Parsing for real
Time flies like an arrow, but fruit flies like a
banana.
I like bananas, too!
- Marx
"lttimegt" "time" ltgt N NOM SG
_at_SUBJ "ltfliesgt" "fly" ltSVOgt ltSVgt V PRES
_at_FMAINV "fly" N NOM PL _at_NPHR "ltlikegt" "like"
PREP _at_ltNOM "ltangt" "an" ltIndefgt DET CENTRAL
ART "lt2-NLgt" "ltarrowgt" "arrow" N NOM SG
_at_NPHR "lt\,gt" "ltbutgt" "but" CC
_at_CC "ltfruit_fliesgt" "fruit_fly" N NOM PL
_at_NPHR "ltlikegt" "like" PREP _at_ltNOM "ltagt" "a"
ltIndefgt DET CENTRAL ART "ltbananagt" "banana" N
NOM SG _at_ltP "lt.gt"
(S (S (NP Time flies) (VP like
(NP an arrow))) , but (S (NP fruit)
(VP flies (PP like (NP a
banana)))) .)
http//www.link.cs.cmu.edu/link/submit-sentence-4.
html
parseTree 1
http//www.lingsoft.fi/cgi-bin/engcg
parseTree 2
22Some conversations
Jabb... Greetings! Jabberwock is at the
keyboard. You... How are you doing? Jabb...
Everything is running smoothly. And how are you
doing? You... Pretty well. It's quiet around
here this morning. Jabb... Does that mean you
agree? You... Agree with what? Jabb...
Sure.
2005 and 2006 winner Rollo Carpenter's
Jabberwacky
23Loebner results
2003
no human averaged less than 80
four humans and four machines participated
2005
24Some conversations
You Hi. youre the program that won the Loebner
competition, right? SID Albert One was the
last winner of the Loebner Contest. I am
something else entirely. You Impressive. Do
you have any areas of particular expertise?
25Some conversations
You Hi. youre the program that won the Loebner
competition, right? SID Albert One was the
last winner of the Loebner Contest. I am
something else entirely. You Impressive. Do
you have any areas of particular expertise? SID
I appreciate that. You Not so impressive. If
I ask a question after a statement, do you
ignore the question?
26Some conversations
You Hi. youre the program that won the Loebner
competition, right? SID Albert One was the
last winner of the Loebner Contest. I am
something else entirely. You Impressive. Do
you have any areas of particular expertise? SID
I appreciate that. You Not so impressive. If
I ask a question after a statement, do you
ignore the question? SID I am free to do as
I please. You Wow! Are you talking free will
vs. determinism?
27Some conversations
You Hi. youre the program that won the Loebner
competition, right? SID Albert One was the
last winner of the Loebner Contest. I am
something else entirely. You Impressive. Do
you have any areas of particular expertise? SID
I appreciate that. You Not so impressive. If
I ask a question after a statement, do you
ignore the question? SID I am free to do as
I please. You Wow! Are you talking free will
vs. determinism? SID I dont think wow am i
talking free will vs. determinism, but I
cant be 100 sure of that.
28Computing with language
open questions in AI
ideas / meaning
(1)
language / words / phrases
computing with language - recognizing English!
(2)
strings
how strings are represented and stored
(3)
numbers / bits
29Caesar Cipher
'Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.' 'Caesar
cipher? I prefer Caesar salad.'
30Caesar Cipher encipher
'gv vw dtwvg' 'hw wx euxwh'
encipher( 'gv vw dtwvg', 0 )
encipher( 'gv vw dtwvg', 1 )
31Caesar Cipher encipher
'gv vw dtwvg' 'hw wx euxwh' 'ix xy fvyxi' 'jy yz
gwzyj' 'kz za hxazk'
encipher( 'gv vw dtwvg', 0 )
encipher( 'gv vw dtwvg', 1 )
encipher( 'gv vw dtwvg', 2 )
encipher( 'gv vw dtwvg', 3 )
encipher( 'gv vw dtwvg', 4 )
. . .
'fu uv csvuf'
encipher( 'gv vw dtwvg', 25 )
should return the string s with each alphabetic
character shifted/wrapped by n places in the
alphabet
encipher( s , n )
32ASCII
American Standard Code for Information Interchange
low-tech art form!
33ASCII
__
_ ,___,-'",--. __,-- _
_,-'_)_ (""'-._\ . _,' __ ,' ,-'
__) ,- /. ,'_,--' -' _)/
\ ,',' ,' ,-'_,
,' ,-' ,(,-(
,' ,-' , _ /
,-._/---' / /
(____)(----. ) ,' / (
.__, /\ /, -.___
/__\/ ,' --. -,\
/ \ .__/ \
(__ \ _ \ ,-,
/ _,\ \ ,' -. ,'_,-' \
(_\,-' ,'\")--,'-' __\
\ / // ,' ,--' -. -.
-/ \' _,' . -._ /
--'/ \ -doh!- ,'
\ /
\ ,-'
/ /
-'
American Standard Code for Information Interchange
low-tech art form!
ASCII-betical order
min
42.42
Homer
Marge
)
bart
lisa
max
34ASCII
American Standard Code for Information Interchange
ASCII is a table that tells the computer how to
represent characters as s
chr
convert to char.
You know, these functions kind of look familiar!
convert to number
ord
35ASCII
American Standard Code for Information Interchange
ASCII is a table that tells the computer how to
represent characters as bits!
value
''
00101010
bits
type str
name
Identical bits are stored in each variable!
8 bits 1 byte
The types determine how to interpret the bits
the names don't matter at all
value
42
00101010
The SAME bits represent integers, if the variable
has type int instead of str
bits
type int
name
36chr and ord
abcdefghijklmnopqrstuvwxyz
ASCII VALUES
97
122
99
101
103
105
107
109
111
113
115
117
119
ABCDEFGHIJKLMNOPQRSTUVWXYZ
65
90
67
69
71
73
75
77
79
81
83
87
85
Input a string of one character, c
ord( c )
Output an integer, the ASCII value of c
CONVERTERS
Input an integer in range(255)
chr( n )
Output a one-char. string of that ASCII value
for i in range(128) print 'i and chr(i)
are', i, chr(i)
try this
37chr and ord
abcdefghijklmnopqrstuvwxyz
ASCII VALUES
97
122
99
101
103
105
107
109
111
113
115
117
119
ABCDEFGHIJKLMNOPQRSTUVWXYZ
65
90
67
69
71
73
75
77
79
81
83
87
85
ord('a') is 97
chr(66) is 'B'
What is chr( ord('i')13 )
What is chr( ord('W')13 ) ?
How can we wrap this around?
38Rot 13
def adv13( c ) """ rotates c by 13 chars,
"wrapping" as needed NON-LETTERS DO NOT
CHANGE! """ if 'a' lt c lt 'z'
new_ord ord(c) 13 if new_ord lt
ord('z') return chr(new_ord)
no wrapping else return
elif else
How would you rotate an entire string?
39Caesar Cipher decipher
gtgtgt decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq
rzkzc.') 'Caesar cipher? I prefer Caesar salad.'
gtgtgt decipher('Hu lkbjhapvu pz doha ylthpuz hmaly
dl mvynla '\ 'lclyfaopun dl ohcl
slhyulk.') 'An education is what remains after we
forget everything we have learned.'
gtgtgt decipher('gv vw dtwvg')
But how ?
40Caesar Cipher decipher
gv vw dtwvg hw wx euxwh ix xy fvyxi jy yz
gwzyj kz za hxazk la ab iybal mb bc jzcbm nc cd
kadcn od de lbedo pe ef mcfep qf fg ndgfq rg gh
oehgr sh hi pfihs ti ij qgjit uj jk rhkju vk kl
silkv wl lm tjmlw xm mn uknmx yn no vlony zo op
wmpoz ap pq xnqpa bq qr yorqb cr rs zpsrc ds st
aqtsd et tu brute fu uv csvuf
Caesar
Brutus
gtgtgt decipher('gv vw dtwvg')
Strategy using max
(1) consider all possible answers
(2) give them each a score
(3) loop through them all and find the one with
the max score!
Score for "Englishness"?
up to you
all 26 possibilities
41Caesar Cipher decipher
0, 'cr rs zpsrc' 0, 'gv vw dtwvg' 0, 'jy yz
gwzyj' 0, 'mb bc jzcbm' 0, 'qf fg ndgfq' 0,
'wl lm tjmlw' 1, 'bq qr yorqb' 1, 'ds st
aqtsd' 1, 'nc cd kadcn' 1, 'vk kl silkv' 1,
'xm mn uknmx' 2, 'ap pq xnqpa' 2, 'hw wx
euxwh' 2, 'ix xy fvyxi' 2, 'kz za hxazk' 2,
'rg gh oehgr' 2, 'sh hi pfihs' 2, 'uj jk
rhkju' 2, 'yn no vlony' 3, 'fu uv csvuf' 3,
'pe ef mcfep' 3, 'ti ij qgjit' 3, 'zo op
wmpoz' 4, 'et tu brute' 4, 'la ab iybal' 4,
'od de lbedo'
Caesar
Brutus
gtgtgt decipher('gv vw dtwvg') 'od de lbedo'
Strategy using max
(1) consider all possible answers
(2) give them each a score
(3) use our techniques with max
number-of-vowels score
won't always be correct!
all 26 possibilities
42Caesar Cipher decipher
0.4680, 'jy yz gwzyj' 0.4960, 'mb bc
jzcbm' 0.5420, 'uj jk rhkju' 0.5567, 'ix xy
fvyxi' 0.5597, 'qf fg ndgfq' 0.5718, 'fu uv
csvuf' 0.5753, 'bq qr yorqb' 0.5833, 'kz za
hxazk' 0.5859, 'xm mn uknmx' 0.5880, 'gv vw
dtwvg' 0.5902, 'vk kl silkv' 0.6110, 'ap pq
xnqpa' 0.6304, 'zo op wmpoz' 0.6318, 'wl lm
tjmlw' 0.6717, 'cr rs zpsrc' 0.6735, 'hw wx
euxwh' 0.6963, 'nc cd kadcn' 0.7153, 'ti ij
qgjit' 0.7398, 'la ab iybal' 0.7442, 'yn no
vlony' 0.7867, 'pe ef mcfep' 0.7880, 'sh hi
pfihs' 0.7918, 'rg gh oehgr' 0.8213, 'ds st
aqtsd' 0.8609, 'od de lbedo' 0.9082, 'et tu
brute'
Caesar
Brutus
gtgtgt decipher('gv vw dtwvg') 'et tu brute'
Strategy using max
(1) consider all possible answers
(2) give them each a score
(3) use our techniques with max
letter- probability score
not always correct, but better!
all 26 possibilities
43The caesar cipher problem offers lots of
freedom Think a bit about how you'll approach it!
Questions / Lab time
44Caesar Cipher decipher
gtgtgt decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq
rzkzc.') 'Caesar cipher? I prefer Caesar salad.'
gtgtgt decipher('Hu lkbjhapvu pz doha ylthpuz hmaly
dl mvynla '\ 'lclyfaopun dl ohcl
slhyulk.') 'An education is what remains after we
forget everything we have learned.'
gtgtgt decipher('gv vw dtwvg') ?
gtgtgt decipher('Uifz xpsl ju pvu xjui b qfodjm!') ?
But how to do this ?
45Caesar Cipher decipher
gv vw dtwvg hw wx euxwh ix xy fvyxi jy yz
gwzyj kz za hxazk la ab iybal mb bc jzcbm nc cd
kadcn od de lbedo pe ef mcfep qf fg ndgfq rg gh
oehgr sh hi pfihs ti ij qgjit uj jk rhkju vk kl
silkv wl lm tjmlw xm mn uknmx yn no vlony zo op
wmpoz ap pq xnqpa bq qr yorqb cr rs zpsrc ds st
aqtsd et tu brute fu uv csvuf
Caesar
Brutus
gtgtgt decipher('gv vw dtwvg') ???
Strategy using max
(1) consider all possible answers
(2) give them each a score
(3) use our techniques to get max
Score for "Englishness"?
up to you
all 26 possibilities
46Caesar Cipher decipher
0, 'cr rs zpsrc' 0, 'gv vw dtwvg' 0, 'jy yz
gwzyj' 0, 'mb bc jzcbm' 0, 'qf fg ndgfq' 0,
'wl lm tjmlw' 1, 'bq qr yorqb' 1, 'ds st
aqtsd' 1, 'nc cd kadcn' 1, 'vk kl silkv' 1,
'xm mn uknmx' 2, 'ap pq xnqpa' 2, 'hw wx
euxwh' 2, 'ix xy fvyxi' 2, 'kz za hxazk' 2,
'rg gh oehgr' 2, 'sh hi pfihs' 2, 'uj jk
rhkju' 2, 'yn no vlony' 3, 'fu uv csvuf' 3,
'pe ef mcfep' 3, 'ti ij qgjit' 3, 'zo op
wmpoz' 4, 'et tu brute' 4, 'la ab iybal' 4,
'od de lbedo'
Caesar
Brutus
gtgtgt decipher('gv vw dtwvg') 'od de lbedo'
Strategy using max
(1) consider all possible answers
(2) give them each a score
(3) use our techniques with max
Score for "Englishness"?
up to you
won't always be correct!
all 26 possibilities
47Caesar Cipher decipher
0.4680, 'jy yz gwzyj' 0.4960, 'mb bc
jzcbm' 0.5420, 'uj jk rhkju' 0.5567, 'ix xy
fvyxi' 0.5597, 'qf fg ndgfq' 0.5718, 'fu uv
csvuf' 0.5753, 'bq qr yorqb' 0.5833, 'kz za
hxazk' 0.5859, 'xm mn uknmx' 0.5880, 'gv vw
dtwvg' 0.5902, 'vk kl silkv' 0.6110, 'ap pq
xnqpa' 0.6304, 'zo op wmpoz' 0.6318, 'wl lm
tjmlw' 0.6717, 'cr rs zpsrc' 0.6735, 'hw wx
euxwh' 0.6963, 'nc cd kadcn' 0.7153, 'ti ij
qgjit' 0.7398, 'la ab iybal' 0.7442, 'yn no
vlony' 0.7867, 'pe ef mcfep' 0.7880, 'sh hi
pfihs' 0.7918, 'rg gh oehgr' 0.8213, 'ds st
aqtsd' 0.8609, 'od de lbedo' 0.9082, 'et tu
brute'
Caesar
Brutus
gtgtgt decipher('gv vw dtwvg') 'et tu brute'
Strategy using max
(1) consider all possible answers
(2) give them each a score
(3) use our techniques to get max
Score for "Englishness"?
up to you
depends on how you score
all 26 possibilities
48Caesar Cipher encipher
'gv vw dtwvg' 'hw wx euxwh' 'ix xy fvyxi' 'jy yz
gwzyj' 'kz za hxazk' 'la ab iybal'
encipher( 'gv vw dtwvg' , 0 )
encipher( 'gv vw dtwvg' , 1 )
encipher( 'gv vw dtwvg' , 2 )
encipher( 'gv vw dtwvg' , 3 )
encipher( 'gv vw dtwvg' , 4 )
encipher( 'gv vw dtwvg' , 5 )
'fu uv csvuf'
encipher( 'gv vw dtwvg' , 25 )
should return the string s with each alphabetic
character shifted/wrapped by n places in the
alphabet
encipher( s , n )
Historically bad decision 43
Brutus won't mind if I use his salary to bring
Justin Timberlake to the Colosseum next month...
49CHR and ORD
Orchard Field
50CHR and ORD
Chateauroux
51Mutable vs. Immutable data
Changeable types
Unchangeable types
tuple
dictionary
float
string
list
bool
int
x 42
42
x
What's a dictionary?
I guess I'll have to look it up!
52Reference vs. Value
Changeable types
Unchangeable types
tuple
dictionary
float
string
list
bool
int
L 5,42,'hi'
x 42
Reference, Pointer, id
42
42
'hi'
5
L
x
L0
L1
L2
53Pass By Value
def main() """ calls conform """ print "
Welcome to Conformity, Inc. " fav 7
conform(fav) print " My favorite number is",
fav def conform(fav) """ sets input to
42 """ fav 42 return fav
7
fav
fav
54Pass By Value
def main() """ calls conform """ print "
Welcome to Conformity, Inc. " fav 7
conform(fav) print " My favorite number is",
fav def conform(fav) """ sets input to
42 """ fav 42 return fav
7
fav
PASS BY VALUE
7
42
fav
Pass by value means that data is copied when
sent to a method
55Passing lists by value
def main() """ calls conform2 """ print "
Welcome to Conformity, Inc. " fav 7, 11
conform2(fav) print " My favorite
numbers are", fav def conform2(fav)
""" sets all of fav to 42 """ fav0 42
fav1 42
42
5
fav
L0
L1
fav
What gets passed by value here?
56Passing lists by value
def main() """ calls conform2 """ print "
Welcome to Conformity, Inc. " fav 7, 11
conform2(fav) print " My favorite
numbers are", fav def conform2(fav)
""" sets all of fav to 42 """ fav0 42
fav1 42
42
5
fav
L0
L1
The reference is copied!
fav
can change data elsewhere!
57The conclusion
You can change the contents of lists in functions
that take those lists as input.
(actually, lists or any mutable objects)
Those changes will be visible everywhere.
(immutable objects are safe, however)
58Views of the world
Engineers think their equations approximate
reality.
59Views of the world
Engineers think their equations approximate
reality. Physicists think reality approximates
their equations.
60Views of the world
Engineers think their equations approximate
reality. Physicists think reality approximates
their equations. Mathematicians don't care.
61Views of the world
Engineers think their equations approximate
reality. Physicists think reality approximates
their equations. Mathematicians don't care.
Creating structure from a few simple actions ...
Creating structure from a few simple facts...
for
arrays
variables
Axioms
while
Definitions
arithmetic operations
if/else
Proof
Algorithm
62Lists flexibility
Lists can hold ANY type of data
A 42., 75., 70.
42.0
75.0
70.0
float
float
float
list
A
they dont have to be horizontal lists!
63Lists flexibility
Lists can hold ANY type of data
A 42., 75., 70.
42.0
75.0
70.0
float
float
float
list
A
42.0
list
double
they dont have to be horizontal lists!
A
75.0
double
70.0
double
64Lsits flexibility
Lists can hold ANY type of data
42.0
75.0
70.0
double
double
list
double
A
42
7
-11
int
int
list
int
A
go
red
sox!
String
String
list
String
A
652d lists or arrays
Lists can hold ANY type of data -- including
lists !
A 1,2,3,4, 5,6, 7,8,9,10,11
list
A
662d arrays
Lists can hold ANY type of data -- including
lists !
A 1,2,3,4, 5,6, 7,8,9,10,11
list
list
A0
A
list
A1
list
A2
67Jagged arrays
Lists can hold ANY type of data -- including
lists !
A 1,2,3,4, 5,6, 7,8,9,10,11
list
list
A0
A
list
A1
list
A2
Rows within 2d arrays need not be the same length
68We will not use jagged arrays
at least in hw 10
Lists can hold ANY type of data -- including
lists !
A 1,2,3,4, 5,6, 7,8,9,10,11
list
list
A0
A
list
A1
list
A2
Rows within 2d arrays need not be the same length
69Rectangular arrays
A12 42
list
list
A0
A
A00
list
A1
list
A2
A23
What does each component of A12 mean ?
How many rows does A have, in general ?
How many columns does A have, in general ?
70Creating a 2d array
def create2dArray( width, height ) """ does
just that """ A start with
nothing for row in range( height )
for col in range( width ) return A
71Displaying a 2d array
import csplot
Be sure to download the newest version of this
file
B create2dArray( 10, 10 ) csplot.show( B
) B18 1 set this item to
1 csplot.show( B )
nearby coordinates
72Problem 2 -- Life
John Conway
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
73Problem 2 -- 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
74Problem 2 -- 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
75Problem 2 -- 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...
76Problem 2 -- Creating Life
B update( B )
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
77Problem 2 -- Creating Life
B update( B )
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
78Problem 2 -- Details
B update( B )
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 before updating any of them
http//www.math.com/students/wonders/life/life.htm
l
life out there...
79Problem 2 -- to ? and beyond!
"rocks"
- Are there stable life configurations?
"plants"
- Are there oscillating life configurations?
period 3
period 2
"animals"
- Are there self-propagating life configurations?
80Problem 2 -- 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?
Google for GOLLY, Game of Life
81Lab Homework
82Without extra spaces
import sys for i in range(4) for j in
range(8) sys.stdout.write('') print
method (function) that prints only its input
software object representing the screen's
standard output
Output