Sha256: e5e29931371165bcabf31a5d724011508472d0c4a0350ee512fdc4edceebe1b3

Contents?: true

Size: 1.38 KB

Versions: 134

Compression:

Stored size: 1.38 KB

Contents

module BookStore

let private costPerGroup groupSize =
    let discountPercentage =
        match groupSize with
        | 1 ->  0.
        | 2 ->  5.
        | 3 -> 10.
        | 4 -> 20.
        | 5 -> 25.
        | _ -> failwith "Invalid group size"

    8. * (groupSize |> float) * (100. - discountPercentage) / 100.

let private remove n list =
    let rec removeTail n list acc =
        match list with
        | x::xs when x = n -> List.append (List.rev acc) xs
        | x::xs            -> removeTail n xs (x::acc)
        | []               -> List.rev acc
    removeTail n list []

let rec private calculateTotalCostHelper books priceSoFar =
    match books |> List.length with
    | 0 -> priceSoFar
    | _ ->
        let groups =
            books
            |> List.groupBy id
            |> List.map fst
        
        let prices =
            [1 .. groups |> List.length]
            |> List.map (fun i ->
                let itemsToRemove =
                    groups
                    |> List.take i

                let remaining =
                    itemsToRemove
                    |> List.fold (fun state t ->
                        remove t state)
                        books

                calculateTotalCostHelper remaining (priceSoFar + costPerGroup i)
            )
        
        prices
        |> List.min

let total books =
    calculateTotalCostHelper books 0.

Version data entries

134 entries across 134 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.118 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.117 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.116 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.115 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.114 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.113 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.111 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.110 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.109 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.108 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.107 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.106 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.105 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.104 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.103 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.102 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.101 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.100 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.99 tracks/fsharp/exercises/book-store/Example.fs