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.98 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.97 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.96 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.95 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.94 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.93 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.92 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.91 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.90 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.89 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.88 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.87 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.86 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.85 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.84 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.83 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.82 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.81 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.80 tracks/fsharp/exercises/book-store/Example.fs
trackler-2.2.1.79 tracks/fsharp/exercises/book-store/Example.fs