IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now!...

82
IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries" 10/15 homework 4 due 10/28 Lec 7 ~ classes & objects 11/29 homework 5 due Strings, lists, and loops ~ beyond 1d ! http://www.asciimation.co.nz/ HMC Fall Break

Transcript of IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now!...

Page 1: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

IS 313 Today

What's ahead?

I see I'm not the only one loopy around here…

10/7 Lec 5 ~ today, now!

HMC standings!?

10/8 homework 3 due

10/14 Lec 6 ~ "dictionaries"10/15 homework 4 due

10/28 Lec 7 ~ classes & objects11/29 homework 5 due

Strings, lists, and loops ~ beyond 1d !

http://www.asciimation.co.nz/

HMC Fall Break

Page 2: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

An image alternative…

Page 3: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

############

for row in range(3): print “#”*4

Print 4 rows

Creates line to print

print automaticallyadds a newline

Output

ASCII Art

Page 4: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

$$$$

$$$$

$$$$

for row in range(3): print '$'*4

Output

A closer look…

Page 5: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

$$$$

$$$$

$$$$

for row in range(3): print '$'*4

Output

No string * !

For this problem, string multiplication

is not allowed.

Page 6: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Without string multiplication…

for row in range(3): for col in range(4): print '$'

Print 3 rows

print 4 chars on each line

nested loops!

Output ?

(columns)

Page 7: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Without string multiplication…

for row in range(3): for col in range(4): print '$',

Print 3 rows

print 4 chars on each line

nested loops!

Output ?

(columns)

Comma suppresses newline

Page 8: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Without string multiplication…

Print 3 rows

print 4 chars on each line

nested loops!

tracking cols and rows!

(columns)

for row in range(3): for col in range(4): print '$', print

Comma suppresses newlineplain print produces newline

$ $ $ $

$ $ $ $

$ $ $ $

Page 9: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Tracking rows and columns

for row in range(3):

for col in range(4): print '$',

print

$ $ $ $

$ $ $ $

$ $ $ $

Output:

Page 10: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Variations!

for row in range( ):

for col in range( ): print '$',

print

def rectangle( width, height ): """ prints a rectangle of $ """

Page 11: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

"Quiz"

for row in range( 3 ):

for col in range( 7 ):

print ____________,

print

0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6

Fill in each block of code so that it will print the lines above it.

for row in range( 3 ):

for col in range( 7 ):

print ____________,

print

0 1 0 1 0 1 01 0 1 0 1 0 10 1 0 1 0 1 0

for row in range( 3 ):

for col in range( 7 ):

print ______________,

print

0 0 0 0 0 0 01 1 1 1 1 1 12 2 2 2 2 2 2

for row in range( 3 ):

for col in range( 7 ):

print _____________,

print

0 1 2 3 4 5 61 2 3 4 5 6 72 3 4 5 6 7 8

Page 12: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Beyond boxes…

## ## # ## # # ## # # # ## # # # # #

What code would print this “ASCII triangle” ? with height rows and width columns…

rows1

2

3

4

5

6

cols 2 3 4 5 6 7

1for row in range(1,7):

for col in range( ):

Page 13: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

A video alternative ?!?

http://www.asciimation.co.nz/

Page 14: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

A simpler web interface…

HTML HyperText Markup Language

<b>and the beautiful</b> <br>

<i>Tower of Pisa</i>

Page 15: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

A simpler web interface…

HTML

CGI

HyperText Markup Language

Common Gateway Interface ~ HTML forms

<b>and the beautiful</b> <br>

<i>Tower of Pisa</i>

<form action="test.cgi">

<input type="text" name="message" />

</form>

Page 16: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

A simpler web interface…

HTML

CSS AJAXJavascript

CGI

HyperText Markup Language

Common Gateway Interface ~ HTML forms

<b>and the beautiful</b> <br>

<i>Tower of Pisa</i>

<form action="test.cgi">

<input type="text" name="message" />

</form>

Others!

Page 17: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Web resources ~ on the Web

www.w3schools.com

started by

every technology - and acronym - you'd ever want to know…

Tim Berners-Lee

Page 18: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

This 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

• be sure that you take in the inputs you need

Checklist:

• watch out for conversions between ints and strings!

• make small changes and test as you go…

This is not webdesign!

Page 19: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Strings vs. language

strings

language / words / phrases

numbers / bits

ideas / meaning

computers

humans

Page 20: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

www.abenteuermedien.de/jabberwock/index.php

Hugh Loebner

How could a computer demonstrate intelligence?

Alan Turing

Turing Test

Confederates

$100,000 Loebner prize for passing the

Turing test, held each year in the

London Museum of

Science

Page 21: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Parsing for real

(S (S (NP Time flies) (VP like (NP an arrow))) , but (S (NP fruit) (VP flies (PP like (NP a banana)))) .)

Time flies like an arrow, but fruit flies like a banana. - Marx

http://www.link.cs.cmu.edu/link/submit-sentence-4.html

"<*time>""time" <*> N NOM SG @SUBJ

"<flies>""fly" <SVO> <SV> V PRES @+FMAINV"fly" N NOM PL @NPHR

"<like>""like" PREP @<NOM

"<an>""an" <Indef> DET CENTRAL ART

"<$2-NL>""<arrow>"

"arrow" N NOM SG @NPHR"<$\,>""<but>"

"but" CC @CC"<fruit_flies>"

"fruit_fly" N NOM PL @NPHR"<like>"

"like" PREP @<NOM"<a>"

"a" <Indef> DET CENTRAL ART"<banana>"

"banana" N NOM SG @<P"<$.>"

http://www.lingsoft.fi/cgi-bin/engcg

I like bananas, too!

parseTree #1

parseTree #2

Page 22: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Some 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

Page 23: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Loebner results

2003

2005

no human averaged less than 80

four humans and four machines participated

Page 24: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Some conversations…

You: Hi. you’re 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?

Page 25: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Some conversations…

You: Hi. you’re 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?

Page 26: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Some conversations…

You: Hi. you’re 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?

Page 27: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Some conversations…

You: Hi. you’re 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 don’t think wow am i talking free will vs. determinism, but I can’t

be 100% sure of that.

Page 28: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Computing with language

strings

language / words / phrases

numbers / bits

ideas / meaningopen

questions in AI …

how strings are

represented and stored

computing with language - recognizing English!

(3)

(2)

(1)

Page 29: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher

'Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.''Caesar cipher? I prefer Caesar salad.'

Page 30: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: encipher

encipher( 'gv vw dtwvg', 0 )

encipher( 'gv vw dtwvg', 1 )

returns

returns

'gv vw dtwvg'

'hw wx euxwh'

Page 31: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: encipher

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', 25 )

returns

returns

returns

returns

returns

returns

'gv vw dtwvg'

'hw wx euxwh'

'ix xy fvyxi'

'jy yz gwzyj'

'kz za hxazk'

'fu uv csvuf'

encipher( s , n )should return the string s with each alphabetic

character shifted/wrapped by n places in the

alphabet

. . .

Page 32: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

ASCII

American Standard Code for Information

Interchange== low-tech art

form!

Page 33: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

__ _ ,___,-'",-=-. __,-- _ _,-'_)_ (""`'-._\ `. _,' __ |,' ,-' __) ,- /. | ,'_,--' | -' _)/ `\ ,',' ,' ,-'_,` : ,' ,-' ,(,-( : ,' ,-' , _ ; / ,-._/`---' / / (____)(----. ) ,' / ( `.__, /\ /, : ;-.___ /__\/| | ,' `--. -,\ | : / \ .__/ \ (__ \ |_ \ ,`-, * / _|,\ \ ,' `-. ,'_,-' \ (_\,-' ,'\")--,'-' __\ \ / // ,'| ,--' `-. `-. `-/ \' | _,' `. `-._ / `--'/ \ -doh!- ,' | \ / | \ ,-' | / / | -'

ASCII

American Standard Code for Information

Interchange== low-tech art

form!

ASCII-betical order:

Homer

min

Marge

$42.42

[:^)]

bart

lisa max

Page 34: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

ASCII

ASCII is a table that tells the

computer how to represent characters as

#s

American Standard Code for Information Interchange

chr

ordconvert to number

convert to char.

You know, these

functions kind of look familiar!

Page 35: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

ASCII

ASCII is a table that tells the

computer how to represent characters as

bits!

8 bits = 1 byte

The SAME bits represent

integers, if the variable has type int instead of

str

American Standard Code for Information Interchange

type: str

type: int

00101010

00101010

bits

bits

name:

name:

value:

value:

'*'

42

Identical bits are stored in

each variable!The types

determine how to interpret the bits; the names don't matter at all…

Page 36: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

chr and ord

chr( n )

ord( c )

Input: an integer in range(255)

Input: a string of one character, c

abcdefghijklmnopqrstuvwxyz

ABCDEFGHIJKLMNOPQRSTUVWXYZASCII

VALUES

Output: a one-char. string of that ASCII value

Output: an integer, the ASCII value of cCONVERTERS

97 122

65 90

99 101 103 105 107 109 111 113 115 117 119

67 69 71 73 75 77 79 81 83 8785

for i in range(128): print 'i and chr(i) are', i, chr(i)

try this

Page 37: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

chr and ord

chr(66) is 'B'

ord('a') is 97

abcdefghijklmnopqrstuvwxyz97 122

65ABCDEFGHIJKLMNOPQRSTUVWXYZ

90

ASCII

VALUES

What is chr( ord('i')+13 )

What is chr( ord('W')+13 ) ?

How can we wrap this

around?

99 101 103 105 107 109 111 113 115 117 119

67 69 71 73 75 77 79 81 83 8785

Page 38: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Rot 13

def adv13( c ): """ rotates c by 13 chars, "wrapping" as needed NON-LETTERS DO NOT CHANGE! """ if 'a' <= c <= 'z': new_ord = ord(c) + 13 if new_ord <= ord('z'):

return chr(new_ord) # no wrapping else:

return

elif

else:

How would you rotate an entire string?

Page 39: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: decipher

>>> decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.')'Caesar cipher? I prefer Caesar salad.'

>>> 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.'

But how ?

>>> decipher('gv vw dtwvg')

Page 40: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: decipher

>>> decipher('gv vw dtwvg')

Caesar Brutus

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!

gv vw dtwvghw wx euxwhix xy fvyxijy yz gwzyjkz za hxazkla ab iybalmb bc jzcbmnc cd kadcnod de lbedope ef mcfepqf fg ndgfqrg gh oehgrsh hi pfihsti ij qgjituj jk rhkjuvk kl silkvwl lm tjmlwxm mn uknmxyn no vlonyzo op wmpozap pq xnqpabq qr yorqbcr rs zpsrcds st aqtsdet tu brutefu uv csvufall 26 possibilities

Score for "Englishness

"?up to you…

Page 41: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: decipher

>>> decipher('gv vw dtwvg')'od de lbedo'

Caesar Brutus

Strategy using max:(1) consider all

possible answers(2) give them each a score(3) use our techniques with max

[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']all 26 possibilities

won't always be correct!

number-of-vowels score

Page 42: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: decipher

>>> decipher('gv vw dtwvg')'et tu brute'

Caesar Brutus

Strategy using max:(1) consider all

possible answers(2) give them each a score(3) use our techniques with max

[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']all 26 possibilities

letter- probability

scorenot always correct,

but better!

Page 43: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

The caesar cipher problem offers lots

of freedom…

Think a bit about how you'll approach

it!

Questions / Lab time…

Page 44: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: decipher

>>> decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.')'Caesar cipher? I prefer Caesar salad.'

>>> 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.'

But how to do this… ?

>>> decipher('Uifz xpsl ju pvu xjui b qfodjm!')?

>>> decipher('gv vw dtwvg') ?

Page 45: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: decipher

>>> decipher('gv vw dtwvg')???

Caesar Brutus

Strategy using max:(1) consider all

possible answers(2) give them each a score(3) use our techniques to get max

gv vw dtwvghw wx euxwhix xy fvyxijy yz gwzyjkz za hxazkla ab iybalmb bc jzcbmnc cd kadcnod de lbedope ef mcfepqf fg ndgfqrg gh oehgrsh hi pfihsti ij qgjituj jk rhkjuvk kl silkvwl lm tjmlwxm mn uknmxyn no vlonyzo op wmpozap pq xnqpabq qr yorqbcr rs zpsrcds st aqtsdet tu brutefu uv csvufall 26 possibilities

Score for "Englishness

"?up to you…

Page 46: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: decipher

>>> decipher('gv vw dtwvg')'od de lbedo'

Caesar Brutus

Strategy using max:(1) consider all

possible answers(2) give them each a score(3) use our techniques with max

[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']all 26 possibilities

Score for "Englishness

"?up to you…

won't always be correct!

Page 47: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: decipher

>>> decipher('gv vw dtwvg')'et tu brute'

Caesar Brutus[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']all 26 possibilities

Score for "Englishness

"?up to you…

Strategy using max:(1) consider all

possible answers(2) give them each a score(3) use our techniques to get max

depends on how you score

Page 48: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Caesar Cipher: encipher

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 )

encipher( 'gv vw dtwvg' , 25 )

returns

returns

returns

returns

returns

returns

returns

'gv vw dtwvg'

'hw wx euxwh'

'ix xy fvyxi'

'jy yz gwzyj'

'kz za hxazk'

'la ab iybal'

'fu uv csvuf'

encipher( s , n )should return the string s with each alphabetic

character shifted/wrapped by n places in the

alphabet

Brutus won't mind if I use his salary to bring Justin Timberlake to the Colosseum next month...

Historically bad decision #43:

Page 49: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

CHR and ORDOrchard Field

Page 50: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

CHR and ORDChateauroux

Page 51: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Mutable vs. Immutable data

Changeable types:

dictionary

Unchangeable types:

list

tuple

string

int

float

bool

What's a dictionary?

I guess I'll have to look it up!

x

42

x = 42

Page 52: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Reference vs. Value

Changeable types:

dictionary

Unchangeable types:

list

tuple

string

int

float

bool

LL[0] L[1] L[2]

Reference,Pointer,

id

L = [5,42,'hi']

x5 42 'hi'

42

x = 42

Page 53: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

“Pass 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

Page 54: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

7

“Pass 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

PASSBY VALUE

“Pass by value” means that data is copied when sent to a method

42

Page 55: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Passing 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 """ fav[0] = 42 fav[1] = 42

What gets passed by value here?

favL[0] L[1]

5 42

fav

Page 56: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Passing 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 """ fav[0] = 42 fav[1] = 42

favL[0] L[1]

5 42

fav

can change data

elsewhere!

The reference is copied!

Page 57: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

The conclusion

You can change the contents of lists in functions that take those lists as input.

Those changes will be visible everywhere.

(actually, lists or any mutable objects)

(immutable objects are safe, however)

Page 58: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Views of the world

Engineers think their equations approximate reality.

Page 59: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Views of the world

Engineers think their equations approximate reality. Physicists think reality approximates their equations.

Page 60: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Views of the world

Engineers think their equations approximate reality. Physicists think reality approximates their equations.

Mathematicians don't care.

Page 61: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Views of the world

Axioms

Definitions

Creating structure from a few simple

facts...

Creating structure from a few simple

actions ...

if/else

while

for

arithmetic operations

variablesarrays

Proof Algorithm

Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care. 

Page 62: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Lists’ flexibility

Lists can hold ANY type of dataA = [ 42., 75., 70. ] 42.0 75.0 70.0

float float floatlistA

they don’t have to be horizontal lists!

Page 63: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

42.0

75.0

70.0

double

double

double

listAthey don’t

have to be horizontal lists!

Lists’ flexibility

Lists can hold ANY type of dataA = [ 42., 75., 70. ] 42.0 75.0 70.0

float float floatlistA

Page 64: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Lsits’ flexibility

Lists can hold ANY type of data

42.0 75.0 70.0double double doublelist

A

42 7 -11int int intlist

A

“go” “red” “sox!”

String String StringlistA

Page 65: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

2d lists or arrays

Lists can hold ANY type of data -- including lists !

listA

A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]

Page 66: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

listA

2d arrays

list

list

list

A[0]

A[1]

A[2]

Lists can hold ANY type of data -- including lists !

A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]

Page 67: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

listA

Jagged arrays

list

list

list

A[0]

A[1]

A[2]

Lists can hold ANY type of data -- including lists !

A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]

Rows within 2d arrays need not be the same length…

Page 68: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

listA

list

list

list

A[0]

A[1]

A[2]

Lists can hold ANY type of data -- including lists !

A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]

Rows within 2d arrays need not be the same length…

We will not use jagged arrays

at least in hw 10

Page 69: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Rectangular arrays

listA

list

list

list

A[0]

A[1]

A[2]

How many rows does A have, in general ?How many columns does A have, in general ?

What does each component of A[1][2] mean ?

A[1][2] = 42

A[2][3]

A[0][0]

Page 70: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Creating 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

Page 71: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Displaying a 2d array

import csplot

B = create2dArray( 10, 10 )

csplot.show( B )

B[1][8] = 1 # set this item to 1

csplot.show( B )

Be sure to download the newest version of this file…

nearby coordinates…

Page 72: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 2 -- “Life”

Evolutionary rules

Grid World

• Everything depends on a cell’s eight neighbors

red cells are alive

white cells are empty

• Exactly 3 neighbors give birth to a new, live cell!• Exactly 2 or 3 neighbors keep anexisting cell alive• Any other number of neighbors killthe central cell (or keep it dead)

John Conway

Page 73: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 2 -- Life

Evolutionary rules

Grid World

• Everything depends on a cell’s eight neighbors

red cells are alive

white cells are empty

• Exactly 3 neighbors give birth to a new, live cell!• Exactly 2 or 3 neighbors keep anexisting cell alive• Any other number of neighbors killthe central cell (or keep it dead)

Page 74: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 2 -- Life

Evolutionary rules

Grid World

• Everything depends on a cell’s eight neighbors

red cells are alive

white cells are empty

• Exactly 3 neighbors give birth to a new, live cell!• Exactly 2 or 3 neighbors keep anexisting cell alive• Any other number of neighbors killthe central cell (or keep it dead)

Page 75: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 2 -- Life

Evolutionary rules

Grid World

• Everything depends on a cell’s eight neighbors

red cells are alive

white cells are empty

• Exactly 3 neighbors give birth to a new, live cell!• Exactly 2 or 3 neighbors keep anexisting cell alive• Any other number of neighbors killthe central cell (or keep it dead)

life out there...

Keep going!

Page 76: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 2 -- Creating Life

0 1 2 3 4 50 1 2 3 4 5

0

1

2

3

4

5

0

1

2

3

4

5

B = update( B )

old generation or "board" new generation or "board"

Page 77: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 2 -- Creating Life

0 1 2 3 4 50 1 2 3 4 5

0

1

2

3

4

5

0

1

2

3

4

5

B = update( B )

old generation or "board" new generation or "board"

Page 78: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 2 -- Details

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.html

life out there...

B = update( B )

old generation or "board" new generation or "board"

Page 79: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 2 -- to and beyond!

• Are there stable life configurations?

• Are there oscillating life configurations?

• Are there self-propagating life configurations?

"rocks"

"plants"

"animals"

period 3

period 2

Page 80: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Problem 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?

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

Google for GOLLY, Game of Life

• Are all feasible configurations reachable?

Page 81: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

Lab & Homework…

Page 82: IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"

import sys

for i in range(4): for j in range(8): sys.stdout.write('#') print

software object representing the screen's standard output

Without extra spaces…

Output

################################

method (function) that prints only its input