• + 0 comments

    Scala: given x -> f(x), if x1 = x2 then f(x1) = f(x2), if f(x1) != f(x2), it is not a function. This is the code in scala:

    import scala.io.StdIn
    
    object Solution {
    
        def main(args: Array[String]) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
    */
            var count_tests = StdIn.readInt
                
            for(j <- List.range(0,count_tests)){
                var input = StdIn.readInt
                var relations = collection.mutable.Map[Int, Int]()
                var result = true
                                        
                for(i <- List.range(0,input)){
                    var test_arr = StdIn.readLine()
                    var test1 = test_arr.split(" ")(0).toInt
                    var test2 = test_arr.split(" ")(1).toInt
                    
                    if (!relations.contains(test1)) 
                        relations = relations + (test1 -> test2)
                    else {
                        if(relations(test1) != test2)
                            result = false                    
                    }
                        
                }
                if(result)
                    println("YES")
                else
                    println("NO")
            }
        }
    }