Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let rec sumListBy money coins highestCoinUsed xs =
- match xs with
- | x::xs -> countChangeImpl (money - x) coins (max x highestCoinUsed) + sumListBy money coins highestCoinUsed xs
- | [] -> 0
- and filterList highestCoinUsed xs ys =
- match xs with
- | x::xs when x >= highestCoinUsed -> filterList highestCoinUsed xs (x::ys)
- | _::xs -> filterList highestCoinUsed xs ys
- | [] -> ys
- and countChangeImpl (money: int) (coins: int list) (highestCoinUsed: int) =
- match money with
- | 0 -> 1
- | money when money < 0 -> 0
- | money ->
- let usableCoins = filterList highestCoinUsed coins []
- sumListBy money coins highestCoinUsed usableCoins
- let countChange money coins = countChangeImpl money coins 0
- let countChangeTest money coins expected =
- let actual = countChange money coins
- printf "money = %d; coins = %A; expected %d; actual %d\n" money coins expected actual
- [<EntryPoint>]
- let main argv =
- countChangeTest 301 [5;10;20;50;100;200;500] 0
- countChangeTest 300 [500;5;50;100;20;200;10] 1022
- countChangeTest 30 [10;20;100;50;5] 6
- countChangeTest 4 [1;2] 3
- System.Console.ReadLine() |> ignore
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement