# lab3 - CS 61A Laboratory Solutions Week 3 1. How to reverse...

This preview shows pages 1–2. Sign up to view the full content.

Sheet1 Page 1 CS 61A Laboratory SolutionsWeek 3 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) (else (+ (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins) (cc amount (+ KINDS-OF-COINS 1)))))) 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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## lab3 - CS 61A Laboratory Solutions Week 3 1. How to reverse...

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

View Full Document
Ask a homework question - tutors are online