Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Potter Kata: http://codingdojo.org/cgi-bin/index.pl?KataPotter
- [<Measure>] type EUR
- let bookPrice = 8M<EUR>
- let priceOfSeries = function
- | 1 -> bookPrice
- | 2 -> 2M * bookPrice * 0.95M
- | 3 -> 3M * bookPrice * 0.90M
- | 4 -> 4M * bookPrice * 0.80M
- | 5 -> 5M * bookPrice * 0.75M
- | n -> invalidOp (sprintf "Invalid set of %d" n)
- let priceOfSets = Seq.sumBy (Set.count >> priceOfSeries)
- let canAdd book series = series |> Set.contains book |> not
- let computeValidAdditions book sets =
- let rec compute front = function
- | [] -> []
- | head::tail ->
- let newFront = head::front
- if canAdd book head
- then
- let newSeries = Set.add book head
- let validSet = newSeries::front@tail
- validSet :: compute newFront tail
- else compute newFront tail
- sets |> compute []
- let toSets books =
- books |> Seq.fold (fun sets book ->
- let additions = computeValidAdditions book sets
- let newSets =
- if additions.Length = 0
- then [set [book]::sets]
- else additions
- newSets
- |> List.map (fun sets -> sets, priceOfSets sets)
- |> List.minBy snd
- |> fst
- ) [Set.empty]
- toSets [0;0;1;1;2;2;3;4;]
- |> priceOfSets
- // val it : decimal<EUR> = 51.20M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement