CS61A Lecture 5 2011-06-27 Colleen Lewis. Clicker Query What else are you doing this summer? A)Taking another class B)Working C)Taking another class and.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
Slide 1 CS3: Lecture 8 Advanced recursion. Slide 2 Schedule Oct 10Advanced recursion Oct 17Number-spelling Miniproject Oct 24Higher order procedures Oct.
Slide 1 CS3 Week 8: Recursion. Slide 2 Midterm 1 You did great If you need a re- grade, see the person that graded that question Solutions are available.
Slide 1. Slide 2 Administrivia Mid-semester survey this week (Thur/Fri) –you NEED to do this. Interviews should be starting in a few weeks. I apologize.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 12: Homework stuff and Accumulating Recursion.
Recursion. Binary search example postponed to end of lecture.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
Slide 1 Midterm 1 Midterm 1: Feb 28 th (next week). –In the lecture slot, plus 20 minutes (4:10-5:30 pm, 120 Latimer) –Everything we’ve covered, including.
Slide 1. Slide 2 Midterm 1 You did great If you need a regrade, see the person that graded that question Solutions available on the portal soon.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 8: Recursion.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 9: Recursion Rocks!
Midterm Logistics Where? 2050 VLSB When? Monday, 4:10 to 5:40 What to do? –“Review problems” from Thursday/Friday in UCWise. –Two practice exams and solutions.
Section 2.6 Question 1. Section 2.6 Answer 1 Section 2.6 Question 2.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 15: Procedures as Arguments.
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 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Induction and recursion
EXAMPLE 4 Use Pascal’s triangle School Clubs The 6 members of a Model UN club must choose 2 representatives to attend a state convention. Use Pascal’s.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Sparse & Redundant Representation Modeling of Images Problem Solving Session 1: Greedy Pursuit Algorithms By: Matan Protter Sparse & Redundant Representation.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
CS61A Lecture Colleen Lewis. Clicker Test How often do you read piazza posts? A)Whenever I receive an B)Once or twice a day C)When.
CS2852 Week 6, Class 1 Today The run-time stack Writing and proving recursive methods SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CS2852 Week 6, Class 2 Today Class exercise: Implementing a recursive method Binary Search Trees Tomorrow: Quiz at start of lab Implementing a recursive.
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
CS61A Lecture Colleen Lewis. Clicker Test When do you think homework should be due? A)10pm B)11pm C)Midnight D)11am the next day.
CS61A Lecture Colleen Lewis. Clicker poll How do you feel about the “talk to your neighbor” stuff during lecture A)Like it B)It is okay.
CS61A Lecture Colleen Lewis 1. Clicker poll What chat client do you use? A)Trillian B)GChat C)iChat D)Facebook chat E)Other Don’t vote if.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
CS61A Lecture Colleen Lewis. Clicker Test Are you coming to the potluck on Thursday on the 4 th floor of Soda from 6:30pm-9:00? A)Yes B)Maybe.
CS61A Lecture Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
(Proof By) Induction Recursion
Java Software Structures: John Lewis & Joseph Chase
Data Structures and Algorithms for Information Processing
Counting Money Directions: 1. Do not view slide show.
This Lecture Substitution model
This Lecture Substitution model
This Lecture Substitution model
Exam Strategies Poetry.
Recursion Examples.
Lecture 6 - Recursion.
Presentation transcript:

CS61A Lecture Colleen Lewis

Clicker Query What else are you doing this summer? A)Taking another class B)Working C)Taking another class and working D)Other E)Nothing

How many ways are there to make 50¢? 50 ways

first-denomination Solution (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) ((= kinds-of-coins 2) 5) ((= kinds-of-coins 3) 10) ((= kinds-of-coins 4) 25) ((= kinds-of-coins 5) 50))) STk> (first-denomination 5) 50 STk> (first-denomination 3) 10 STk> (first-denomination 1) 1

count-change (define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) … (first-denomination kinds-of-coins) …

cc base cases (define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) STk> (cc _______ 5) 1 0 STk> (cc 20 _______) 0 How many have you figured out? A)0 B)1 C)2 D)3

Base Cases How many ways can you count $0.00 in change using 5 types of coins? – 1 way -$0.20 in change using 5 types of coins? – 0 ways $1.00 in change using 0 types of coins? – 0 ways

cc base cases Solution (define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) STk> (cc _______ 5) 1 STk> (cc _______ 5) 0 STk> (cc 20 _______)

count-change base cases (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((< amount 0) 0) ((= kinds-of-coins 0) 0) (else (+ ((or (< amount 0)(= kinds-of-coins 0)) 0)

Designing the recursion Delegate (make a recursive call): Assuming you use the biggest coin (w/ highest value) Assuming you don’t use the biggest coin Example: How many ways can you make 5 cents?

Use biggest coin Don’t use biggest coin

Woah! The number of coins didn’t change! Woah! The amount didn’t change!

Continuing from previous slide

amount in each recursive call

(cc 5 5) (cc -45 5) (cc 5 4) (cc -20 4) (cc 5 3) (cc -5 3) (cc 5 2) (cc 0 2) (cc 5 1) (cc 4 1) (cc 3 1) (cc 2 1) (cc 1 1) (cc 0 1) (cc 1 0) (cc 2 0) (cc 3 0) (cc 4 0) (cc 5 0) each recursive call

count-change recursive cases (define (cc amount kinds-of-coins) … (+ (cc (- amount (first-denomination kinds-of-coins) ) kinds-of-coins) (cc amount (- kinds-of-coins 1)))))) Blank 1 How many have you figured out? A)0 B)1 C)2 D)3 E)4 Blank 2

(define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((or (< amount 0) (= kinds-of-coins 0)) 0) (else (+ (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins) (cc amount (- kinds-of-coins 1)))))) (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) ((= kinds-of-coins 2) 5) ((= kinds-of-coins 3) 10) ((= kinds-of-coins 4) 25) ((= kinds-of-coins 5) 50))) Woah! This is overwhelming!

Reading Code (Like analyzing a poem) Start with simple parts – e.g. ( first-denomination) Read, re-read, re-read, re-read, test, re-read – Re-read specific sections – Highlight things you think are important – Write down your hypotheses after each reading Tests your hypothesis when possible – You’re not going to understand it the first or third time you read it! Same is true for project specs!!!!

Iterative versus Recursive Process (all of which use recursion!)

Remove all non-even numbers from a sentence STk>(evens ’ (2 8 3)) (2 8) ’ () (evens ’ ( )) (se 2 (evens ’ ( 8 3 )) (se 8 (evens ’ ( 3 )) (se (evens ’ ()) (se 2 (se 8 (se ’ ()))) Recursive process Once I hit the base case I’m not “done”

evens (define (evens sent) (cond ((empty? sent) ’ ()) ((even? (first sent)) (se (first sent) (evens (bf sent)))) (else (evens (bf sent))))) Waiting to do se

Remove all non-even numbers from a sentence STk>(evens2 ’ (2 8 3) ) (2 8) ’ (2 8) (evens-iter ’ ( ) ’ ()) (evens-iter ’ ( 8 3 ) ’ (2)) (evens-iter ’ ( 3 ) ’ (2 8)) (evens-iter ’ () ’ (2 8)) Iterative process Once I hit the base case I’m DONE! Iterative process Keep track of your answer as you go!

evens2 Solution (define (evens2 sent) (define (evens-iter sent answer) (if (empty? sent) answer (evens-iter (bf sent) (se answer (first sent ))))) (evens sent '())) Do you like this version better? A) Yes B) No

Factorial (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) Does this generate: A)A recursive process B)An iterative process Correct Answer

Factorial STk>(factorial 4) 24 1 (factorial 4) (* 4 (factorial 3) (* 3 (factorial 2) (* 2 (factorial 1) (* 4 (* 3 (* 2 1))) Recursive process Once I hit the base case I’m not “done”

Remove all non-even numbers from a sentence STk>(factorial 4) 24 (fact-iter 4 1) (fact-iter 3 4) (fact-iter 2 12) (fact-iter 1 24) Iterative process Once I hit the base case I’m DONE! Iterative process Keep track of your answer as you go!

factorial version 2 (define (factorial n) (define (fact-iter n answer) (if (= n 1) answer (fact-iter (- n 1) (* n answer)))) (fact-iter n 1)) Do you like this version better? A) Yes B) No

Pascal’s Triangle R: 0 R: 1 R: 2 R: 3 R: 4 R: 5 R: 6 C: 0 C: 1 C: 2 C: 3 C: 4 C: 5 R5C3 = R4C2 + R4C3

Recursion (Cont.) (define (pascal row col) (cond ((= col 0) 1) ((= col row) 1) (else (+ (pascal (- row 1) (- col 1)) (pascal (- row 1) col)))) Does this generate: A)A recursive process B)An iterative process R5C3 = R4C2 + R4C3 (R,C) = (R-1,C-1) + (R-1, C) Correct Answer

Summary count-change an example of: – Practicing learning to read and trace code – Tree recursion multiple recursive calls in a single case Recursive vs. Iterative Processes – All of them are implemented with recursion! – Recursive processes aren’t done at the base case – Iterative processes keep their answer with them