HR F#: Filter Array
The next HackerRank functional problem is Filter Array.
Filter a given array of integers and output only those values that are less than a specified value X.
The idea is to create a filtering function, not using the one from the standard library. With what is already known from solving previous problem[1], following the recommended method signature should not be difficult.
The recommended signature is upper limit and list, returns list.
Instead of lists, let's use the sequence:
let f x s =
seq { for v in s do if v < x then yield v }
readItemsFromInput
was introduced in the solution for the problem "List Replication".
The solution:
open System
let f x s =
seq { for v in s do if v < x then yield v }
let rec readItemsFromInput () = seq {
let line = Console.ReadLine ()
match line with
| null -> ()
| _ -> yield line |> int
yield! readItemsFromInput ()
}
[<EntryPoint>]
let main argv =
let x = Console.ReadLine () |> int
let items = readItemsFromInput ()
items
|> f x
|> Seq.iter (fun v -> printfn "%A" v)
0 // return an integer exit code
HR F#: List Replication, GHOST_URL/hr-f-list-replication/ ↩︎