Skip to content
loading...

re: Daily Challenge #137 - Help the Bookseller VIEW POST

FULL DISCUSSION
 

Another F# one.

(But as I read it, the return value should be a string, not a list, and therefore a little input validation is needed to handle the input arrays being empty).

let stockByCategory(books: string[]) (cats: string[]): string =
    if books = [||] || cats = [||] then
        ""
    else
        cats
        |> Seq.map
            (fun cat -> books |> Seq.filter (fun book -> book.StartsWith cat)
                              |> Seq.sumBy(fun book -> book.Split(' ').[1] |> int)
                              |> (sprintf "(%s : %d)" cat))
        |> String.concat " - "
 

You're right about the requirements, should have been a string. 🤷‍♂️

code of conduct - report abuse