Sort by

recency

|

57 Discussions

|

  • + 0 comments

    Scala

    import scala.io.StdIn.readLine
    
    object Solution {
    
        def main(args: Array[String]) {
            def swap(str: List[Char]): List[Char] = {
                def swap(res: List[Char], str: List[Char]): List[Char] = {
                    str match {
                        case a :: b :: tail => swap(res :+ b :+ a, tail)
                        case Nil  => res
                    }
                }
                swap(Nil, str)
            }
    
            val n = readLine().toInt
    
            for (i <- 1 to n) {
                val str = readLine()
    
                println(swap(str.toList).mkString)
            }
        }
    }
    
  • + 0 comments

    f# solution

    open System
    
    let swap str =
        str
        |> Seq.chunkBySize 2
        |> Seq.map Seq.rev
        |> Seq.concat
        |> Seq.map string
        |> String.concat ""
    
    [<EntryPoint>]
    let main args =
        let n = Console.ReadLine() |> int
    
        [ 1..n ]
        |> Seq.map (fun _ -> Console.ReadLine())
        |> Seq.map swap
        |> Seq.iter Console.WriteLine
    
        0
    
  • + 0 comments

    A simple haskell solution:

    import Control.Monad
    
    merge (x:y:rest) = y:x: merge rest
    merge rest = rest
    
    main = do
        count <- getLine
        forM [0..(read count :: Int)-1] $
            (\_ -> do -- Here's where the magic happens N times:
                line <- getLine
                putStrLn $ merge line
            )
    
  • + 0 comments
    main1 :: IO ()
    main1 = traverse_ TIO.putStrLn . ((\inputString -> mconcat $ (\text -> T.drop 1 text <> T.take 1 text) <$> T.chunksOf 2 (T.pack inputString))<$>) =<< ((`replicateM` getLine) =<< read @Int<$> getLine)
    
  • + 0 comments
    indexed :: [a] -> [(Int,a)]
    indexed xs = go 0 xs
        where go i (a:as) = (i,a) : go (i+1) as
              go _ _ = []
    
    solve :: (Ord a) => [(Int,a)] -> [a]
    solve x =
        let evenList = foldr (\(i,v) acc -> if (even i) then v:acc else acc) [] x
            oddList = foldr (\(i,v) acc -> if (odd i) then v:acc else acc ) [] x
        in concat [ [a,b] | (a,b) <- (flip zip evenList oddList) ]
        
    main = interact $ unlines . map solve . map indexed . tail . words