Advertisement
pellekrogholt

Untitled

Sep 27th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.77 KB | None | 0 0
  1. ;; CHF - swiss franc -using block approach
  2.  
  3. ;; cc amount 7 - number of coin types
  4. (define (count-change amount)
  5.   (define (cc amount kinds-of-coins)
  6.     (cond ((= amount 0) 1)
  7.           ((or (< amount 0) (= kinds-of-coins 0)) 0)
  8.           (else (+ (cc amount
  9.                        (- kinds-of-coins 1))
  10.                    (cc (- amount
  11.                           (first-denomination kinds-of-coins))
  12.                        kinds-of-coins)))))
  13.   (define (first-denomination kinds-of-coins)
  14.     (cond ((= kinds-of-coins 1) 5)
  15.           ((= kinds-of-coins 2) 10)
  16.           ((= kinds-of-coins 3) 20)
  17.           ((= kinds-of-coins 4) 50)
  18.           ((= kinds-of-coins 5) 100)
  19.           ((= kinds-of-coins 6) 200)        
  20.           ((= kinds-of-coins 7) 500)))
  21.   (cc amount 7))
  22.  
  23. ;; lets try it out
  24. (count-change 300)
  25. ;; expected 1022
  26.  
  27. (count-change 301)
  28. ;; expected 0 because no pennies
  29.  
  30.  
  31.  
  32. ;; alternative coin order - shouldn't make a difference - lets be sure
  33. (define (count-change-2 amount)
  34.   (define (cc amount kinds-of-coins)
  35.     (cond ((= amount 0) 1)
  36.           ((or (< amount 0) (= kinds-of-coins 0)) 0)
  37.           (else (+ (cc amount
  38.                        (- kinds-of-coins 1))
  39.                    (cc (- amount
  40.                           (first-denomination kinds-of-coins))
  41.                        kinds-of-coins)))))
  42.   (define (first-denomination kinds-of-coins)
  43.     (cond ((= kinds-of-coins 1) 500)
  44.           ((= kinds-of-coins 2) 5)
  45.           ((= kinds-of-coins 3) 50)
  46.           ((= kinds-of-coins 4) 100)
  47.           ((= kinds-of-coins 5) 20)
  48.           ((= kinds-of-coins 6) 200)        
  49.           ((= kinds-of-coins 7) 10)))
  50.   (cc amount 7))
  51.  
  52. ;; lets try it out
  53. (count-change-2 300)
  54. ;; expected 1022
  55.  
  56. (count-change-2 301)
  57. ;; expected 0 because no pennies
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement