Sort by

recency

|

21 Discussions

|

  • + 0 comments

    Here's my haskell solution.

    allRotations s = build (length s) s
      where
        build 0 s = []
        build n s = let s' = rotl s in s' : build (n - 1) s'
        rotl s = tail s ++ [head s]
    
    main = interact (unlines . map (unwords . allRotations) . tail . lines)
    
  • + 0 comments
    import Data.List(intercalate)
    
    rotatedStrings :: String -> String
    rotatedStrings a = intercalate " " $ stringRotations a
    
    stringRotations :: String -> [String]
    stringRotations s = [rotate i s | i <- [1.. length s] ] where
        rotate i s = drop i s ++ take i s
    
    main :: IO ()
    main = do 
        _ <- getLine
        interact (unlines . map rotatedStrings . lines)
    
  • + 0 comments

    HASKELL

    rotate :: [Char] -> String 
    rotate [] = []
    rotate (x:xs) = xs ++ [x]
    
    rotateAll :: [Char] -> String
    rotateAll s = unwords $ auxRotate (rotate s) (length s)
        where  
            auxRotate rs 0 = []
            auxRotate rs n = rs : auxRotate (rotate rs) (n-1)
    
    main :: IO ()
    main = interact $ unlines . map rotateAll .  drop 1 . lines
    
  • + 0 comments

    Elixir solution:

    defmodule Solution do
        def rotate(_, c) when c == 0 do
            []
        end
    
        def rotate(s, c) do
            [start | rest] = String.codepoints(s)
            r = List.to_string(rest) <> start
            [r] ++ rotate(r, c - 1)
        end
    
        def rotate_all(_, elements) do
            Enum.map(elements, fn x -> 
                Solution.rotate(x, String.length(x)) 
                |> Enum.join(" ") 
            end)
        end 
    end
    
    input = IO.read(:stdio, :all)
      |> String.split("\n")
      
    [n | elements] = input
    Solution.rotate_all(n, elements)
    |> Enum.join("\n")
    |> IO.puts
    
  • + 0 comments

    Scala, using ScanLeft:

    object Solution {
        import scala.io.StdIn.{readInt, readLine}
    
        def main(args: Array[String]): Unit =
            (1 to readInt).map(_ => rotateAll(readLine)).foreach(println)
    
        def rotateAll(s: String): String = 
            (1 to s.length).scanLeft(s){ case (s, _) => rotate1(s) }.tail.mkString(" ")
    
        def rotate1(s: String): String = 
            s.tail.foldRight(List(s.head))(_ :: _).mkString
    }