Title: CS 60 Slides
1Driving N on the Dalton Highway (though it feels
like it!)
Welcome to Programming Practicum
Victorville, for DARPA's Urban Granc Challenge
Pittsburgh
Putting the C into CS
University of St. Petersburg
Waiting for the snow enveloping you on Route 5 N
to melt
Engineering dept.
exploring martian soil
Denver, CO or Minneapolis, MN
On the 405, in traffic, being chased by police
(and TV) helicopters.
You arent here
Worldcom Headquarters
Krispy Kremes drive-through
writing clinic reports
Waiting in line to vote in the Florida primaries
rebooting knuth (or turing or)
coding chunky strings
Being dragged off-course 18 miles into a marathon
race by a crazed spectator
clinic liaison phone call
installing Debian 3.1
Teaching Honors English for Janice Barbee at
Pomona High School
Leading a Gray Davis / Gary Coleman / Arnold
T-800 Schwarzenegger gubernatorial fundraiser
Massey University Palmerston North, NZ
Mailing something at the Claremont Post Office
the dumpster
2What is this course about?
3What is this course about?
- A chance to improve your programming skills
Algorithm analysis and insight
optimizing coding time
What
Program design and implementation
ACM programming contest
Hands-on practice with algorithms and techniques
Why
Familiarity with Javas libraries OR your choice
of language
"reasonable"
Research/prototype programming
Unofficial course name CS -70
42007 ACM regionals - results
http//www.socalcontest.org/history/2007/results-2
007.shtml
CSLB
Pomona
SDSU
CPSLO
http//icpc.baylor.edu/icpc/
'09 world finals Stockholm, Sweden
5Class Organization
alternating format
discussion sessions
lab sessions
- teams may use 1 machine per person (only the
mock contest adheres to ACM rules) - these problems count for each member of the group
- problem and program analysis
- discussion of strategy and coding tips
- deciding on functional decomposition, data
structures, language facilities, and algorithms
to use in teams of 2-3
a team might want to practice with only 1 machine
- sometimes new problems, other times with known
ones
- 4 problems given out per week
6Class Organization
Feedback from prior semesters
- there should be an opportunity to start coding
cold
- make individual vs. team-based work clear,
lectures vs. labs
- problems are, in general, individually completed
-- except
- those done during the lab "mock contest" sessions
- provide the code to all team members
- you may or may not choose to work as a team
afterwards
- submit for each person (email me if there are
problems)
- problems per person per week?
- about 12 (if you work on a team in lab)
- and consider all of the weeks of the term!
7Course Organization
Sep 2 Welcome! Review of dynamic
programming 4 problems Sep 9 Lab/Mock
contest session 4 problems Sep 16 Discussion
session on graph problems 4 problems Sep 23
Lab/Mock contest session 4 problems Sep 30
Discussion session on geometry problems 4
problems Oct 7 No class (I'll be out of
town) Oct 14 Lab/Mock contest session 4
problems Oct 21 No class Fall break! Oct 28
Mock ACM contest, 9pm 100am, 6 problems Nov
4 Regional participants' -- preparation
meeting Nov 11 Discussion and wrap-up for the
semester
You may submit problems until the end of exams
Sat. Nov. 8 may be the regional contest
8Course webpage
references
problem statements and sample data
problems you have solved
administrative info
9Grading
CS 189 is graded individually... (its possible
to take it P/F, too)
though not for CS elective credit
Coding Guidelines
- problems can be done any time during the
semester - discussion of algorithms always OK
- coding should be within teams
- you may use any references except an existing
solution or partial solution - one person should take the lead on each problem
- use /cs/ACM/acmSubmit ltfilegt to submit
- try things out !
the reason for ACM!
10Problem multipliers
Problems are worth double if
- You solve them during the 415 - 530 lab
sessions - It's one of the "extra difficult" problems,
which will be determined as we go - The new-language bonus is only in the spring
term! - Any standard language is OK -- but do keep in
mind that the competition allows only Java, C,
and C .
the team gets credit, up to 3 people
These multipliers may be accumulated
Language Choice?
Other "standard" languages
C, python, ruby
Ask about our 2 extra-credit projects!
reasonable alternatives will also be considered
web-updating and web-jotto
11Spring 2008 summary
17 (2)
8
4
16 (2)
1 (1)
6 (1)
20 (12) (2)
3 (1)
1
1
2
11 (1)
1 (1)
17 (9) (2)
4 (2)
1
2
2
8
3
2
8 (2)
13 (10)
14 (9)
1
21 (16) (4)
1
1
15 (14)
number of 2x scores
number of 4x scores
python 82 java 60 C 40
d 8 ruby 6 scheme 3
lua 2 awk 2 js 2
sql cobol basic x86 asm pascal mathematica sh,
latex
Tallies per problem and per language (thus far)
1 each
12Dynamic programming!
13Jotto!
A word-guessing game similar to mastermind
Sophs
Jrs
Srs
Profs
pluot 1
pluot 0
pluot 1
pluot 2
14Dynamic programming
When a seemingly intractable problem has lots of
repeated substructure, go DP!
Build a table of partial results.
Replace computation with table look-up when
possible
For example
15Dynamic programming
When a seemingly intractable problem has lots of
repeated substructure, go DP!
Build a table of partial results.
Replace computation with table look-up when
possible
the binary-decimal problem, for example
Output
2 6 3 19 0
Input
Numbers, N, up to 106
10 1110 111 11001
the smallest decimal multiple of N with only the
digits 0 and 1 !
0 marks the end of the input
Ideas?
16One way
Count up to the solution, starting from 1
17Dynamic programming
Storing intermediate results in a table for fast
look-up
input N 6
possible remainders upon dividing by N (6)
0
1
2
3
4
5
1
2
of digits in answer
3
4
18Dynamic programming
Storing intermediate results in a table for fast
look-up
input N 6
possible remainders upon dividing by N (6)
0
1
2
3
4
5
1
1
2
of digits in answer
3
4
19Dynamic programming
Storing intermediate results in a table for fast
look-up
input N 6
possible remainders upon dividing by N (6)
0
1
2
3
4
5
1
1
1
2
10
11
of digits in answer
3
4
20Dynamic programming
Storing intermediate results in a table for fast
look-up
input N 6
possible remainders upon dividing by N (6)
0
1
2
3
4
5
1
1
1
2
10
11
of digits in answer
1
3
10
11
111
110
4
21Dynamic programming
Storing intermediate results in a table for fast
look-up
input N 6
possible remainders upon dividing by N (6)
0
1
2
3
4
5
1
1
1
2
10
11
of digits in answer
1
3
10
11
111
110
1
1110
111
110
10
11
4
22DP!
Only checking values for which a remainder has
not yet been used
Fast
23Another example
binary-as-decimal problem
"pebbles" problem (2007 ACM regionals)
Input
3 8 6 7 9 1 4 6 1
Idea
3 8 6 7 9 1 4 6 1
maximum possible sum
Square array, up to 15x15, of integers from 1 to
99
14?
place "pebbles" on integers, trying to maximize
total, but no two pebbles may be adjacent
Output
vertically, horizontally, or diagonally
24Pebbles
consider all possibilities for pebbling each row
- they only depend on the previous row's best
scores!
DP Idea
Subset chosen (pebbles)
Input
000
001
010
011
100
101
110
111
0
3 8 6 7 9 1 4 6 1
1
Row
2
Store the BEST score available for each possible
subset.
a
b
c
Square array, up to 15x15, of integers from 1 to
99
25Pebbles
consider all possibilities for pebbling each row
- they only depend on the previous row's best
scores!
DP Idea
Subset chosen (pebbles)
Input
000
001
010
011
100
101
110
111
0
6
8
x
3
9
x
x
0
3 8 6 7 9 1 4 6 1
1
Row
2
Store the BEST score available for each possible
subset.
a
b
c
Square array, up to 15x15, of integers from 1 to
99
26Pebbles
consider all possibilities for pebbling each row
- they only depend on the previous row's best
scores!
DP Idea
Subset chosen (pebbles)
Input
000
001
010
011
100
101
110
111
0
6
8
x
3
9
x
x
0
3 8 6 7 9 1 4 6 1
09
13
90
76
80
1
x
x
x
9
4
9
13
8
Row
2
a
b
c
Store the BEST score available for each possible
subset.
Square array, up to 15x15, of integers from 1 to
99
27Pebbles
consider all possibilities for pebbling each row
- they only depend on the previous row's best
scores!
DP Idea
Subset chosen (pebbles)
Input
000
001
010
011
100
101
110
111
0
6
8
x
3
9
x
x
0
3 8 6 7 9 1 4 6 1
09
13
90
76
80
1
x
x
x
9
4
9
13
8
Row
x
x
x
2
c13
b9
a9
013
?
a
b
c
Store the BEST score available for each possible
subset.
Square array, up to 15x15, of integers from 1 to
99
running time?
28Pebbles
binary-as-decimal problem
"pebbles" problem (2007 ACM regionals)
Input
71 24 95 56 54 85 50 74 94 28 92 96 23 71 10 23
61 31 30 46 64 33 32 95 89
Idea
71 24 95 56 54 85 50 74 94 28 92 96 23 71 10 23
61 31 30 46 64 33 32 95 89
maximum possible sum
Square array, up to 15x15, of integers from 1 to
99
572
place "pebbles" on integers, trying to maximize
total, but no two pebbles may be adjacent
Output
29Scanner sc new Scanner(System.in)
even a bit easier!
This sure is sum code
to the max
Martijn is VERY shifty!
Where is the table?
Martijn is shifty!
Thanks, Martijn!
30Problem Set 0
(4 problems)
In teams of 23, read over these
I need a picture of Farmer Ran!
31Problem Set 0
(4 problems)
In teams of 23, read over these.
- Where does the structure of the problem depend
on similar (but smaller!) substructure?
- How might you build up a table of values toward
an overall solution?
- Think of your next 5-letter jotto word !
32See you next Tuesday!
bring a laptop, if you have one
33Jotto!
A word-guessing game similar to mastermind
Sophs
Jrs
Srs
Me
pluot 1
pluot 0
pluot 1
pluot 2
34Problem Set 1
(6 probs, 3 wks)
Input
of words in the puzzle (to follow)
of rows in the puzzle (after the words)
4 4 tow cat row care ... .. ... . 0 0
the words
the puzzle
this indicates the end of the input
35Problem Set 1
(6 probs, 3 wks)
Input
4 4 tow cat row care ... .. ... . 0 0
Problem 1 cat ao row e Problem 2 no
layout is possible.
either the solution OR a statement that it can't
be solved
Output
36Problem Set 1
(6 probs, 3 wks)
Input
of empty boxes on the form
of clerks in the office (three lines each)
6 5 1 1 2 2 3 4 4 5 1 3 2 1 4 2 1
0 0 0
the box(es) CHECKED
clerk 0
the box(es) ERASED
the clerks who get a copy
0
1
2
3
4
5
clerk 1
the form
clerk 2
clerk 3
clerk 4
this indicates the end of the input
37Problem Set 1
(6 probs, 3 wks)
Input
of empty boxes on the form
of clerks in the office (three lines each)
6 5 1 1 2 2 3 4 4 5 1 3 2 1 4 2 1
0 0 0
the box(es) CHECKED
clerk 0
the box(es) ERASED
the clerks who get a copy
0
1
2
3
4
5
clerk 1
the form
clerk 2
let's try it
clerk 3
clerk 4
this indicates the end of the input
38Problem Set 1
(6 probs, 3 wks)
Input
of empty boxes on the form
of clerks in the office (three lines each)
6 5 1 1 2 2 3 4 4 5 1 3 2 1 4 2 1
0 0 0
the box(es) CHECKED
clerk 0
the box(es) ERASED
the clerks who get a copy
0
1
2
3
4
5
clerk 1
the form
clerk 2
clerk 3
Output
1 3 4 5
the boxes checked the last time it leaves clerk
0's desk
clerk 4
this indicates the end of the input
39Problem Set 1
(6 probs, 3 wks)
Input
list of traits, R "recessive" D "dominant"
D traits are passed if EITHER parent has them
RDDR Speedy M 0101 Jumper F 0101 Slowpoke M
1101 Terror F 1100 Shadow F 1001 Frisky
0101 Sleepy 1101
R traits are passed if BOTH parents have them
all of the parents, gender, and traits
the baby shrews
Deduce their possible parents!
40Problem Set 1
(6 probs, 3 wks)
Input
list of traits, R "recessive" D "dominant"
D traits are passed if EITHER parent has them
RDDR Speedy M 0101 Jumper F 0101 Slowpoke M
1101 Terror F 1100 Shadow F 1001 Frisky
0101 Sleepy 1101
R traits are passed if BOTH parents have them
all of the parents, gender, and traits
the baby bunnies
Deduce their possible parents!
Frisky by Jumper-Slowpoke or Jumper-Speedy or
______ Sleepy by _________
Output
41Problem Set 1
(6 probs, 3 wks)
Input
list of traits, R "recessive" D "dominant"
D traits are passed if EITHER parent has them
RDDR Speedy M 0101 Jumper F 0101 Slowpoke M
1101 Terror F 1100 Shadow F 1001 Frisky
0101 Sleepy 1101
R traits are passed if BOTH parents have them
all of the parents, gender, and traits
the baby bunnies
Deduce their possible parents!
Frisky by Jumper-Slowpoke or Jumper-Speedyor
Shadow-Speedy Sleepy by Shadow-Slowpoke
Output
42Problem Set 1
(6 probs, 3 wks)
Read these three problems then
Decide which problem is the easiest and which one
is the hardest (to code, not to compute!)
43Problem Set 1
(6 probs, 3 wks)
my estimates
important heuristic Im always wrong
hardest
easiest
44See you next Tuesday in the CS labs !
45Driving N on the Dalton Highway (though it feels
like it!)
Welcome to Programming Practicum
Pittsburgh
Putting the C into CS
University of St. Petersburg
Waiting for the snow enveloping you on Route 5 N
to melt
Engineering dept.
exploring martian soil
On the 405, in traffic, being chased by police
(and TV) helicopters.
You arent here
Worldcom Headquarters
Krispy Kremes drive through
writing clinic reports
rebooting knuth (or turing or)
coding chunky strings
Being dragged off-course 18 miles into a marathon
race by a crazed spectator
clinic liaison phone call
installing Debian 3.1
Teaching Honors English for Janice Barbee at
Pomona High School
Leading a Gray Davis / Gary Coleman / Arnold
T-800 Schwarzenegger gubernatorial fundraiser
Massey University Palmerston North, NZ
Mailing something at the Claremont Post Office
the dumpster
46What is this course about?
- A chance to improve your programming skills
Algorithm analysis and insight
optimizing coding time
What
Program design and implementation
ACM programming contest
Hands-on practice with algorithms and techniques
Why
Familiarity with Javas libraries OR your choice
of language
Research/prototype programming
Unofficial course name CS -70
47Course Organization
Jan 16 Welcome! Random teams of 3 6
problems, 3 weeks Jan 23 Lab session to work
on problems (Beckman B102 or 105) Jan 30
Discussion session on the first set of problems
Feb 6 Lab session with new problems 4
problems, 2 weeks Feb 13 Discussion session
on the second set of problems Feb 20 Lab
session with new problems 6 problems, 3
weeks Feb 27 Discussion session on the third
set of problems Mar 6 No class - Fall
break Mar 20 Mock ACM contest, 9pm 1am,
teams of 3, 6 pr, 4 hours Mar 27 No class (out
of town) Apr 3 Discussion session on the
mock contest Finale!
2 per team
2/team
3 per team
1-4 / team
You may submit problems until the end of exams
48Class Organization
alternating format
discussion sessions
lab sessions
- problem and program analysis
- discussion of strategy and coding tips
- deciding on functional decomposition, data
structures, language facilities, and algorithms
to use in teams of 2-3
- teams should use 1 terminal per person (only the
mock contest adheres to ACM rules) - these problems count for each member of the group
- sometimes new problems, other times with known
ones
- 1 problem per week per person
49Course webpage
reference links
problem statements and test data
problems your team has solved
administrative info
50Grading
CS 189 is graded individually... (its possible
to take it P/F, too)
Coding Guidelines
- problems can be done any time during the
semester - discussion of algorithms always OK
- coding should be within teams
- you may use any references except an existing
solution or partial solution - one person should take the lead on each problem
- use /cs/ACM/acmSubmit ltfilegt to submit
- try things out !
the reason for ACM!
51Choose your language
Java is almost the universal choice for the
competition
- extensive library of data structures and
algorithms available
- I/O made simpler with 1.5s Scanner and printf
Input from stdin Output to stdout
the C in CS!
52Choose your language
Python is nice
C
Whatever language you choose needs to be able to
run on the Macs in the CS labs (and preferably
knuth, as well)
def floyd_warshall(W, infinity) n
matrix.get_num_rows(W) D matrix.clone(W)
DD matrix.make(n,n) for k in xrange(n)
for i in xrange(n) for j in
xrange(n) DDij
min(Dij, Dik Dkj) DD, D
D, DD return D
Ill likely need your help to get things set up
for testing
Others ?
Marty prolog!
53Problem Set 1
(6 probs, 3 wks)
Input
of words in the puzzle (to follow)
of rows in the puzzle (after the words)
4 4 tow cat row care ... .. ... . 0 0
the words
the puzzle
this indicates the end of the input
54Problem Set 1
(6 probs, 3 wks)
Input
of empty boxes on the form
of clerks in the office (three lines each)
6 5 1 1 2 2 3 4 4 5 1 3 2 1 4 2 1
0 0 0
the box(es) CHECKED
clerk 0
the box(es) ERASED
the clerks who get a copy
0
1
2
3
4
5
clerk 1
the form
clerk 2
clerk 3
Output
1 3 4 5
the boxes checked the last time it leaves clerk
0's desk
clerk 4
this indicates the end of the input
55Problem Set 1
(6 probs, 3 wks)
Input
list of traits, R "recessive" D "dominant"
D traits are passed if EITHER parent has them
RDDR Speedy M 0101 Jumper F 0101 Slowpoke M
1101 Terror F 1100 Shadow F 1001 Frisky
0101 Sleepy 1101
R traits are passed if BOTH parents have them
all of the parents, gender, and traits
the baby shrews
Deduce their possible parents!
Frisky by Jumper-Slowpoke or Jumper-Speedy or
______ Sleepy by _________
Output
56Problem Set 1
(6 probs, 3 wks)
Read these three problems then
Decide which problem is the easiest and which one
is the hardest (to code, not to compute!)
57Coaches Room
58Choose your language
Java/Cthe universal choice for the competition
- extensive library of data structures and
algorithms available
- I/O made simpler with 1.5s Scanner and printf
Input from stdin Output to stdout
the C in CS!
59Choose your language
Python is nice
Postscript!
def floyd_warshall(W, infinity) n
matrix.get_num_rows(W) D matrix.clone(W)
DD matrix.make(n,n) for k in xrange(n)
for i in xrange(n) for j in
xrange(n) DDij
min(Dij,
Dik Dkj) DD, D D, DD
return D
Ruby -)
Marty prolog!
60Driving N on the Dalton Highway (though it feels
like it!)
Welcome to Programming Practicum
Victorville, for DARPA's Urban Granc Challenge
Pittsburgh
Putting the C into CS
University of St. Petersburg
Waiting for the snow enveloping you on Route 5 N
to melt
Engineering dept.
exploring martian soil
On the 405, in traffic, being chased by police
(and TV) helicopters.
You arent here
Worldcom Headquarters
Krispy Kremes drive-through
writing clinic reports
Waiting in line to vote in the Florida primaries
rebooting knuth (or turing or)
Waiting in line to vote in the Florida primaries
coding chunky strings
Being dragged off-course 18 miles into a marathon
race by a crazed spectator
clinic liaison phone call
installing Debian 3.1
Teaching Honors English for Janice Barbee at
Pomona High School
Leading a Gray Davis / Gary Coleman / Arnold
T-800 Schwarzenegger gubernatorial fundraiser
Massey University Palmerston North, NZ
Mailing something at the Claremont Post Office
the dumpster
61What is this course about?
- A chance to improve your programming skills
Algorithm analysis and insight
optimizing coding time
What
Program design and implementation
ACM programming contest
Hands-on practice with algorithms and techniques
Why
Familiarity with Javas libraries OR your choice
of language
Research/prototype programming
Unofficial course name CS -70
62Class Organization
alternating format
discussion sessions
lab sessions
- problem and program analysis
- discussion of strategy and coding tips
- deciding on functional decomposition, data
structures, language facilities, and algorithms
to use in teams of 2-3
- teams should use 1 machine per person (only the
mock contest adheres to ACM rules) - these problems count for each member of the group
- sometimes new problems, other times with known
ones
- 3 problems per week per person
63Class Organization
Feedback from prior semesters
- there should be an opportunity to start coding
cold
- make individual vs. team-based work clear,
lectures vs. labs
- problems are, in general, individually
completed, except
- those done during the lab "mock contest" sessions
- provide the code to all team members
- you may or may not choose to work as a team
afterwards
- submit for each person (or email me)
- problems per person per week?
- 2 in the fall
- 3 in the spring
64Course Organization
Jan 29 Welcome! Review of dynamic
programming 4 problems Feb 5 Lab/Mock
contest session 4 problems Feb 12 Discussion
session on geometry problems 4 problems Feb 19
Lab/Mock contest session 4 problems Feb 26
Lab/Mock contest session 4 problems Mar 4
Discussion session on search problems 4
problems Mar 11 No class need to be away Mar
18 No class - Spring break Mar 23 Mock ACM
contest, 9pm midnight, 6 problems Mar 25
Discussion and wrap-up of the semester
You may submit problems until the end of exams
Sat. Mar 8 and Sat. Apr 19 external competitions
65Competition Options
www.ccsc.org/southwestern/2008/contest.html
Sat. Mar 8 and Sat. Apr 19 external competitions
www.ieee.org/web/membership/students/scholarshipsa
wardscontests/ieeextreme.html
66Course webpage
references
problem statements and test data
problems you have solved
administrative info
67Grading
CS 189 is graded individually... (its possible
to take it P/F, too)
Coding Guidelines
- problems can be done any time during the
semester - discussion of algorithms always OK
- coding should be within teams
- you may use any references except an existing
solution or partial solution - one person should take the lead on each problem
- use /cs/ACM/acmSubmit ltfilegt to submit
- try things out !
the reason for ACM!
68Problem multipliers
Problems are worth double if
- You solve them during the 415 - 530 lab
sessions - You are the first person to use a particular
language - though there is an additional
responsibility here to set up the testing system
to handle that language! - It's one of the "extra difficult" problems,
which will be determined as we go
the team gets credit, up to 3 people
languages already used
python 56 ruby 12 java 6
c 11 perl 2 postscript 2
c 8 haskell 1 prolog 2
c 5 php 1
These multipliers may be accumulated
69Dynamic programming
When a seemingly intractable problem has large
amounts of repeated or redundant substructure, DP
can sometimes provide an efficient solution.
Build a table of partial results.
Replace computation with table look-up when
possible
For example
Output
2 6 3 19 0
Input
10 1110 111 11001
the smallest decimal multiple of N with only the
digits 0 and 1 !
Numbers, N, up to 106
0 marks the end of the input
Ideas?
70One way
Count up to the solution, starting from 1
71Dynamic programming
Storing intermediate results in a table for fast
look-up
input N 6
possible remainders upon dividing by N (6)
0
1
2
3
4
5
1
1
1
2
10
11
of digits in answer
1
3
10
11
111
110
1
1110
111
110
10
11
4
72DP!
Only checking values for which a remainder has
not yet been used
73Problem Set 1
(4 problems)
- divide up with one problem per group -
In teams of 3, think about
- how would you solve this problem?
- is it a dynamic programming problem?
- how could you most simplify the programming ?
- think of a 5-letter jotto word !
74Competition Options
www.ccsc.org/southwestern/2008/contest.html
Sat. Mar 8 and Sat. Apr 19 external competitions
www.ieee.org/web/membership/students/scholarshipsa
wardscontests/ieeextreme.html