EECS 110: Lec 7: Program Planning - PowerPoint PPT Presentation

About This Presentation
Title:

EECS 110: Lec 7: Program Planning

Description:

rock-paper-scissors judger ''' if 'p1' == 'p2': return 0. def letterscore(let): if let in 'zq' ... Wanted: a function that solves it. Are these tasks still too ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 36
Provided by: csNorth
Category:
Tags: eecs | it | lec | let | planning | program | rock

less

Transcript and Presenter's Notes

Title: EECS 110: Lec 7: Program Planning


1
EECS 110 Lec 7 Program Planning
  • Aleksandar Kuzmanovic
  • Northwestern University

http//cs.northwestern.edu/akuzma/classes/EECS110
-s09/
2
EECS 110 Today
Computing to the max
The not-so-subtle art of singling out the best
(and worst) of anything
Hw 3 due Sunday
Lights On!for fun and safety
How to break apart a tough problem
3
Notes from prior hwks
Warnings!
def rps(p1,p2) """ rock-paper-scissors
judger """ if 'p1' 'p2' return 0
def rps(p1,p2) if p1 p2 return
'0'
def letterscore(let) if let in 'zq'
return 10 (lots more)
4
Notes from prior hwks
Warnings!
def rps(p1,p2) """ rock-paper-scissors
judger """ if 'p1' 'p2' return 0
The string 'p1' is not the same as the variable
p1 !
def rps(p1,p2) """ rock-paper-scissors judger
""" if p1 p2 return '0'
The string '0' is not the same as the number 0 !
no docstring!
def letterscore(let) if let in 'zq'
return 10 (lots more)
Capital letters count! (It should be letterScore.)
5
What CS is really about
thinking like a machine
variables
42
storage
int guess
sequences
'w'
'a'
'r'
't'
str s1
str s2
str s3
str s0
ifelifelse
making decisions
high or low
recursion
repeated actions
6
What CS is really about
thinking like a machine
thinking for a machine
variables
library
42
storage
int guess
deciding how to use these tools
sequences
functions
'w'
'a'
'r'
't'
str s1
str s2
str s3
str s0
creating your own tools ...
ifelifelse
classes
making decisions
creating your own data structures ...
high or low
(later)
recursion
repeated actions
7
Top-down program design
Given a description of the problem
translation!
Wanted a function that solves it
8
Top-down program design
Given a description of the problem
translation!
Wanted a function that solves it
with as much detail as possible
1. Visualize what the program will do
2. Break up the work into a set of smaller tasks
3. Compose solutions for these tasks (functions)
variables, lists, ifelifelse, recursion
  • What do you need for each?
  • Are these tasks still too big? if so, go to
    step 1

1. Visualize what the function will do ...
9
Top-down program design
Given a description of the problem
translation!
Wanted a function that solves it
with as much detail as possible
1. Visualize what the program will do
2. Break up the work into a set of smaller tasks
How to do this
3. Compose solutions for these tasks (functions)
variables, lists, ifelifelse, recursion
  • What do you need for each?
  • Are these tasks still too big? if so, go to
    step 1

1. Visualize what the function will do ...
10
Monte Carlo Monty Hall
Suppose you always switch to the other door...
What are the chances that you will win the car ?
Run it (randomly) 1000 times and see!
How can we write MCMH?
11
Monte Carlo Monty Hall
How can we write MCMH?
What is the input/output of your function?
What data do we need to keep track of?
12
Monte Carlo Monty Hall
How can we write MCMH?
What specific actions does your function need to
take?
13
Monte Carlo Monty Hall
How can we write MCMH?
Put it all together into an algorithm
14
Monte Carlo Monty Hall
Then translate the algorithm to code!
def MCMH( init, sors, N ) """ plays the same
"Let's make a deal" game, N times returns
the number of times you win the car """ if N
0 return 0 don't play, can't win
carDoor choice(1,2,3) where is the car?
if init carDoor and sors 'stay' result
'Car!' elif init carDoor and sors
'switch' result 'Spam.' elif init ! carDoor
and sors 'switch' result 'Car!' else
result
'Spam.' print 'You get the', result if
result 'Car!' return 1 MCMH( init, sors,
N-1 ) else return 0 MCMH(
init, sors, N-1 )
15
Sorting a List
Sorting a List
What is the input/output of the function?
What data do we need to keep track of?
16
Sorting a List
Sorting a List
If we had an easy way to find the maximum of the
list, how could we use this to sort the list?
17
Taking only one
def removeOne( e, L ) """ this function
removes one element e from the top level
of the list L """ if len(L) 0
return L L is empty elif e L0
return L1 remove this one
else return
L01 removeOne(e,L1) keep
the non-e element and then keep going
removeOne(42, 5,7,42,8,42)
removeOne('p', 'computer programming')
5,7,8,42
'comuter programming'
18
max
A recipe for life ?
and python already has it for us
The hard part is knowing what we want to maximize!
19
Google Inc
to the max
If we want the highest price
max( 449.5, 580.0, 562.4, 481.3, 498.3, 414.5 )
'sep'
'aug'
'jul'
'jun'
'may'
'apr'
What if the months are in there, as well?
max( 449.5,'apr', 580.0,'may',
562.4,'jun', 481.3,'jul', 498.3,'aug',
414.5,'sep' )
20
"Best" word
def scrabbleScore(w) see homework 1!
Let's abbreviate this function as scsc(w)
def bestWord( L ) """ finds the "best" word
from L, a list of words here, "best"
means highest scrabble score """
21
"Best" word
def scrabbleScore(w) see homework 1!
Let's abbreviate this function as scsc(w)
def bestWord( L ) """ finds the "best" word
from L, a list of words here, "best"
means highest scrabble score """ if len(L) lt
2 return elif return
else return
22
"Best" word
def scrabbleScore(w) see homework 1!
Let's abbreviate this function as scsc(w)
def bestWord( L ) """ finds the "best" word
from L, a list of words here, "best"
means highest scrabble score """ if len(L) lt
2 return L0 elif scsc(L0) lt
scsc(L1) return bestWord( L1 )
else return bestWord( L01 L2 )
23
A suggestion
def scrabbleScore(w) see homework 1!
Let's abbreviate this function as scsc(w)
def bestWord( L ) """ returns the word in L
w/max scrabble score """ LOL scsc(w),
w for w in L LOL bestPair max( LOL
) return bestPair
24
The last word on bestWord
def scrabbleScore(w) see homework 1!
Let's abbreviate this function as scsc(w)
using raw recursion
def bestWord( L ) """ finds the "best" word
from L, a list of words here, "best"
means highest scrabble score """ if len(L) lt
2 return L0 elif scsc(L0) lt
scsc(L1) return bestWord( L1 ) else
return bestWord( L01 L2 )
using max
def bestWord( L ) """ returns the word in L
w/max scrabble score """ LOL scsc(w), w
for w in L bestPair max( LOL )
return bestPair
25
Examples
gtgtgt bestNumber( 10, 20, 30, 40, 50, 60, 70
) 40
gtgtgt bestNumber( 100, 200, 300, 400 ) 100
gtgtgt bestNumber( 1, 2, 3, 4, 5, 6, 7, 8, 7 ) 8
gtgtgt mode( 1, 2, 3, 4, 5, 6, 7, 8, 7 ) 7
What is bestNumber ? mode ?
26
"Quiz"
Name(s)
Nothing but the best!
abs( x ) is built-in to Python
Hints
Use bestWord as a guide
Write this function using max/min or recursively
def bestWord( L ) """ example code """
LOL scsc(w), w for w in L bestPair
max( LOL ) return bestPair
def bestNumber( L ) """ returns the in L
closest to 42 """
Hint
Write this function however you like
Consider defining a helper function !
def mode( L ) """ returns the element
appearing most often in L """
27
"Quiz"
abs( x ) is built-in to Python
Hints
Solutions
Use bestWord as a guide
def bestWord( L ) """ example code """
LOL scsc(w), w for w in L bestPair
max( LOL ) return bestPair
Write this function using max/min
def bestNumber( L ) """ returns the in L
closest to 42 "" LOL abs(w-42), w for
w in L bestPair min( LOL ) return
bestPair1
28
"Quiz"
Solutions
Hint
Write this function however you like
Consider defining a helper function !
def numberOfTimes( w, L ) """ returns the
in times w repeats in L """ return sum(kw
for k in L)
def mode( L ) """ returns the element
appearing most often in L """ LOL
numberOfTimes(w,L),w for w in L return
max(LOL)1
29
sort(L)
def sort( L ) """ a list of elements in L,
sorted from hi to low """ if len(L) lt 1
return L else
30
sort(L)
def sort( L ) """ a list of elements in L,
sorted from hi to low """ if len(L) lt 1
return L else return max(L)
sort(removeOne( max(L), L ))
31
sort(L, maxFun)
def sort( L, maxFun ) """ a list of elements
in L, sorted using maxFun """ if len(L) lt 1
return L else return
32
sort(L, maxFun)
def sort( L, maxFun ) """ a list of elements
in L, sorted using maxFun """ if len(L) lt 1
return L else return maxFun(L)
sort(removeOne( maxFun(L), L ))
Will this work?
33
sort(L, maxFun)
def sort( L, maxFun ) """ a list of elements
in L, sorted using maxFun """ if len(L) lt 1
return L else return maxFun(L)
sort(removeOne( maxFun(L), L ), maxFun)
34
sort(L, maxFun)
def sort( L, maxFun ) """ a list of elements
in L, sorted using maxFun """ if len(L) lt 1
return L else return maxFun(L)
sort(removeOne( maxFun(L), L ), maxFun)
What happens if you call gtgtgtsort( L, min )
35
See you in Lab !
Write a Comment
User Comments (0)
About PowerShow.com