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.180 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.179 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.178 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.177 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.176 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.175 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.174 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.173 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.172 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.171 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.170 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.169 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.167 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.166 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.165 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.164 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.163 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.162 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.161 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.160 tracks/fsharp/exercises/book-store/Example.fs