Sort by

recency

|

22 Discussions

|

  • + 0 comments

    The string rotation problem is a great way to practice coding with strings by shifting characters left one by one. For example, “abc” rotates to “bca” and “cab.” Also, if you’re interested in sävytetyt ikkunakalvot, they are tinted window films used for privacy and protection in homes and cars.

  • + 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