Filter Array
Filter Array
Darshit_Shah + 0 comments Another solution approach in Scala using
for
expression:for (i <- arr if i < delim) yield i
nevertiree + 3 comments Haskell
f n arr = filter (< n) arr
har33sh + 0 comments f n arr = [ a | a <- arr , a < n ]
gabrielhuff + 0 comments f = filter . (>)
matharumanpreet + 0 comments anytime you have same dangling arguments at the end on both sides, you can get rid of it and be point free, like this
f n = filter (< n)
PitrDubovich + 0 comments In Scala, assuming we're not able to use
filter
, we could instead use the fact thatflatMap
will filter out values ofNone
.arr.flatMap(a => if (a < delim) Some(a) else None)
m0smith + 0 comments Another Clojure solution. Not lazy unfortunately.
(fn[delim lst] (reduce #(if (< %2 delim) (conj %1 %2) %1) [] lst))
metaculus + 0 comments Here is how to read the stdin for this question in Ocaml:
let f n arr = (* implementation *) let () = (* first line is parsed as the first arg to f *) let num = int_of_string (read_line ()) in (* the rest until EOF are parsed into an array for second arg *) f num ( let rec aux arr = try let input = read_line () in match input with | "" -> arr | a -> aux (arr @ [int_of_string a]) with End_of_file -> arr in aux [] )
JCrazafy + 1 comment I am not particularly convinced that the specifications are tested fully. I used list comprehension to circumvent the use of "filter" and it worked.
abhiranjan + 0 comments Hi @JCrazafy, thanks for pointing. I have reworded the constraint. It is suggested to write own implementation, not enforced.
denisdifazio + 0 comments Clojure:
(fn [delim lst] (filter #(< % delim) lst))
Technical_Rambo + 0 comments Scala : Create Either Single-Element List Or Empty List for each element based on condition and flatMap to merge them.
def f(delim:Int,arr:List[Int]):List[Int] = arr.flatMap(x=>if (x<delim) List(x) else List())
meincke911 + 1 comment An F# solution
open System [<EntryPoint>] let main args = let x = Console.ReadLine() |> int let numbers = Seq.initInfinite (fun _ -> Console.ReadLine()) |> Seq.takeWhile (String.IsNullOrWhiteSpace >> not) |> Seq.filter (fun v -> v |> int < x) |> Seq.iter (printf "%s\n") 0
travisbrat + 0 comments The following part which will be going to get the part that will be enable to manage the part accroding to the best way so for that it wil be hvaing the best to get the array code that firefox couldn't load xpcom represneted immeditaley recursion for it .
jb_gn + 0 comments I decided I didn't want to use the
Enum
library at all and implemented my Elixir solution using pure naive comprehensions. One of the interesting things about recursion is that the list is represented backwards in memory immediately aftermake_list
is run, but sincefilter
walks the list again and places the last element in the first position, everything comes back out in the correct order once it is time toprint
:Ddefmodule NaiveList do def print([]), do: nil def print([value | rest]) do IO.puts(value) print(rest) end def filter(limit, list), do: filter(limit, list, []) def filter(_limit, [], output), do: output def filter(limit, [value | rest], output) when value < limit, do: filter(limit, rest, [value | output]) def filter(limit, [_value| rest], output), do: filter(limit, rest, output) def make_list(), do: make_list(IO.gets(""), []) defp make_list(""<>number, list), do: make_list(IO.gets(""), [(number |> String.trim |> String.to_integer) | list]) defp make_list(:eof, list), do: list end n = IO.gets("") |> String.trim |> String.to_integer list = NaiveList.make_list() NaiveList.filter(n, list) |> NaiveList.print
Sort 74 Discussions, By:
Please Login in order to post a comment