Sort by

recency

|

20 Discussions

|

  • + 0 comments
    object Solution {
        
        def solve(n: Int, input: Seq[Int]): Unit = {
          implicit val orderingIntSeq: Ordering[Seq[Int]] = (lsa: Seq[Int], lsb: Seq[Int]) =>
            implicitly[Ordering[Int]].compare(lsa.head, lsb.head)
    
          println(
            input.grouped(2).toList.groupBy(_.head)
              .filter(_._2.length == n)
              .view.mapValues(_.minBy(_.tail)).toMap
              .values
              .toList
              .sortBy(_.head)
              .map(_.mkString(" "))
              .reduce(_ + " " + _)
          )
        }
        
        def apply(): Unit = {
          val N: Int = io.StdIn.readLine().trim.toInt
    
          val inputList: Seq[Int] = (1 to N).flatMap { _ =>
            val input: Array[Int] =
              io.StdIn.readLine().trim.split(" ").map(_.toInt)
    
            input
          }
    
          solve(N, inputList)
        }
        
        def main(args: Array[String]) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
    */      Solution.apply()
        }
    }
    
  • + 0 comments

    I missed the issue articulation that the primes are undeniably requested from littlest to biggest, and had unnecessarily executed utilizing Guide rather than plain rundown coordinating site.

  • + 0 comments

    f# solution

    open System
    
    let toTuple [ a; b ] = (a, b)
    
    let q = Console.ReadLine() |> int
    
    [ 1..q ]
    |> List.map (fun _ ->
        Console.ReadLine()
        |> (fun s -> s.Split(" ") |> List.ofArray |> List.map int))
    |> List.map (fun l -> l |> List.chunkBySize 2 |> List.map toTuple)
    |> List.concat
    |> List.groupBy (fun (key, _) -> key)
    |> List.filter (fun (_, value) -> value |> List.length = q)
    |> List.map (fun (_, value) -> value |> List.min)
    |> List.map (fun (key, value) -> [ key; value ])
    |> List.concat
    |> List.map string
    |> String.concat " "
    |> Console.WriteLine
    
  • + 2 comments

    Haskell

    import Data.List (intercalate)
    
    gcdl :: [Int] -> [Int] -> [Int]
    gcdl [] xs = []
    gcdl xs [] = []
    gcdl xs@(x : xe : xtail) ys@(y : ye : ytail)
      | x < y = gcdl xtail ys
      | x > y = gcdl xs ytail
      | otherwise = x : min xe ye : gcdl xtail ytail
    
    main = do
      contents <- getContents
      let xss = [map read $ words l | l <- tail $ lines contents]
      putStrLn $ intercalate " " $ map show $ foldl1 gcdl xss
    
  • + 1 comment

    Solution in Haskell.

    These kinds of problems do not play nice with immutability

    import Data.List
    
    makeRowTuples :: [Char] -> [[Int]]
    makeRowTuples row =
      let intValues =  map (\x -> read x :: Int) (words row)
          seperatedValues = partition (\x -> even (snd x)) (zip intValues [0..])
      in zipWith (\x y -> [fst x, fst y]) (fst seperatedValues) (snd seperatedValues)
    
    valueIsInAllRows :: Int -> [[[Int]]] -> Bool
    valueIsInAllRows value rows =
      all (\r -> any (== value) (map (head) r)) rows
      
    removeRelativePrimes :: [Int] -> [[[Int]]] -> [[[ Int ]]]
    removeRelativePrimes truePrimes rows =
      let isTruePrime = (\x -> (head x) `elem` truePrimes)
      in map (\row -> filter isTruePrime row) rows
    
    main = do
      _ <- getLine
      matrix <- getContents
      let rawNumbers = lines matrix
          dataMatrix = map makeRowTuples rawNumbers
          firstValues = map (head) (head dataMatrix)
          truePrimes = filter (\x -> valueIsInAllRows x dataMatrix) firstValues
          matrixWithTruePrimes = removeRelativePrimes truePrimes dataMatrix
          sortedValues = sort (concat matrixWithTruePrimes)
          groupedValues = groupBy (\x y -> (head x) == head y) sortedValues
          answer = concat $ map head groupedValues
      putStrLn (unwords $ map show answer)