Berkeley COMPSCI 3 - Lecture 5: Recursion Midterm-like problems

Unformatted text preview:

CS3: Introduction to Symbolic ProgrammingFall 2006 Nate [email protected] 5:RecursionMidterm-like problemsSpring 2006 CS3: 2 ScheduleLecture: finishing recursionLab: Miniproject #2Oct 16-208Lecture: Advanced RecursionLab: Recursion IIIOct 9-137Lecture: Midterm 1Lab: Recursion IIOct 2-66Lecture: Introduction to RecursionLab: RecursionSept 25-295Lecture: Data abstraction in DbDLab: Miniproject ISept 18-224Announcements•Nate's office hours:-Wednesday, 1:30-3:30, in 329 Soda-Special: Monday Oct 2nd, 1-3pm, in 329 Soda•Midterm next week! -(More on this in a bit)•Reading for this week-Simply Scheme, chapter 11-Difference between Dates, Recursive version-(These will be on the midterm)•Still having trouble working at home?-Go to 333 Soda hall !Spring 2006 CS3: 4 Drop day•The last day to drop is Sept 29- I thinkRecursion•Everyone thinks it's hard!-(well, it is… aha!-hard, not complicated-hard)•The first technique (in this class) to handle arbitrary length inputs.-There are other techniques, easier for some problems.•What is it?An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task.All recursion procedures need…1. Base Case (s)•Where the problem is simple enough to be solved directly2. Recursive Cases (s)1. Divide the Problem•into one or more smaller problems2. Invoke the function•Have it call itself recursively on each smaller part3. Combine the solutions•Combine each subpart into a solution for the whole(define (find-first-even sent) (if (even? (first sent)) (first sent) ;base case: return ; that even number (find-first-even (bf sent)) ;recurse on the ; rest of sent )) Problem: find the first even number in a sentence of numbers (if <test> (<do the base case>) (<do the recursive case>)Count the number of words in a sentence(define (count sent) (if (empty? (bf sent)) 1 ;base case: return 1 (+ 1 (count (bf sent)) ;recurse on the ; rest of sent))Base cases can be tricky•By checking whether the (bf sent) is empty, rather than sent, we won't choose the recursive case correctly on that last element!-Or, we need two base cases, one each for the last element being odd or even.•Better: let the recursive cases handle all the elementsYour book describes this well(define (count sent) (if (empty? sent ) 0 ;base case: return 0 (+ 1 (count (bf sent)) ;recurse on the ; rest of sent)) (define (count sent) (if (empty? (bf sent)) 1 ;base case: return 1 (+ 1 (count (bf sent)) ;recurse on the ; rest of sent)) Count the number of words in a sentence> (count '(a b c)) (+ 1 (+ 1 (+ 1 0)))  3(+ 1 (+ 1 (+ 1 0 sent = ( a b c )sent = ( b c )sent = ( c )sent = ( )Spring 2006 CS3: 13 (define (find-evens sent) (cond ((empty? sent) ;base case '() ) ((odd? (first sent)) ;rec case 1 (find-evens (bf sent)) ) (else ;rec case 2: even (se (first sent) (find-evens (bf sent))) ) )) (define (find-evens sent) (cond ( ;base case ) ( ;rec case 1: odd ) ( ;rec case 2: even ) )) Problem: find all the even numbers in a sentence of numbersSpring 2006 CS3: 14 > (find-evens '(2 3 4 5 6)) (se 2 (se 4 (se 6 ())))  (2 4 6)(se 2 (se 4 (se 6 () sent = ( 2 3 4 5 6 )sent = ( 3 4 5 6 )sent = ( 4 5 6 )sent = ( 5 6 )sent = ( 6 )sent = ( )Why is recursion hard?•ONE function:-replicates itself, -knows how to stop, -knows how to combine the “replications”•There are many ways to think about recursion: you absolutely do not need to understand all of them.-Knowing recursion WILL help with all sorts of ways while programming, even if you don’t often use it.Midterm 1: Oct 2nd (next week) -Location: 50 Birge -Time: In the lecture slot, plus 20 minutes -(4:10-5:30)-Open book, open notes. -Nothing that can compute, though-Everything we’ve covered, including the coming week on recursion.-Review session this Saturday, Sep 30th, 2-4pm. -430 Soda (Wozniak lounge). -Practice exam in reader (do this all at once)-Check Announcements for more practice items, solutionsSpring 2006 CS3: 17 Special midterm issues•Several of you have come to me with midterm conflicts-You need to email me (and get a response) or talk to me so I can get a count!Sample problem for midterm 1Consider a procedure named double that, given a word as argument, returns a two-word sentence. The first word is two. The second word is the result of adding an "s" to the end of the argument. (two boxs)(double 'box)(two buss)(double 'bus)(two apples)(double 'apple)intended resultexpressionNow consider some incorrect implementations of double. For each one, indicate what the call(double 'apple) will return. If no value is returned because the procedure crashes, give the error message that results.(define (double wd) (sentence 'two (wd 's)) ) (define (double wd) (sentence 'two (sentence wd s)) ) (define (double wd) (sentence 'two '(word wd s)))Spring 2006 CS3: 20 between?Write a procedure called between? which takes three numbers as arguments, and returns true if and only if the second argument is between and not equal to the first and the third:(between? 5 6 7) -> #t(between? 7 6 5) -> #tPart A: Write between? without using if or cond.Part B: Write between? without using and or or.Part C: Write a suite of test cases for between?. Make sure you test the possible sets of parameters exhaustively as possible, in order to test different ways the code could be written.Also, make sure you describe what the result of the call should


View Full Document

Berkeley COMPSCI 3 - Lecture 5: Recursion Midterm-like problems

Download Lecture 5: Recursion Midterm-like problems
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 5: Recursion Midterm-like problems and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 5: Recursion Midterm-like problems 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?