week3 - CS 61A Homework and Laboratory Solutions Week 3...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 61A Homework and Laboratory Solutions Week 3 LABORATORY ASSIGNMENT: 1. How to reverse the order in which coins are tried? One solution is to reverse the order of the numbers in FIRST-DENOMINATION: (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 50) ((= kinds-of-coins 2) 25) ((= kinds-of-coins 3) 10) ((= kinds-of-coins 4) 5) ((= kinds-of-coins 5) 1))) Another would be, using the original version of first-denomination, to change COUNT-CHANGE and CC so that the variable KINDS-OF-COINS counts upward instead of downward: (define (count-change amount) (cc amount 1)) (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((or (< amount 0) (> KINDS-OF-COINS 5)) 0) ; changed here (else (+ (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins) (cc amount (+ KINDS-OF-COINS 1)))))) ; changed here 2. Explore the efficiency. Here is what happens with (cc 5 2) in the original order, as in the book: > (cc 5 2) "CALLED" cc 5 2 "CALLED" cc 0 2 "CALLED" cc 5 1 "CALLED" cc 4 1 "CALLED" cc 3 1 "CALLED" cc 2 1 "CALLED" cc 1 1 "CALLED" cc 0 1 "CALLED" cc 1 0 "CALLED" cc 2 0 "CALLED" cc 3 0 "CALLED" cc 4 0 "CALLED" cc 5 0 2 (I've deleted the "RETURNED" lines in the trace; it's easier to read this way and doesn't affect our analysis.) To try out just pennies and nickels backwards, I modified first-denomination this way: (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 5)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
((= kinds-of-coins 2) 1))) And here are the results: > (cc 5 2) "CALLED" cc 5 2 "CALLED" cc 4 2 "CALLED" cc 3 2 "CALLED" cc 2 2 "CALLED" cc 1 2 "CALLED" cc 0 2 "CALLED" cc 1 1 "CALLED" cc -4 1 "CALLED" cc 1 0 "CALLED" cc 2 1 "CALLED" cc -3 1 "CALLED" cc 2 0 "CALLED" cc 3 1 "CALLED" cc -2 1 "CALLED" cc 3 0 "CALLED" cc 4 1 "CALLED" cc -1 1 "CALLED" cc 4 0 "CALLED" cc 5 1 "CALLED" cc 0 1 "CALLED" cc 5 0 2 We get the same answer, but with 21 calls to CC instead of 13. Why? The extra calls are for attempts to match a small amount of money with a large coin -- for example, to use a nickel in counting four cents. When the coins are tried in the book's order, by the time we are thinking about four cents, we have already abandoned the idea of using nickels and so we quickly find the four-pennies solution. But in backward order, we have to discover that a nickel is too big for four cents, and then that a nickel is too big for three cents, and so on. (These situations give rise to the calls with a negative amount of money; notice that there aren't any like that in the first trace.) 3. Modify CC to take a sentence of denominations. (define (cc amount kinds-of-coins)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

week3 - CS 61A Homework and Laboratory Solutions Week 3...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online