DEV Community

Discussion on: Daily Challenge #72 - Matrix Shift

Collapse
 
aminnairi profile image
Amin

Elm

module MatrixShift exposing (matrixShiftRight)


split : Int -> List anything -> List (List anything)
split count list =
    case list of
        [] ->
            []

        _ ->
            let
                toAppend : List anything
                toAppend =
                    List.take count list

                newList : List anything
                newList =
                    List.drop count list

            in
                toAppend :: (split count newList)


headLength : List (List anything) -> Int
headLength list =
    list
        |> List.head
        |> Maybe.withDefault []
        |> List.length


matrixShiftRight : List (List Char) -> List (List Char)
matrixShiftRight matrix =
    let
        concatenated : List Char
        concatenated =
            List.concat matrix

        reversed : List Char
        reversed =
            List.reverse concatenated

        last : Char
        last =
            List.head reversed
                |> Maybe.withDefault ' '
    in
    reversed
        |> List.drop 1
        |> List.reverse
        |> (++) [ last ]
        |> split (headLength matrix)

Tests

module MatrixShiftTest exposing (suite)

import Expect exposing (equal)
import MatrixShift exposing (matrixShiftRight)
import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "Matrix Shift"
        [ test "It should return the array shifted #1" <|
            \_ ->
                let
                    input =
                        [ [ 'a', 'b', 'c', 'd' ]
                        , [ '1', '2', '3', '4' ]
                        , [ 'c', 'o', 'd', 'e' ]
                        , [ 'b', 'l', 'a', 'h' ]
                        ]

                    output : List (List Char)
                    output =
                        [ [ 'h', 'a', 'b', 'c' ]
                        , [ 'd', '1', '2', '3' ]
                        , [ '4', 'c', 'o', 'd' ]
                        , [ 'e', 'b', 'l', 'a' ]
                        ]
                in
                equal output <| matrixShiftRight input
        , test "It should return the array shifted #2" <|
            \_ ->
                let
                    input =
                        [ [ 'h', 'i' ]
                        , [ 'o', 'k' ]
                        ]

                    output : List (List Char)
                    output =
                        [ [ 'k', 'h' ]
                        , [ 'i', 'o' ]
                        ]
                in
                equal output <| matrixShiftRight input
        , test "It should return the array shifted #3" <|
            \_ ->
                let
                    input : List (List Char)
                    input =
                        [ [ 'd', 'u', 'd', 'e' ]
                        , [ 'i', 'm', 'c', 'o' ]
                        , [ 'd', 'i', 'n', 'g' ]
                        ]

                    output : List (List Char)
                    output =
                        [ [ 'g', 'd', 'u', 'd' ]
                        , [ 'e', 'i', 'm', 'c' ]
                        , [ 'o', 'd', 'i', 'n' ]
                        ]
                in
                equal output <| matrixShiftRight input
        ]