# paulfioravanti's solution

## to Transpose in the Elm Track

Published at Aug 11 2019 · 0 comments
Instructions
Test suite
Solution

Given an input text output it transposed.

Roughly explained, the transpose of a matrix:

``````ABC
DEF
``````

is given by:

``````AD
BE
CF
``````

Rows become columns and columns become rows. See https://en.wikipedia.org/wiki/Transpose.

If the input has rows of different lengths, this is to be solved as follows:

• Pad to the left with spaces.
• Don't pad to the right.

Therefore, transposing this matrix:

``````ABC
DE
``````

results in:

``````AD
BE
C
``````

And transposing:

``````AB
DEF
``````

results in:

``````AD
BE
F
``````

In general, all characters from the input should also be present in the transposed output. That means that if a column in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces in its right-most column(s).

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

Reddit r/dailyprogrammer challenge #270 [Easy]. https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text

## Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (tests)

import Expect
import Test exposing (Test, describe, skip, test)
import Transpose exposing (transpose)

tests : Test
tests =
describe "Hamming"
[ test "empty string" <|
\_ ->
Expect.equal [] (transpose [])
, skip <|
test "two characters in a row" <|
\_ ->
Expect.equal
[ "A"
, "1"
]
(transpose [ "A1" ])
, skip <|
test "two characters in a column" <|
\_ ->
Expect.equal
[ "A1" ]
(transpose
[ "A"
, "1"
]
)
, skip <|
test "simple" <|
\_ ->
Expect.equal
[ "A1"
, "B2"
, "C3"
]
(transpose
[ "ABC"
, "123"
]
)
, skip <|
test "single line" <|
\_ ->
Expect.equal
[ "S"
, "i"
, "n"
, "g"
, "l"
, "e"
, " "
, "l"
, "i"
, "n"
, "e"
, "."
]
(transpose [ "Single line." ])
, skip <|
test "first line longer than second line" <|
\_ ->
Expect.equal
[ "TT"
, "hh"
, "ee"
, "  "
, "ff"
, "oi"
, "uf"
, "rt"
, "th"
, "h "
, " l"
, "li"
, "in"
, "ne"
, "e."
, "."
]
(transpose
[ "The fourth line."
, "The fifth line."
]
)
, skip <|
test "second line longer than first line" <|
\_ ->
Expect.equal
[ "TT"
, "hh"
, "ee"
, "  "
, "fs"
, "ie"
, "rc"
, "so"
, "tn"
, " d"
, "l "
, "il"
, "ni"
, "en"
, ".e"
, " ."
]
(transpose
[ "The first line."
, "The second line."
]
)
, skip <|
test "mixed line length" <|
\_ ->
Expect.equal
[ "TAAA"
, "h   "
, "elll"
, " ooi"
, "lnnn"
, "ogge"
, "n e."
, "glr"
, "ei "
, "snl"
, "tei"
, " .n"
, "l e"
, "i ."
, "n"
, "e"
, "."
]
(transpose
[ "The longest line."
, "A long line."
, "A longer line."
, "A line."
]
)
, skip <|
test "square" <|
\_ ->
Expect.equal
[ "HEART"
, "EMBER"
, "ABUSE"
, "RESIN"
, "TREND"
]
(transpose
[ "HEART"
, "EMBER"
, "ABUSE"
, "RESIN"
, "TREND"
]
)
, skip <|
test "rectangle" <|
\_ ->
Expect.equal
[ "FOBS"
, "RULE"
, "ATOP"
, "CLOT"
, "TIME"
, "UNIT"
, "RENT"
, "EDGE"
]
(transpose
[ "FRACTURE"
, "OUTLINED"
, "BLOOMING"
, "SEPTETTE"
]
)
, skip <|
test "triangle" <|
\_ ->
Expect.equal
[ "TEASER"
, " EASER"
, "  ASER"
, "   SER"
, "    ER"
, "     R"
]
(transpose
[ "T"
, "EE"
, "AAA"
, "SSSS"
, "EEEEE"
, "RRRRRR"
]
)
]``````
``````module Transpose exposing (transpose)

transpose : List String -> List String
transpose lines =
case lines of
[] ->
[]

_ ->
let
maxWidth =
maxRowWidth lines
in
lines
|> transposeLines

-- PRIVATE

{-| Chosen arbitrarily...
-}
'\$'

maxRowWidth : List String -> Int
maxRowWidth lines =
lines
|> List.map String.length
|> List.maximum
|> Maybe.withDefault 0

generateRightPaddedLine : Int -> String -> List String
line
|> String.split ""

transposeLines : List (List String) -> List String
transposeLines lines =
lines
|> zip []
|> List.map (String.join "")

zip : List (List String) -> List (List String) -> List (List String)
zip zippedLines lines =
case lines of
[] ->
zippedLines
|> List.map List.reverse

case zippedLines of
[] ->
let
newZippedLines =
in
zip newZippedLines tail

_ ->
let
newZippedLines =
in
zip newZippedLines tail

revertPadding : List String -> List String
let
' '

else
char
in
lines
|> List.map String.reverse
|> List.map String.toList
|> List.map String.fromList
|> List.map String.reverse

dropWhile : (a -> Bool) -> List a -> List a
dropWhile predicate list =
case list of
[] ->
[]

dropWhile predicate tail

else
list``````