EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University

Slides:



Advertisements
Similar presentations
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
Advertisements

Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University
HW 1: Problems 3 & 4 EC 1 & 2.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Complexity (Running Time)
CIS 310: Visual Programming, Spring 2006 Western State College 310: Visual Programming Othello.
Homework 4 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/29 Mon., 9/30 Problems
EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University
An Introduction to Textual Programming
IS 313 Tomorrow… IS 313 last week ? 9/20/09 - today: beyond recursion! 9/27/09 - next wk: web technologies, pt 2 Assignment reminders... Which door to.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Lists Computers and Programming. Agenda What is a list? How to access elements in the list? The for statement Operations on lists Looping with.
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
… Caesar Cipher: encipher encipher( 'gv vw dtwvg', 0 ) encipher( 'gv vw dtwvg', 1 ) encipher( 'gv vw dtwvg', 2 ) encipher( 'gv vw dtwvg', 3 ) encipher(
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
Welcome to CS61A Disc. 29/47 :D Dickson Tsai OH: Tu, Th 4-5pm 411 Soda Previous stop: None >>> Today: Working effectively in.
CS 121 Today Fractals and Turtles! The Koch Curve how random…
IS 313 Tomorrow… IS 313 Today? 9/16/09 - today: recursion and beyond! 9/23/09 - next wk: no meeting (DC) 9/30/09 - following wk: for & while Homework functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Announcements Course evaluation Your opinion matters! Attendance grades Will be posted prior to the final Project 5 grades Will be posted prior to the.
 Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper.
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
Code Compression the benefits of looping... Today in CS 5 HW 4 - (3 problems) M/T sections W/Th sections due Sunday, 9/26 at midnight due Monday, 9/27.
EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University
The last CS 5 lecture you’ll ever need! Inducing labor for the machine! == Reducing labor for humans! On Warner Brothers' insistence, we affirm that this.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 13: Dictionaries Aleksandar Kuzmanovic Northwestern University
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Ray Karol 2/26/2013. Let’s Make a Deal Monte Hall Problem Suppose you’re on a game show, and you’re given a choice of three doors: Behind one door is.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CS 121 Today Fractals and Turtles! The Koch Curve how random…
EECS 110: Lec 8: Lists of Lists
EECS 110: Lec 13: Dictionaries
More on Functions (Part 2)
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 5: List Comprehensions
Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 7: Program Planning
Caesar Cipher: encipher
EECS 110: Lec 6: Fractals and Trutles
Functions CIS 40 – Introduction to Programming in Python
EECS 110: Lec 4: Functions and Recursion
Random numbers Taken from notes by Dr. Neil Moore
Spring 2010 EECS 110: Homework I.
EECS 110: Lec 4: Functions and Recursion
More on Functions (Part 2)
Big problem  small steps
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Last Class We Covered Recursion Stacks Parts of a recursive function:
EECS 110: Lec 12: Mutable Data
Recursion Review Spring 2019 CS 1110
Presentation transcript:

EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University

The not-so-subtle art of singling out the best (and worst) of anything… Lights On! for fun and safety Computing to the max Hw #3 due Sunday… How to break apart a tough problem EECS 110 Today

Notes from prior hwks… Warnings! def rps(p1,p2): """ rock-paper-scissors judger """ if 'p1' == 'p2': return 0 def letterscore(let): if let in 'zq': return 10 …(lots more)… def rps(p1,p2): if p1 == p2: return '0'

Notes from prior hwks… Warnings! def rps(p1,p2): """ rock-paper-scissors judger """ if 'p1' == 'p2': return 0 def letterscore(let): if let in 'zq': return 10 …(lots more)… def rps(p1,p2): """ rock-paper-scissors judger """ if p1 == p2: return '0' The string 'p1' is not the same as the variable p1 ! The string '0' is not the same as the number 0 ! Capital letters count! (It should be letterScore.) no docstring!

What CS is really about recursion variables if…elif…else making decisions storage repeated actions “high” or “low” int guess 42 thinking like a machine sequences 'w''a''r''t' str s[0] str s[1] str s[2] str s[3]

thinking like a machinethinking for a machine What CS is really about recursion variables if…elif…else making decisions storage repeated actions “high” or “low” int guess 42 sequences str s[0] str s[1] str s[2] str s[3] 'w''a''r''t' deciding how to use these tools library functions creating your own tools... classes creating your own data structures... (later)

Top-down program design Given: a description of the problem Wanted: a function that solves it translation!

Top-down program design Given: a description of the problem 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) Wanted: a function that solves it Are these tasks still too big? if so, go to step 1… What do you need for each? 1. Visualize what the function will do... translation! with as much detail as possible variables, lists, if…elif…else, recursion

Top-down program design Given: a description of the problem 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) Wanted: a function that solves it Are these tasks still too big? if so, go to step 1… What do you need for each? 1. Visualize what the function will do... translation! with as much detail as possible variables, lists, if…elif…else, recursion How to do this…

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?

Monte Carlo Monty Hall How can we write MCMH? What data do we need to keep track of? What is the input/output of your function?

Monte Carlo Monty Hall How can we write MCMH? What specific actions does your function need to take?

Monte Carlo Monty Hall How can we write MCMH? Put it all together into an algorithm…

Monte Carlo Monty Hall 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 ) Then translate the algorithm to code!

Sorting a List What data do we need to keep track of? What is the input/output of the function?

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?

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 == L[0]: return L[1:] # remove this one else: return L[0:1] + removeOne(e,L[1:]) # keep the non-e element and then keep going removeOne(42, [5,7,42,8,42]) [5,7,8,42] removeOne('p', 'computer programming') 'comuter programming'

max A recipe for life ? The hard part is knowing what we want to maximize! and python already has it for us…

to the max If we want the highest price… What if the months are in there, as well? max( [449.5, 580.0, 562.4, 481.3, 498.3, 414.5] ) 'sep''aug''jul''jun''may''apr' max([ [449.5,'apr'], [580.0,'may'], [562.4,'jun'], [481.3,'jul'], [498.3,'aug'], [414.5,'sep'] ]) Google Inc

"Best" word def scrabbleScore(w): # see homework #1! def bestWord( L ): """ finds the "best" word from L, a list of words here, "best" means highest scrabble score """ Let's abbreviate this function as scsc(w)

"Best" word def scrabbleScore(w): # see homework #1! def bestWord( L ): """ finds the "best" word from L, a list of words here, "best" means highest scrabble score """ if len(L) < 2: return elif return else: return Let's abbreviate this function as scsc(w)

"Best" word def scrabbleScore(w): # see homework #1! def bestWord( L ): """ finds the "best" word from L, a list of words here, "best" means highest scrabble score """ if len(L) < 2: return L[0] elif scsc(L[0]) < scsc(L[1]): return bestWord( L[1:] ) else: return bestWord( L[0:1] + L[2:] ) Let's abbreviate this function as scsc(w)

A suggestion 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 def scrabbleScore(w): # see homework #1! Let's abbreviate this function as scsc(w)

The last word on bestWord def scrabbleScore(w): # see homework #1! def bestWord( L ): """ finds the "best" word from L, a list of words here, "best" means highest scrabble score """ if len(L) < 2: return L[0] elif scsc(L[0]) < scsc(L[1]): return bestWord( L[1:] ) else: return bestWord( L[0:1] + L[2:] ) 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 ] bestPair = max( LOL ) return bestPair using raw recursion using max

Examples >>> bestNumber( [ 10, 20, 30, 40, 50, 60, 70 ] ) 40 >>> bestNumber( [ 100, 200, 300, 400 ] ) 100 >>> bestNumber( [ 1, 2, 3, 4, 5, 6, 7, 8, 7 ] ) 8 >>> mode( [ 1, 2, 3, 4, 5, 6, 7, 8, 7 ] ) 7 What is bestNumber ? mode ?

"Quiz" def bestNumber( L ): """ returns the # in L closest to 42 """ def mode( L ): """ returns the element appearing most often in L """ Name(s): Nothing but the best! Hint: Consider defining a helper function ! Hints: abs( x ) is built-in to Python Use bestWord as a guide: Write this function using max/min or recursively : Write this function however you like: def bestWord( L ): """ example code """ LOL = [ [scsc(w), w] for w in L ] bestPair = max( LOL ) return bestPair

"Quiz" Solutions… def bestNumber( L ): """ returns the # in L closest to 42 ""“ LOL = [ [abs(w-42), w] for w in L ] bestPair = min( LOL ) return bestPair[1] Write this function using max/min : Hints: abs( x ) is built-in to Python Use bestWord as a guide: def bestWord( L ): """ example code """ LOL = [ [scsc(w), w] for w in L ] bestPair = max( LOL ) return bestPair

"Quiz" Solutions… def mode( L ): """ returns the element appearing most often in L """ LOL = [[numberOfTimes(w,L),w] for w in L] return max(LOL)[1] Hint: Consider defining a helper function ! Write this function however you like: def numberOfTimes( w, L ): """ returns the # in times w repeats in L """ return sum([k==w for k in L])

sort(L) def sort( L ): """ a list of elements in L, sorted from hi to low """ if len(L) < 1: return L else:

sort(L) def sort( L ): """ a list of elements in L, sorted from hi to low """ if len(L) < 1: return L else: return [max(L)] + sort(removeOne( max(L), L ))

sort(L, maxFun) def sort( L, maxFun ): """ a list of elements in L, sorted using maxFun """ if len(L) < 1: return L else: return

sort(L, maxFun) def sort( L, maxFun ): """ a list of elements in L, sorted using maxFun """ if len(L) < 1: return L else: return [maxFun(L)] + sort(removeOne( maxFun(L), L )) Will this work?

sort(L, maxFun) def sort( L, maxFun ): """ a list of elements in L, sorted using maxFun """ if len(L) < 1: return L else: return [maxFun(L)] + sort(removeOne( maxFun(L), L ), maxFun)

sort(L, maxFun) def sort( L, maxFun ): """ a list of elements in L, sorted using maxFun """ if len(L) < 1: return L else: return [maxFun(L)] + sort(removeOne( maxFun(L), L ), maxFun) What happens if you call >>>sort( L, min )

See you in Lab !