Lecture 2: Python and Picobot!
Download
Report
Transcript Lecture 2: Python and Picobot!
Welcome (back) to IST 338 !
Wow – I see the
resemblance
Average of
these two?
Alien
Wally
Homework 0
due Sun. night (11:59pm)
Problem 0:
Already complete!
Problem 1:
Four-fours program: Can be done for lab...
Problem 2:
Rock-paper-scissors program (Maybe done already!)
Problems 3-5: Picobot! empty room (3) maze (4) + extra (5)
Problem 6: Reading + response…
Welcome (back) to IST 338 !
Wow – I see the
resemblance
Picobot!
Alien
Wally
Homework 0
due Sun. night (11:59pm)
Problem 0:
Already complete!
Problem 1:
Four-fours program: Can be done for lab...
Problem 2:
Rock-paper-scissors program (Maybe done already!)
Problems 3-5: Picobot! empty room (3) maze (4) + extra (5)
Problem 6: Reading + response…
Welcome
back (from)
Baltimore!
world's expert on badgebased incentive systems
for education…
Extreme
RPS!?
They call that an alien?
Spock mind-melds
three-eyed aliens!
Provably.
http://www.youtube.com/watch?v=iapcKVn7DdY
http://www.youtube.com/watch?v=yuEZEyDdmvQ
Last time…
CS != Programming
What about the Python
programming language ?
Python ?
One possible relationship... vs.
Python !
One possible relationship... vs.
Happy co-existence…
It can even be comfy!
Exclusive Choices
if ... elif ... else
if perc > .95:
print 'A'
elif perc > .90:
print 'A-'
4 mutually exclusive blocks
elif perc > .70:
print 'Pass'
else:
print 'Aargh!'
elif and else are optional
in a single control structure
When using
if . elif … . else
at most one block will run:
the first whose test is True.
If all fail, the else will run
What's the difference?
mutually exclusive blocks
nonexclusive blocks
if perc > .95:
print 'A'
if perc > .95:
print 'A'
elif perc > .90:
print 'A-'
if perc > .90:
print 'A-'
elif perc > .70:
print 'Pass'
if perc > .70:
print 'Pass'
What if perc == .99 ? (How would we set it?)
How many separate control structures does each side have?
What will this do?
Does this program print the
correct RPS result this time?
Does it always?
comp = 'rock'
user = 'paper'
if comp == 'paper' and user == 'paper':
print 'We tie. Try again?'
elif comp == 'rock':
if user == 'scissors':
print 'I win! *_*'
else:
print 'You win. Aargh!'
# of BLOCKS ?
try it: RPS example #1
# of TESTS ?
# of Control Structures ?
Counting...
comp = 'rock'
user = 'rock'
# of BLOCKS
# of TESTS
if comp == 'rock':
if user == 'paper':
print 'I win *_*!'
elif user == 'scissors':
print 'You win.'
Printing…
else:
print 'Tie.'
print 'Ties go to the runner.'
print ' - and I am running!'
try it: RPS example #2
... what if the else were indented?
What does
this print?
How many possible “input cases” are there?
For how many is this program correct?
comp = 'rock'
user = 'rock'
if comp == 'rock':
print 'I win *_*!'
if user == 'paper':
print 'You win.'
else:
print 'An awful tie'
try it: RPS example #3
How many possible “input cases” are there?
For how many is this program correct?
What does
this print?
comp
if comp == 'rock':
print 'I win *_*!'
if user == 'paper':
print 'You win.'
else:
print 'An awful tie'
'scissors' 'paper'
user
'paper' 'scissors'
'rock'
'rock'
comp = 'rock'
user = 'rock'
… how efficiently can RPS be represented by program?
• Fewest number of blocks?
• Fewest number of tests?
This week: hw0pr1
shell prompt > or $
Python prompt >>>
Python source code,
a plain-text file
(here, edited by the Sublime text editor)
shell or commandline or terminal
(the execution environment)
get Python running on your own machine
Commanding the command-line
in Windows'
plain cmd
Mac/Linux/Windows Powershell
cd .
pwd
print working directory (current path)
dir
ls
list
cd
cd
change directory
..
.
..
.
(current directory)
shortcut for "directory above"
shortcut for "current directory"
move
mv
move
(source/from) (destination/to)
copy
cp
copy
(source/from) (destination/to)
mkdir
mkdir
make directory
(name of new dir)
Desktop
Mental model .. .
CS5
CS5
start
pico3.txt
pico4.txt
(folder)
(folder)
(file)
(file)
usual picture ~ a window into the underlying structure
a couple of shortcuts…
Up arrow
• Brings back previously-typed commands
• Down arrow ~ scrolls the other way
Tab key
• Completes the file or folder name you start
• Cycles through possibilities (older versions would pause…)
Desktop
Mental model .. .
CS5
(folder)
$ cd ..
cd
$ pwd
dir
$ ls
CS5
$ cd C
start
$ mkdir BotFiles
$ cd B
copy
$ cp ../pico3.txt
$
move
$ mv p
..
.
pico3.txt
pico4.txt
(folder)
(file)
(file)
Desktop
Mental model .. .
pico3.txt
CS5
(file)
(folder)
$ cd ..
cd .
$ pwd
dir
$ ls
CS5
$ cd C
end
$ mkdir BotFiles
pico4.txt
BotFiles
(folder)
(file)
(folder)
$ cd B
copy
$ cp ../pico3.txt
pico3.txt
$
move
$ mv p
.
BotFiles
..
(folder)
(file)
Try it!
Desktop
Draw the changes these shell
commands will make…
CS5
(folder)
$ mkdir Week0
$ cd W
move
$ mv ../h
CS5
.
$ cd ..
start
hw0pr0.py
hw0pr1.py
(folder)
(file)
(file)
$ mkdir Week1
$ cd W
copy
$ cp ../h
.
$
$
$
Extra #2: What line here would copy the hw0pr0.py
file to the current location? See if you can use tabs…
Extra #1: What is the current folder here?
Not to mention…
python
where
which
type
cat
tree
find .
if you don't want to type the full
path: c:\python27\python
add ;c:\python27
ping
may need
to install
curl or wget
This is a window into the directory tree…
XKCD's
perspective…
Command line?!?
The Operating System's Window Manager
Consume
Computational interactions (files, folders, data…)
Compose
Physical interactions (arithmetic and storage)
Crazy!
not really fair…
Command line?!?
The Operating System's Window Manager
Consume
Computational interactions (files, folders, data…)
Compose
Physical interactions (arithmetic and storage)
Commoditized!
Useful?
Consuming
Composing
Useful?
Consuming
Everything at the command line is
automatable and programmable…
Composing
Useful!
Consuming
Everything at the command line is
automatable and programmable…
Composing
Another language already?
Python
Picobot!
General-purpose language
you might see
50% by the end
of the term
even then, <1% of its libraries!
Picobot
Special-purpose language
you'll see 100% in
the next 10 minutes
The Picobot simulator
www.cs.hmc.edu/picobot
HW problems 3 and 4: Picobot!
Goal: full-room
coverage with only
local sensing…
walls
Picobot
area not
covered
(yet!)
area
already
covered
Inspiration?
Picobot
can't tell…
HW problems 3 and 4: Picobot!
Goal: full-room
coverage with only
local sensing…
walls
Picobot
area not
covered
(yet!)
area
already
covered
Roomba!
can't tell "vacuumed"
from "unvacuumed" area
Picobot
can't tell…
Surroundings
Picobot can only sense things
directly to the N, E, W, and S
N
E
W
S
For example, here its surroundings are
NxWx
N E W S
Surroundings are
always in NEWS order.
What are these surroundings?
Surroundings are
always in NEWS order.
N E W S
NxWx
Wow - this one is
disgusting!
Surroundings
How many distinct
surroundings are there?
N
E
W
S
Surroundings
How many distinct
surroundings are there?
N
E
W
24 == 16 possible
S
xxxx
Nxxx
xExx
xxWx
xxxS
NExx
NxWx
xEWx
xExS
xxWS
NEWx
NExS
NxWS
xEWS
NxxS
NEWS
(won’t happen)
State
I am in state 0.
My surroundings
are xxWS.
0
Picobot's memory is a single
number, called its state.
State is the internal context of a
computation, i.e., the subtask.
Picobot always starts in state 0.
State and surroundings represent
everything Picobot knows about the world
self-contained
but not simplistic
Rules
I am in state 0.
My surroundings
are xxWS.
Picobot acts through a set of rules:
state
0
Notes
surroundings
xxWS
direction
new state
N
0
Picobot checks its rules from the top each time.
When it finds a matching rule, that rule runs.
Rules
I am in state 0.
My surroundings
are xxWS.
Picobot acts through a set of rules
Each rule expresses your intent for Picobot!
state
0
surroundings
xxWS
If I'm now in state
0 seeing xxWS,
direction
new state
N
0
Then I move North, and
"change" to state 0.
Wildcards
I only care about NORTH being EMPTY
Asterisks * are wild cards.
They match walls or empty space:
state
surroundings
0
x***
direction
new state
N
0
EWS may be wall or empty space
N must be empty
(A) A rule that sends Picobot to the North as far as it can go:
state
0
surroundings
x***
direction
new state
->
N
0
->
W
1
->
S
1
(B) What does this next rule do?
0
N***
(C) What about this one?
1
***x
(D) How could we stop at the bottom and return up through the same column?
One rule to
rule them all?
Precious!
The Rule:
One step per rule
Picobot's world
How many times does rule (A) run? ___________________
How many times does rule (B) run? ___________________
P
How many times does rule (C) run? ___________________
How many times does rule (D) run? ___________________
Where does Picobot stop? Why?
Try it!
Extra! What additional rules would be needed in order to ensure that Picobot
fully explores the empty room (not this crazy room) from any starting spot?
(this is problem 3…)
Picobot hw problems
problem 3
problem 4
Extra! (#5)
Your rules must work regardless of
Picobot's starting position… !
Picobot's revenge!
problem 3
problem 4
Extra! (#5)
Your rules must work regardless of
Picobot's starting position… !
Be Picobot!
(1) How many times does the 1st rule run? _________
(2) How many times does the 2nd rule run? _________
(3) How many times does the 3rd rule run? _________
A more intricate picobot world
(4) In English, what does state 0 do?
(5) How could this help clear the empty room?
… just a thought experiment
"Run" this Picobot program:
top of stalagmite
P
state
surr
0
0
0
***x
*x*S
*E*S
->
->
->
dir
newstate
S
E
X
0
0
1
(6) Add 2-3 rules that will, in state 1, get Picobot to the top of the stalagmite, then stop.
(Extra!) What approach could get Picobot to travel around the whole outer edge?
1st rule
2nd rule
3rd rule
Maze strategies?
Maze strategies?
Right Hand Rule
Why might this
be difficult for
Picobot?
Maze strategies?
Right Hand Rule
facing
State 0
State 1
State 2
State 3
We'll need to use
state to represent
the direction
Picobot is facing.
Suppose Picobot wants to traverse a maze
with its right hand always on the wall.
We use state 0 to represent that
“Picobot is facing North”
(A) Here is a single rule that tells Picobot:
If you're facing N with a wall at right and space ahead then go forward”
0
xE**
->
N
0
0
(A)
(B) Let's write a single rule that tells Picobot:
“If you're facing North and lose the wall, then get over to the wall now!”
0
0
->
(B)
(C) (for HW) Write 1 or 2 rules to tell Picobot to do the right thing if it hits a dead end.
Repeat this IDEA for all four states, representing all four facing directions.
0
(C)
Problem 6 this week…
Typically an article on CS or an application...
Submit a one-paragraph response
Small part (5 pts)
A few sentences that raise or
address questions, using the
article as a guide.
5 – insightful, careful
4 – thoughtful
3 – complete, on topic
0-2 – less than complete
This week's article might
not seem like CS at first…
and I thought my
language was alien!
Seventy years ago, in 1940, a popular
science magazine published a short
article that set in motion one of the
trendiest intellectual fads of the 20th
century. At first glance, there seemed
little about the article to augur its
subsequent celebrity. Neither the title,
“Science and Linguistics,” nor the
magazine, M.I.T.’s Technology
Review, was most people’s idea of
glamour. And the author, a chemical
engineer who worked for an
insurance company and moonlighted
as an anthropology lecturer at Yale
University, was an unlikely candidate
for international superstardom. And
yet Benjamin Lee Whorf let loose an
alluring idea about language’s power
over the mind, and his stirring prose
seduced a whole generation into
believing that our mother tongue
restricts what we are able to think.
and I thought my
language was alien!
Seventy years ago, in 1940, a popular
science magazine published a short
article that set in motion one of the
trendiest intellectual fads of the 20th
century. At first glance, there seemed
little about the article to augur its
subsequent celebrity. Neither the title,
“Science and Linguistics,” nor the
magazine, M.I.T.’s Technology
Review, was most people’s idea of
glamour. And the author, a chemical
engineer who worked for an
insurance company and moonlighted
as an anthropology lecturer at Yale
University, was an unlikely candidate
for international superstardom. And
yet Benjamin Lee Whorf let loose an
alluring idea about language’s power
over the mind, and his stirring prose
seduced a whole generation into
believing that our mother tongue
restricts what we are able to think.
CS ~ complexity science
problem 3
problem 4
Extra! (5)
Shortest Picobot
program:
Shortest Picobot
program:
Shortest Picobot
program:
6 rules
8 rules
? rules
complexity is deeper than simply the # of rules…
As you head northwards…
You are not alone!
I can attest to that!
Tutoring hours all weekend in the LAC lab also, weekday evenings @ LAC lab, too
Lead on!
I will follow.
Each week's lab…
0) Feel free to stay for the final hour…
1) … to work on the problems
2) Also, you can use the CS5 lab and
tutoring times, linked from the site
Encouraged: bring your laptop
Tutoring schedule…
Seek help!
every day there are tutoring hours in the LAC computer lab
Linde Activities Center
this link:
HMC office + tutoring locations
Tutoring hours
are in the Linde
Activities Center
computer lab
(LAC lab)
Enter through Olin
building
west-side entrance
Beckman B163
~ office location
Each week's lab…
0) Feel free to stay for the final hour…
1) … to work on the problems
2) Also, you can use the CS5 lab and
tutoring times, linked from the site
Encouraged: bring your laptop
The shell ~ yet another language…
Programming as learning a foreign language
1) Expect it to be different!
2) You don't need to know all the details
No one does!
3) Immersion ~ Experimentation
4) Key benefit == new mindset
go/went
It's all language …but it's not all languages
Say hello.
Cow
Fibonacci
Whitespace
Hello world
Var'aq
Fibonacci
Malbolge
Hello world
0 lI'moH A cher
1 lI'moH B cher
A cha' B cha'
18 {
A B boq
latlh
cha'
B "A" cher
"B" cher
} vangqa'
MoO moO MoO mOo MOO OOM MMM moO moO
MMM mOo mOo moO MMM mOo MMM moO moO
MOO MOo mOo MoO moO moo mOo mOo moo
Piet
Fibonacci
(=<`$9]7<5YXz7wT.3,+O/o'K%$H"'~D|#z@b=`{
^Lx8%$Xmrkpohm_Ni;gsedcba`_^]\[ZYXWVUTSR
QPONMLKJIHGFEDCBA@?>=<;:9876543s+O<oLm
Fortunately, CS 5 does NOT goto the fringe of the programming-language universe !
Who are you
calling fringe?
The foreign language of python…
syntax
semantics
intent
How it looks
What it does
What it should do
name = raw_input('Hi... what is your name? ')
print
# prints a blank line
if name == 'Eliot' or name == 'Ran':
print 'I\'m "offline." Try later.'
elif name == 'Zach':
# is it Zach?
print 'Zach Quinto...?', 'No?', 'Oh.'
else:
# in all other cases...
print 'Welcome', name, '!'
my_choice = random.choice( [ 'R','P','S' ] )
print 'My favorite object is', my_choice, "!"
The foreign language of python…
syntax
semantics
intent
How it looks
What it does
What it should do
name = raw_input('Hi... what is your name? ')
print
# prints a blank line
if name == 'Eliot' or name == 'Ran':
print 'I\'m "offline." Try later.'
elif name == 'Zach':
# is it Zach?
print 'Zach Quinto...?', 'No?', 'Oh.'
else:
# in all other cases...
print 'Welcome', name, '!'
my_choice = random.choice( [ 'R','P','S' ] )
print 'My favorite object is', my_choice, "!"
The foreign language of python…
syntax
semantics
intent
How it looks
What it does
What it should do
name = raw_input('Hi... what is your name? ')
print
# prints a blank line
if name == 'Eliot' or name == 'Ran':
print 'I\'m "offline." Try later.'
elif name == 'Zach':
# is it Zach?
print 'Zach Quinto...?', 'No?', 'Oh.'
else:
# in all other cases...
print 'Welcome', name, '!'
my_choice = random.choice( [ 'R','P','S' ] )
print 'My favorite object is', my_choice, "!"
The foreign language of python…
syntax
semantics
intent
How it looks
What it does
What it should do
name = raw_input('Hi... what is your name? ')
print
# prints a blank line
if name == 'Eliot' or name == 'Ran':
print 'I\'m "offline." Try later.'
elif name == 'Zach':
# is it Zach?
print 'Zach Quinto...?', 'No?', 'Oh.'
else:
# in all other cases...
print 'Welcome', name, '!'
my_choice = random.choice( [ 'R','P','S' ] )
print 'My favorite object is', my_choice, "!"
a graphical view of a
program's semantics
‘Eliot’
'Zach'or
‘Ran’
Zach
Eliot
or Ran
'Chris'
Chris
Zach
What about me?
The foreign language of python…
syntax
semantics
intent
How it looks
What it does
What it should do
www.theonion.com
How
Python
looks!
• how punctuation is used
• the language keywords used
• use of whitespace
• peculiarities of formatting
• how behavior is affected …
The foreign language of python…
syntax
semantics
intent
How it looks
What it does
What it should do
www.theonion.com
How
Python
looks!
• how punctuation is used
• the language keywords used
• use of whitespace
• peculiarities of formatting
• how behavior is affected …
The challenge of programming…
syntax
semantics
intent
How it looks
What it does
What it should do
www.theonion.com
humantyped
input
machineproduced
output
?
humandesired
output
Syntax challenge!
import random
(1) Find and correct as many
errors as you can here…
(2) This line is doing three things… what are they?
user = raw_input( "Choose your weapon! " )
comp = random.choice( ['rock','paper','scissors") ]
print 'user (you) chose:', 'user'
print 'comp (me!) chose:'
comp
if user == rock and comp = 'paper'
print 'The result is, YOU LOSE!'
print 'unless you're a CS 5 grader, then YOU WIN!'
(3) What are the 6-7 punctuation marks used in more than one way here?
Syntax challenge!
every block of code must line up!
import random
(1) Find and correct as many
errors as you can here…
(2) This line is doing three things… what are they?
(1) prints the "weapon" prompt
(2) gets user's input from the kbd
(3) assigns that input to the variable user
set-equals always uses
ONE equals sign
user = raw_input( "Choose your weapon! " )
comp = random.choice( ['rock','paper','scissors'] )
print 'user (you) chose:', user
match brackets
and quotes !
print 'comp (me!) chose:', comp
test-equals uses TWO equals signs
The comma prints a space and
does NOT go to the next line.
if user == rock and comp == 'paper':
every
block of
code must
line up!
a colon starts a new block
print 'The result is, YOU LOSE.'
print 'unless you\'re a CS 5 grader, then YOU WIN!'
a backslash handles special characters
flattering - or flouting graders is encouraged!
(3) Punctuation characters used in more than one way: ().'=,:
Tear off that page
Pass those to the aisles + Southward…
only 1 "quiz" is needed per pair
… then turn back into the packet
The Rule: One step per rule
One rule to
rule them all?
Using the ruleset above…
Picobot's world
This is precious!
How many times does rule (A) run? ___________________
How many times does rule (B) run? ___________________
How many times does rule (C) run? ___________________
How many times does rule (D) run? ___________________
P
In a phrase, what do state 0 and state 1 do?
Where does Picobot stop? Why?
Extra! What additional rules would be needed in order to ensure that Picobot fully explores the empty room
(not this crazy room) from any starting spot? (this is problem 3…)
Quiz!
Solve this on the back page first…
Pico-reminders…
Picobot!
Picobot follows a set of rules:
state
surroundings
direction
new state
0
x***
->
N
0
0
N***
->
W
0
Picobot takes ONE STEP each time a rule runs.
The programmer uses states to create behaviors!
Each week's lab…
0) Find the lab! Sign in…
1) Get Python running…
2) Run and submit a file…
Encouraged: bring your laptop
in Platt
Evening lab? You can
pickup a keycard from
HMC's F&M dep't.
Shanahan
coffee
Map to
CS Lab
Laptop? Bring it!
Koi
Edwards
Macalister
Pryne
cool machines - drills, lathes, etc.
other keyboard-free machines
Your account for
CS submissions:
Physicists, chemists
& other parenthesisneeding individuals,
B102
B100
Galileo
Beckman
CS Hallway and Labs
passwd:
Big
Beckman
(B126)
or Olin
B105
to Olin (Bio + CS)
Biologists, bees,
spiders and other
arachnophiles
Choices, choices!
Suppose we set the value of perc to 0.91...
perc = 0.91
if perc >
print
elif perc
print
elif perc
print
else:
print
# of BLOCKS here:
# of TESTS here:
0.95:
'A'
> 0.90:
'A-'
> 0.70:
'Pass'
'Aargh!'
What will this program print,
if perc is 0.91?
in a fiercely contested game of RPS...
Does this program print the
correct RPS result this time?
Does it always?
comp = 'rock'
user = 'paper'
if comp == 'paper' and user == 'paper':
print 'We tie. Try again?'
elif comp == 'rock':
if user == 'scissors':
print 'I win! *_*'
else:
print 'You win. Aargh!'
# of BLOCKS here:
# of TESTS here:
Choices, choices!
Suppose that the value of perc is 0.91...
perc = 0.91
if perc >
print
elif perc
print
elif perc
print
else:
print
0.95:
'A'
> 0.90:
'A-'
> 0.70:
'Pass'
'Aargh!'
# of BLOCKS here:
# of TESTS here:
What will this program print,
if perc is 0.91?
Choices, choices!
perc = 0.80
perc = 0.80
if perc >
print
elif perc
print
elif perc
print
else:
print
if perc >
print
elif perc
print
elif perc
print
else:
print
0.95:
'A'
> 0.90:
'A-'
> 0.70:
'Pass'
'Aargh!'
0.00:
'Aargh!'
> 0.70:
'Pass'
> 0.90:
'A-'
'A'
What does each of these programs print out, if perc is 0.8?
What value of perc gives an 'A-' on the right?
How can you get a better grade on the right than the left?
What's the difference?
nonexclusive blocks
mutually exclusive blocks
if perc > .95:
print 'A'
if perc > .95:
print 'A'
elif perc > .90:
print 'A-'
if perc > .90:
print 'A-'
elif perc > .70:
print 'Pass'
if perc > .70:
print 'Pass'
What if perc == .99 ?
How many separate control structures does each side have?
in a fiercely contested game of RPS...
Does this program print the
correct RPS result this time?
Does it always?
comp = 'rock'
user = 'paper'
if comp == 'paper' and user == 'paper':
print 'We tie. Try again?'
elif comp == 'rock':
if user == 'scissors':
print 'I win! *_*'
else:
print 'You win. Aargh!'
# of BLOCKS here:
# of TESTS here:
First: Pick up your CS lab login + password and look up your submission-site login
Pair up with someone nearby – and answer these questions together…
"Quiz"
Name ______________________
Name
______________________
Your favorite __________ is ____________.
Your favorite __________ is ____________.
Your least favorite ____________ is ____________.
Your least favorite ____________ is ____________.
What is something non-Claremont-collegey you have in common?
Then, try these Python q's:
(0) How many tests and blocks are here?
(1) What output does this code print?
comp = 'rock'
user = 'rock'
if comp == 'rock':
if user == 'paper':
print 'I win *_*!'
elif user == 'scissors':
print 'You win.'
else:
print 'Tie.'
(2) As written, what output
does this print?
comp = 'rock'
user = 'rock'
if comp == 'rock':
print 'I win *_*!'
if user == 'paper':
print 'You win.'
else:
print 'Tie.'
(3) Change these inputs
to produce a completely
correct RPS output here.
(4) How many RPS cases are
there? How many of them does
this code handle correctly?
(5) What is the smallest
number of tests and blocks of
code you'll need to correctly
handle a full game of RPS?
(Extra) What if it were RPS-5,
with Lizard and Spock? RPS-101?
Counting...
comp = 'rock'
user = 'rock'
# of BLOCKS
# of TESTS
if comp == 'rock':
if user == 'paper':
print 'I win *_*!'
elif user == 'scissors':
print 'You win.’
else:
print 'Tie.'
print 'Ties go to the runner!'
print ' - and I'm running.'
... what if the else were indented?
What does
this print?
How many possible “inputs” are there?
For how many is this program correct?
comp = 'rock'
user = 'rock'
if comp == 'rock':
print 'I win *_*!‘
if user == 'paper':
print 'You win.‘
else:
print 'An awful tie'
… how efficiently can RPS be represented?
• Fewest number of blocks?
• Fewest number of tests?
What is CS?
'HUMAN'
'CHIMPANZEE'
How can it be done?
How well can it be done?
Can it be done at all?
Can you solve the problem?
Can you create a process to
solve such problems?
What is the Longest Common
Subsequence between 2 strings?
biology's string-matching problem, "LCS"
'CGCTGAGCTAGGCA...'
'ATCCTAGGTAACTG...'
+109more
Eye oneder if this haz
othur aplications?
Feels like home!
What is CS?
How can it be done?
How well can it be done?
Can it be done at all?
How quickly can you find
a solution?
Is your solution the "best"
possible?
How much work is needed
to simulate N stars?
physics's "N-body" problem
official alien of CS 5 Gold
Why here?
official mascot of CS 5 Black
Placement based on experience, not ability:
• Gold is for students mostly new to CS,
• Black for students with a good amount of previous CS
• Tradeoff: more careful coverage – a few more applications
• Green is for those who'd like biology-flavored CS!
official turtle
of CS 5 Green
• All of Gold Black and Green will prepare you well for
• CS 60
• Future computational work
• A happy and fulfilling life :-)
switching?
… and who is this Mudder, 35 years later?
Jon “Dean” Jacobsen, 1979
What is programming ?
Programming as "recipe-writing"
vs.
Programming as learning a foreign language
What is programming ?
Programming as "recipe-writing"
vs.
Programming as learning a foreign language
1) Expect it to be different!
2) Don’t feel you need to memorize it
3) Immersion == Experimentation
go/went
Why CS 5 ?
2
Broad, constructive tour in CS
1
recursion
3
simulation
4
design
5
Python Programming?
in pictures?
Python Programming?
One possibility...
Python Programming?
One possibility...
Happy co-existence! It
can even be comfy!
Python Programming?
Python (language) and IDLE (editor)
open-source, free, powerful, and common
We will be using Python 2.7.3
~ the “Enthought” distribution
Plain old Python 2.7.x is certainly OK, too.
Don't get 3.1 or 3.x!
"Editor" and "Shell"
Editor (IDLE): typing code
Here, you can save and change
programs. Hitting F5 runs your
program over in the shell
Shell window: running code
Here, you can try things out at
the command prompt >>>
The foreign language of python…
name = raw_input('Hi... what is your name? ')
print
# prints a blank line
if name == 'Eliot' or name == 'Ran':
print 'I\'m "offline." Try later.'
elif name == 'Zach':
# is it Zach?
print 'Zach Quinto...?', 'No?', 'Oh.'
else:
# in all other cases...
print 'Welcome', name, '!'
my_choice = random.choice( [ 'R','P','S' ] )
print 'My favorite object is', my_choice, "!"
Lab 0 – Tues. / Wed. is to run and submit “this” program
The foreign language of python…
syntax
semantics
intent
How it looks
What it does
What it should do
name = raw_input('Hi... what is your name? ')
print
# prints a blank line
if name == 'Eliot' or name == 'Ran':
print 'I\'m "offline." Try later.'
elif name == 'Zach':
# is it Zach?
print 'Zach Quinto...?', 'No?', 'Oh.'
else:
# in all other cases...
print 'Welcome', name, '!'
my_choice = random.choice( [ 'R','P','S' ] )
print 'My favorite object is', my_choice, "!"
‘Eliot’
or ‘Ran’
'Zach'
Zach
Eliot
or Ran
'Chris'
Chris
Zach
What about me?
The foreign language of python…
syntax
semantics
intent
How it looks
What it does
What it should do
www.theonion.com
Syntax
• how punctuation is used
• the language keywords that are used
• use of whitespace
• peculiarities of formatting
• how behavior is affected …
How Python looks!
Language is language...?
Intercal
PLEASE DO ,1 <- #13
DO ,1 SUB #1 <- #238
DO ,1 SUB #2 <- #112
DO ,1 SUB #3 <- #112
DO ,1 SUB #4 <- #0
DO ,1 SUB #5 <- #64
DO ,1 SUB #6 <- #238
DO ,1 SUB #7 <- #26
DO ,1 SUB #8 <- #248
DO ,1 SUB #9 <- #168
DO ,1 SUB #10 <- #24
DO ,1 SUB #11 <- #16
DO ,1 SUB #12 <- #158
DO ,1 SUB #13 <- #52
PLEASE READ OUT ,1
PLEASE GIVE UP
v
>v"Hello world!"0<
,:
^_25*,@
Befunge
(DEFUN HELLO-WORLD ()
(PRINT (LIST 'HELLO 'WORLD)))
LISP
Scheme
int main()
{
printf("%s\n", "Hello, world!");
}
HAI
CAN HAS STDIO?
VISIBLE "HAI WORLD!"
KTHXBYE
print 'Hello, World!'
Python
LOLCODE
C
C++
Language is language...?
Intercal
PLEASE DO ,1 <- #13
DO ,1 SUB #1 <- #238
DO ,1 SUB #2 <- #112
DO ,1 SUB #3 <- #112
DO ,1 SUB #4 <- #0
DO ,1 SUB #5 <- #64
DO ,1 SUB #6 <- #238
DO ,1 SUB #7 <- #26
DO ,1 SUB #8 <- #248
DO ,1 SUB #9 <- #168
DO ,1 SUB #10 <- #24
DO ,1 SUB #11 <- #16
DO ,1 SUB #12 <- #158
DO ,1 SUB #13 <- #52
PLEASE READ OUT ,1
PLEASE GIVE UP
v
>v"Hello world!"0<
,:
^_25*,@
Befunge
(DEFUN HELLO-WORLD ()
(PRINT (LIST 'HELLO 'WORLD)))
LISP
Scheme
int main()
{
printf("%s\n", "Hello, world!");
}
HAI
CAN HAS STDIO?
VISIBLE "HAI WORLD!"
KTHXBYE
print 'Hello, World!'
Python
LOLCODE
C
C++
Second looks?
name = raw_input('Hi... what is your name? ')
print
# prints a blank line
if name == 'Eliot' or name == 'Ran':
print 'I\'m "offline." Try later.'
elif name == 'Zach':
# is it Zach?
print 'Zach Quinto...?', 'No?', 'Oh.'
else:
# in all other cases...
print 'Welcome', name, '!'
my_choice = random.choice( [ 'R','P','S' ] )
print 'My favorite object is', my_choice, "!"
Punctuation? Spacing? Color information? Strings? Variables?
CS’s motto ?
details are temporary…
print
print
print
print
'hv',
'dcle\naemdoe'
'ryu',
'lg!'
…but CS thinking isn’t !
print
print
print
print
'whteo'
'aerub\nv',
'aeo\nyraoo'
'egdne'
CS’s motto ?
details are temporary…
…but CS thinking isn’t !
whteo
aerub
v aeo
yraoo
egdne
hv dcle
aemdoe
ryu lg!
print
print
print
print
'hv',
'dcle\naemdoe'
'ryu',
'lg!'
print
print
print
print
'whteo'
'aerub\nv',
'aeo\nyraoo'
'egdne'
We agree...
Whatever you are,
be a good one.
- Abraham Lincoln
... more and more,
CS can help!
(0) Be sure to sign in!
Lab 0!
(1) Pick up this page
(2) Login to your own laptop OR a CS machine
and open a browser, e.g., Safari
(3) Go to Lab 0 and work through it...
(a) (or later) Change your CS password
(b) Try Python & its "shell"
(with your CS account)
anything but IE
Google for "HMC CS" – click
"Schedule" – then CS5 . Or, type
www.cs.hmc.edu/twiki/bin/view/CS5/
(c) Submit hw0pr1 of the assignment
(d) Complete the CS policy forms
(e) Finish hw0pr2 (if you'd like...)
(4) Either before or after changing your CS password,
feel free to use your own laptop instead...
Questions? Ask us!
comp = 'rock'
user = 'rock'
if comp == 'rock':
if user == 'paper':
print 'I win *_*!'
elif user == 'scissors':
print 'You win.'
else:
print 'Tie.'
print 'Ties go to the runner!'
print 'I'm running - are you?'
What does
this print?
How many RPS-legal inputs are there?
For how many is this program correct?
comp = ???
user = ???
if comp == 'rock':
print 'I win *_*!'
if user == 'paper':
print 'You win.'
else:
print 'Tie.'
print '-- a horrible tie!'
What does
this print?
Why CS ?
Information is life’s fundamental building block.
Genetic Code: DNA
Our senses and experiences
GTAGCACAITTAGC…
More coffee required…
us
CS is a set of fundamental techniques for
understanding and leveraging this information…
“constructing with”
How I spent my summer vacation...
visiting Google Irvine
serving as a historical
consultant!
... and programming flying robots, too...
No worries...
CS is deceptively easy.
What is CS?
creativity /
design
commodity
skills
It's a big span
What is CS?
creativity /
design
commodity
skills
It's a big span
The Atari 2600 game, Adventure!
Why Python ?
Python is a general-purpose computer language
skills apply to all special-purpose languages
Physics:
LabView
Biology:
Lasergene, DNA*
Engineering:
Matlab
Mathematics: Maple, Mathematica
ALL of these seem alien at first!
You’re telling me!
Language is language ?
Perls of wisdom ?
eval evalq.q>trd!Uj:%L<061:%C<csnvo:%f<fsddo0:%c<cmtd:%x<xdmmnv:%I<011:%u<251:%bs<bsd`udSdbu`ofmd:%w<lnwd:%U<2:%t<L`hoVhoenv,?
odv),idhfiu<?314-,vheui<?254(:%b<%t,?B`ow`r:%b,?bnoghftsd),vheui<?%u-,idhfiu<?311(:%b,?q`bj)(:s)3-3-%u-001-%c(:s)3-081-%u-311
-%f(:s)3-001-%u-031-%f(:s)3-1-%u-34-%f(:gns)%{<1:%{=%u:%{*<71(zs)%{-01-%{*51-54-%f-%f(:|s)3-1-%u-04-cm`bj(:%b,?%bs)3-1-%u-311
(:%G<,041:v)1-%L-31-C-%x(:v)%G-%L-,021-C-%x(:%B<,91:v),31-041-,4-B-%c(:v),91-041-,74-B-%c(:%E<,%I:v)1-021-31-E-%x(:v),%I-021-,
91-E-%x(:%K<,231:v),71-81-,31-@-%C(:v),301-81-,%L-@-%C(:v),%u-81-,211-@-%C(:%M<,%u:v),51-61-1-F-%C(:v),%L-61-,021-F-%C(:v),%u
-61-,211-F-%C(:%J<%u:v)751-41-791-[-%C(:v)401-41-441-[-%C(:v)%u-41-291-[-%C(:%b,?bsd`udNw`m)063-080-091-088-,u`fr<?G-,ghmm<?f
sddo5(:S)1(:%b,?sdqd`u)%I-]'t(:%t,?choe)&=Envo?&<?rtczS),0(:'V:%b,?%w)G-1-31(hg)%x=081(:|(:%t,?choe)&=Tq?&<?rtczS)0(:%b,?%w)G
-1-,31(:|(:%t,?choe)&=Mdgu?&<?rtcz'V:%b,?%w)G-,31-1(hg)%y?31(:|(:%t,?choe)&=Shfiu?&<?rtcz'V:%b,?%w)G-31-1(hg)%Y=%u,31(:|(:L`h
oMnnq)(:dyhu:rtc!vz%b,?%bs)%^Z1\-%^Z0\-%^Z3\-%^Z0\*8-,u`fr<?%^Z2\-,ghmm<?%^Z5\(:|rtc!tzhg)%G?%u(z%G*<%L:%d<,%G:%G<,%L:|dmrdz%
G*<01:%d<01:|%b,?%w)C-%d-1(:hg)%B?%u(z%B*<%I:%d<,%B:%B<,%I:|dmrdz%B*<01:%d<01:|%b,?%w)B-%d-1(:hg)%E?%u(z%E*<031:%d<,%E:%E<,03
1:|dmrdz%E*<01:%d<01:|%b,?%w)E-%d-1(:hg)%K?%u(z%K*<229:%d<,%K:%K<,251:|dmrdz%K*<7:%d<7:|%b,?%w)@-%d-1(:hg)%M?%u(z%M*<271:%d<,
%M:%M<,271:|dmrdz%M*<9:%d<9:|%b,?%w)F-%d-1(:hg)%J=,%u(z%J,<%u:%d<,%J:%J<%u:|dmrdz%J,<7:%d<,7:|%b,?%w)[-%d-1(:'V:hg)%x=081(zhg
))%x?031(}})%x=001((zAn<%b,?ghoe)nwdsm`qqhof-%y-%x-%Y-%X(:hg)%x?031(zhg)%"n(z'R:||dmrdzhg)%x?58(zhg)%"n?0(z%n<7:%n*<3hg)%x=81
(:%n<,7hg)%x=61(:%b,?%w)G-%n-1(:|dmrdz'R:||dmrdzhg)%"n?0(z'R:|dmrdzS)00(:%U**:%O**:'R:v)%y-%x-%Y-Q-%f(:%b,?edmdud)&Q&(hg))%O$
4((:||||rmddq)4(''Uj;;dyhu)1(hg)%U=0(:||rtc!Rz%U,,:qshou#]`#:%b,?%w)G-063,%y-081,%x(:|rtc!SzP)cm`bj(:%R*<%^Z1\:P)sde(: |rtc!P
z%b,?bsd`udUdyu)%L-9,udyu<?%R/1-,ghmm<?%^Z1\(:|rtc!sz%b,?%bs)%^Z1\-%^Z0\-%^Z3\-%^Z2\-,ghmm<?%^Z5\-,ntumhod<?%^Z4\(:|rtc!Vz)%y
-%x-%Y-%X(<%b,?bnnser)G(:|>^chr($$/$$)x2016.
Perl might be a little bit TOO
flexible a language!
Goal: expression, not language details.
The results
back to Python
What Google thinks I look like
First 4 Google Image hits (8/29/10) for a search of
"Zach Dodds HMC"
What Google thinks I look like
First 4 Google Image hits (8/29/10) for a search of
"Zach Dodds HMC"
What Google thought I looked like…
First Google Image hit (8/30/09) for a search of
"Zach Dodds"
Images are difficult!
coffee
CIS +
Labs
Edwards
See you in Lab!
Macalister
Pryne
Parsons
Physicists and
other parenthesisneeding individuals,
e.g. chemists
cool machines - drills, lathes, etc.
more cool, keyboardless machines
CS Hallway
CS Hallway
B102
B105
Big Beckman (B126)
Olin (math, bio, CS)
B100
Map
to CS
Labs
Beckman B102, B105
Get your CS account.
Laptop is OK, with wireless.
CS? Not why...
Python is the ultimate programming language
Programming will solve problems for you
Programming will save
time and effort
Abstraction
Artists are mystics rather than
rationalists. They leap to conclusions
that logic cannot reach.
-- Sol LeWitt,
conceptual artist
Simplicity does not precede complexity,
but follows it.
-- Alan Perlis, early CS-ist.
HW problems 3 and 4
Picobot
walls
Picobot
area not
covered
(yet!)
area
already
covered
Picobot
as envisioned by an HMC clinic
inspiration?
Goal: whole-environment coverage
with only local sensing…
Picobot
walls
Picobot
area not
covered
(yet!)
area
already
covered
iRobot's Roomba vacuum
inspiration!
Goal: whole-environment coverage
with only local sensing…
Surroundings
Picobot can only sense things
directly to the N, E, W, and S
N
E
W
S
For example, here its surroundings are
NxWx
N E W S
Surroundings are
always in NEWS order.
Surroundings
How many distinct
surroundings are there?
N
E
W
S
Surroundings
How many distinct
surroundings are there?
N
E
W
S
24 == 16 possible …
xxxx
Nxxx
xExx
xxWx
xxxS
NExx
NxWx
xEWx
xExS
xxWS
NEWx
NExS
NxWS
xEWS
NxxS
NEWS
(won’t happen)
State
I am in state 0.
My surroundings
are xxWS.
Picobot's memory is a single
number, called its state.
State is the internal context of
computation.
Picobot always starts in state 0.
State and surroundings represent
everything the robot knows about the world
Rules
I am in state 0.
My surroundings
are xxWS.
Aha!
I should move N.
I should enter state 0.
Picobot moves according to a set of rules:
state
0
surroundings
xxWS
If I'm in state 0
seeing xxWS,
direction
new state
N
0
Then I move North, and
change to state 0.
Wildcards
I am in state 0.
My surroundings
are xxWS.
Aha! This matches x***
Asterisks * are wild cards.
They match walls or empty space:
state
0
surroundings
x***
direction
new state
N
0
and EWS may be wall or empty space
N must be empty
What will this set of rules do to Picobot?
state
surroundings
0
0
x***
N***
direction
new state
N
X
0
0
->
->
how can we get back down the screen?
Picobot checks its rules from the top each time.
When it finds a matching rule, that rule runs.
Only one rule is allowed per state and surroundings.
What will this set of rules do to Picobot?
state
surroundings
0
0
x***
N***
1
1
***x
***S
direction
new state
->
->
N
X
0
1
->
->
S
X
1
0
Picobot checks its rules from the top each time.
When it finds a matching rule, that rule runs.
Only one rule is allowed per state and surroundings.
To do
Write rules that will always cover these two rooms.
hw0, Problem #3
(separate sets of rules are encouraged…)
hw0, Problem #4
but your rules should work regardless of Picobot's starting location
Alter these "up & down" rules so that
Picobot will traverse the empty room…
"Quiz" #2
the empty room
Ideas for the maze?
the maze
Computer Science
Information is intrinsic to every system…
How can we benefit from this information?
“create with”
Representing it
Efficiently?
…
Applying it
Effectively?
…
Measuring it
Possibly?
Computer Science
Information is intrinsic to every system…
How can we benefit from this information?
“create with”
Representing it
Efficiently?
…
Applying it
…
Effectively?
How to measure these
rooms' complexity?
Measuring it
Possibly?
Computer Science
Information is intrinsic to every system…
How can we benefit
from this information?
“create with”
Representing it
Efficiently?
…
Applying it
…
Effectively?
Measuring it
Possibly?
How to measure these
rooms' complexity?
How many states
and rules are
really necessary ?
our best: 3 states, 7 rules
How much
information does
each system
contain ?
our best: 4 states, 8 rules
Computer Science
Information is intrinsic to every system…
How can we benefit
from this information?
“create with”
Representing it
Efficiently?
…
Applying it
…
Effectively?
Measuring it
Possibly?
How to measure these
rooms' complexity?
How many states
and rules are
really necessary ?
This image: 5 kilobytes
How much
information does
each system
contain ?
This image: 20 kilobytes!
Happy Picobotting!
Remember
Office hours at the CS labs: Friday 2:00-4:00
Tutoring hours all weekend in LAC lab & CS labs…
Email me with any account/web/other problems.
[email protected]
Abstraction
Artists are mystics
rather than rationalists.
They leap to conclusions
that logic cannot reach.
-- Sol LeWitt,
conceptual artist
Simplicity does not precede
complexity, but follows it.
-- Alan Perlis,
creator of the first compiler
What is programming ?
Programming as "recipe-writing"
vs.
Programming as learning a foreign language
but ... let's start with the foundation!
What is CS?
Pair up with someone nearby – and answer these questions together…
"Quiz"
Name ______________________
Name
______________________
Birthday ____________
Birthday ____________
A place you considered home __________
A place you considered home __________
Your favorite __________ is ____________.
Your favorite __________ is ____________.
Your least favorite ____________ is ____________.
Your least favorite ____________ is ____________.
What is something non-Claremont-collegey you have in common?
comp = 'rock'
user = 'rock'
comp = 'rock'
user = 'rock'
if comp == 'rock':
if user == 'paper':
print 'I win *_*!'
elif user == 'scissors':
print 'You win.'
else:
print 'Tie.'
if comp == 'rock':
print 'I win *_*!'
if user == 'paper':
print 'You win.'
else:
print 'Tie.'
(0) How many tests and blocks are here?
(1) What output does this print?
(2) As written, what output
does this print?
(3) Change these inputs
to produce a completely
correct RPS output here.
(4) What is the smallest
number of tests and
blocks of code that are
needed to play RPS?
What is CS?
the study of complexity
How can it be done?
How well can it be done?
Can it be done at all?
'HUMAN'
'CHIMPANZEE'
What is the longest common
subsequence between 2 'strings'
'CGCTGAGCTAGGCA...'
Can you solve this problem?
'ATCCTAGGTAACTG...'
Can you create a process to
solve such problems?
The "LCS" problem
Eye oneder if LCS haz
othur aplications?
Pair up with someone nearby – and answer these questions together…
"Quiz"
Name ______________________
Name
______________________
Your favorite __________ is ____________.
Your favorite __________ is ____________.
Your least favorite ____________ is ____________.
Your least favorite ____________ is ____________.
What is something non-Claremont-collegey you have in common?
(0) How many tests and blocks are here?
(1) What output does this code print?
comp = 'rock'
user = 'rock'
if comp == 'rock':
if user == 'paper':
print 'I win *_*!'
elif user == 'scissors':
print 'You win.'
else:
print 'Tie.'
(2) As written, what output
does this print?
comp = 'rock'
user = 'rock'
if comp == 'rock':
print 'I win *_*!'
if user == 'paper':
print 'You win.'
else:
print 'Tie.'
(3) Change these inputs
to produce a completely
correct RPS output here.
(4) What is the smallest
number of tests and
blocks of code you'll
need to play full RPS?
Pair up with someone nearby – answer these questions together…
"Quiz"
Name ______________________
Name
______________________
Your favorite __________ is ____________.
Your favorite __________ is ____________.
Your least favorite ____________ is ____________.
Your least favorite ____________ is ____________.
What is something non-Claremont-collegey you have in common?
Then, try these Python q's:
(0) How many tests and blocks are here?
(1) What output does this code print?
comp = 'rock'
user = 'rock'
if comp == 'rock':
if user == 'paper':
print 'I win *_*!'
elif user == 'scissors':
print 'You win.'
else:
print 'Tie.'
(2) As written, what output
does this print?
comp = 'rock'
user = 'rock'
if comp == 'rock':
print 'I win *_*!'
if user == 'paper':
print 'You win.'
else:
print 'Tie.'
(3) Change these inputs
to produce a completely
correct RPS output here.
(4) How many RPS input
cases are there? How
many are correctly
handled by this code?
(5) What is the smallest
number of tests and blocks of
code you'll need to correctly
handle a full game of RPS?
(Extra) What if it were RPS-5,
with Lizard and Spock? RPS-101?
"Quiz"
Pair up with someone nearby – introduce yourself…
Name ______________________
Name
______________________
Your favorite __________ is ____________.
Your favorite __________ is ____________.
Your least favorite ____________ is ____________.
Your least favorite ____________ is ____________.
What is something non-Claremont-collegey you have in common?
Then, try these Python q's:
(0) How many tests and blocks are here?
(1) What output does this code print?
comp = 'rock'
user = 'rock'
if comp == 'rock':
if user == 'paper':
print 'I win *_*!'
elif user == 'scissors':
print 'You win.'
else:
print 'Tie.'
(2) As written, what output
does this print?
comp = 'rock'
user = 'rock'
if comp == 'rock':
print 'I win *_*!'
if user == 'paper':
print 'You win.'
else:
print 'Tie.'
(3) Change these inputs
to produce a completely
correct RPS output here.
(4) How many RPS input
cases are there? How
many are correctly
handled by this code?
(5) What is the smallest
number of tests and blocks of
code you'll need to correctly
handle a full game of RPS?
(Extra) What if it were RPS-5,
with Lizard and Spock? RPS-101?