We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  1. Practice
  2. Functional Programming
  3. Introduction
  4. Filter Array
  5. Discussions

Filter Array

  • Problem
  • Submissions
  • Leaderboard
  • Discussions

Sort 74 Discussions, By:

votes
  • recency
  • votes

Please Login in order to post a comment

  • Darshit_Shah 9 months ago+ 0 comments

    Another solution approach in Scala using for expression:

    for (i <- arr if i < delim) yield i
    
    4|
    Permalink
  • nevertiree 2 years ago+ 3 comments

    Haskell

    f n arr = filter (< n) arr
    
    4|
    Permalink
    • har33sh 2 years ago+ 0 comments

      f n arr = [ a | a <- arr , a < n ]

      7|
      ParentPermalink
    • gabrielhuff 7 months ago+ 0 comments

      f = filter . (>)

      2|
      ParentPermalink
    • matharumanpreet 3 months ago+ 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)

      1|
      ParentPermalink
  • PitrDubovich 1 year ago+ 0 comments

    In Scala, assuming we're not able to use filter, we could instead use the fact that flatMap will filter out values of None.

    arr.flatMap(a => if (a < delim) Some(a) else None)
    
    3|
    Permalink
  • m0smith 2 years ago+ 0 comments

    Another Clojure solution. Not lazy unfortunately.

    (fn[delim lst] (reduce #(if (< %2 delim) (conj %1 %2) %1) [] lst))
    
    3|
    Permalink
  • metaculus 1 year ago+ 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 [] )
    
    2|
    Permalink
  • JCrazafy 4 years ago+ 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.

    2|
    Permalink
    • abhiranjan 4 years ago+ 0 comments

      Hi @JCrazafy, thanks for pointing. I have reworded the constraint. It is suggested to write own implementation, not enforced.

      1|
      ParentPermalink
  • denisdifazio 6 months ago+ 0 comments

    Clojure:

    (fn [delim lst] (filter #(< % delim) lst))
    
    1|
    Permalink
  • Technical_Rambo 1 year ago+ 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())
    
    1|
    Permalink
  • meincke911 1 year ago+ 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
    
    1|
    Permalink
    • travisbrat 4 weeks ago+ 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 .

      0|
      ParentPermalink
  • jb_gn 1 year ago+ 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 after make_list is run, but since filter 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 to print :D

    defmodule 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
    
    1|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature