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.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Functional Programming
  3. Introduction
  4. Functions or Not?
  5. Discussions

Functions or Not?

Problem
Submissions
Leaderboard
Discussions

Sort 51 Discussions, By:

votes

Please Login in order to post a comment

  • Rhaseven7h 4 years ago+ 0 comments

    Might be helpful to know ...

    In case it is useful for someone, in the context of this challenge, a valid function is that which for a given input, ALWAYS gives the same output.


    VALID Function

    Given a function f() that functions as:

    f(1)=1
    f(2)=2
    f(3)=3
    f(2)=2
    f(4)=1000
    

    It is considered VALID, as outputs are always in a 1:1 relation with inputs, even although we don't know the exact way the relationship (function) works (i.e. we got no idea how f(4) gives 1000).


    INVALID Function

    A function g() which functions as follows:

    g(1)=1
    g(2)=333
    g(3)=89
    g(2)=777
    g(4)=1000
    

    The above function g, is NOT VALID, since, for input 2, we get two different results, first 333, then later we get 777.


    NOTE : It is worth noting that multiple inputs may give the same output:

    f(1)=99
    f(2)=99
    f(3)=99
    

    Would still be a valid function.

    67|
    Permalink
  • zylviij 4 years ago+ 0 comments

    Here is a template for haskell.

    import Control.Monad
    
    valid :: [(Int, Int)] -> Bool
    valid f = -- code goes here --
    
    main = do
        t <- fmap (read::String->Int) getLine
        forM [1..t] (\_->do
            n <- fmap (read::String->Int) getLine
            func <- forM [1..n] (\_->do fmap ((\[a, b]->(a,b)).map (read::String->Int).words) getLine :: IO (Int, Int))
            putStrLn $ if valid func then "YES" else "NO")
    
    29|
    Permalink
  • vestail 3 years ago+ 0 comments

    My strange scala solution.

    object Solution {
      def main(args: Array[String]) {
        val n = io.StdIn.readInt()
        (1 to n).foreach { i =>
          val k = io.StdIn.readInt()
          val list = (1 to k).map(x => io.StdIn.readLine.split("\\s+")(0)).toList
          if (list.distinct.size == list.size) println("YES")
          else println("NO")
        }
      }
    }
    
    8|
    Permalink
  • Tleilaxi 4 years ago+ 0 comments

    I think there is a problem in the input of the tests case of OCaml. Just doing the simple

    let a = read_int ();;
    

    that should normally read the first line of the input (the number of tests cases), fail with the exception

    "int_of_string"
    

    meaning that the conversion was impossible. There's probably an odd character or byte in the input that' s not displayed when it's printed, because when I copy/paste the inputs of the tests cases as displayed in my browser and run them with "Test against custom input", they execute without errors.

    4|
    Permalink
  • GeneralGrievous 4 years ago+ 0 comments

    This input is a bit tricky. My code may help F# folks.

    open System
    
    let readList n parser =
        Seq.initInfinite (fun _ -> Console.ReadLine())
        |> Seq.take n
        |> Seq.map parser
        |> Seq.toList
    
    let parseRelation (s: string) =
        match s.Split(' ') with
        | [|x|] -> int x, None
        | [|k; v|] -> int k, Some (int v)
        | _ -> failwith "OhGodWhy"
    
    let readTestCase () =
        let n = int <| Console.ReadLine()
    
        readList n parseRelation
    
    let isFunc relations =
        relations
        |> Map.ofList
        |> Map.toSeq
        |> Seq.length
        |> (=) (List.length relations)
    
    let boolStr = function
        | true -> "YES"
        | false -> "NO"
    
    [<EntryPoint>]
    let main args =
        let t = int <| Console.ReadLine()
    
        Seq.initInfinite (fun _ -> readTestCase ())
        |> Seq.take t
        |> Seq.map isFunc
        |> Seq.map boolStr
        |> Seq.iter (printfn "%s")
    
        0
    
    3|
    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