Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; CHF - swiss franc -using block approach
- ;; cc amount 7 - number of coin types
- (define (count-change amount)
- (define (cc amount kinds-of-coins)
- (cond ((= amount 0) 1)
- ((or (< amount 0) (= kinds-of-coins 0)) 0)
- (else (+ (cc amount
- (- kinds-of-coins 1))
- (cc (- amount
- (first-denomination kinds-of-coins))
- kinds-of-coins)))))
- (define (first-denomination kinds-of-coins)
- (cond ((= kinds-of-coins 1) 5)
- ((= kinds-of-coins 2) 10)
- ((= kinds-of-coins 3) 20)
- ((= kinds-of-coins 4) 50)
- ((= kinds-of-coins 5) 100)
- ((= kinds-of-coins 6) 200)
- ((= kinds-of-coins 7) 500)))
- (cc amount 7))
- ;; lets try it out
- (count-change 300)
- ;; expected 1022
- (count-change 301)
- ;; expected 0 because no pennies
- ;; alternative coin order - shouldn't make a difference - lets be sure
- (define (count-change-2 amount)
- (define (cc amount kinds-of-coins)
- (cond ((= amount 0) 1)
- ((or (< amount 0) (= kinds-of-coins 0)) 0)
- (else (+ (cc amount
- (- kinds-of-coins 1))
- (cc (- amount
- (first-denomination kinds-of-coins))
- kinds-of-coins)))))
- (define (first-denomination kinds-of-coins)
- (cond ((= kinds-of-coins 1) 500)
- ((= kinds-of-coins 2) 5)
- ((= kinds-of-coins 3) 50)
- ((= kinds-of-coins 4) 100)
- ((= kinds-of-coins 5) 20)
- ((= kinds-of-coins 6) 200)
- ((= kinds-of-coins 7) 10)))
- (cc amount 7))
- ;; lets try it out
- (count-change-2 300)
- ;; expected 1022
- (count-change-2 301)
- ;; expected 0 because no pennies
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement