Advertisement
Guest User

Untitled

a guest
Sep 19th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let rec sumListBy money coins highestCoinUsed xs =
  2.     match xs with
  3.     | x::xs -> countChangeImpl (money - x) coins (max x highestCoinUsed) + sumListBy money coins highestCoinUsed xs
  4.     | [] -> 0
  5. and filterList highestCoinUsed xs ys =
  6.     match xs with
  7.     | x::xs when x >= highestCoinUsed -> filterList highestCoinUsed xs (x::ys)
  8.     | _::xs -> filterList highestCoinUsed xs ys
  9.     | [] -> ys
  10. and countChangeImpl (money: int) (coins: int list) (highestCoinUsed: int) =
  11.     match money with
  12.     | 0 -> 1
  13.     | money when money < 0 -> 0
  14.     | money ->
  15.         let usableCoins = filterList highestCoinUsed coins []
  16.         sumListBy money coins highestCoinUsed usableCoins
  17.  
  18. let countChange money coins = countChangeImpl money coins 0
  19.  
  20. let countChangeTest money coins expected =
  21.     let actual = countChange money coins
  22.     printf "money = %d; coins = %A; expected %d; actual %d\n" money coins expected actual
  23.  
  24. [<EntryPoint>]
  25. let main argv =
  26.     countChangeTest 301 [5;10;20;50;100;200;500] 0
  27.     countChangeTest 300 [500;5;50;100;20;200;10] 1022
  28.     countChangeTest 30 [10;20;100;50;5] 6
  29.     countChangeTest 4 [1;2] 3
  30.     System.Console.ReadLine() |> ignore
  31.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement