Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

# jyeshe's solution

## to Transpose in the Erlang Track

Published at Mar 10 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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).

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## 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's possible to submit an incomplete solution so you can see how others have completed the exercise.

### transpose_tests.erl

``````%% Based on canonical data version 1.1.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/transpose/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(transpose_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_empty_string_test'() ->
Input=[

],
Expected=[

],
?assertEqual(Expected, transpose:transpose(Input)).

'2_two_characters_in_a_row_test'() ->
Input=[
"A1"
],
Expected=[
"A",
"1"
],
?assertEqual(Expected, transpose:transpose(Input)).

'3_two_characters_in_a_column_test'() ->
Input=[
"A",
"1"
],
Expected=[
"A1"
],
?assertEqual(Expected, transpose:transpose(Input)).

'4_simple_test'() ->
Input=[
"ABC",
"123"
],
Expected=[
"A1",
"B2",
"C3"
],
?assertEqual(Expected, transpose:transpose(Input)).

'5_single_line_test'() ->
Input=[
"Single line."
],
Expected=[
"S",
"i",
"n",
"g",
"l",
"e",
" ",
"l",
"i",
"n",
"e",
"."
],
?assertEqual(Expected, transpose:transpose(Input)).

'6_first_line_longer_than_second_line_test'() ->
Input=[
"The fourth line.",
"The fifth line."
],
Expected=[
"TT",
"hh",
"ee",
"  ",
"ff",
"oi",
"uf",
"rt",
"th",
"h ",
" l",
"li",
"in",
"ne",
"e.",
"."
],
?assertEqual(Expected, transpose:transpose(Input)).

'7_second_line_longer_than_first_line_test'() ->
Input=[
"The first line.",
"The second line."
],
Expected=[
"TT",
"hh",
"ee",
"  ",
"fs",
"ie",
"rc",
"so",
"tn",
" d",
"l ",
"il",
"ni",
"en",
".e",
" ."
],
?assertEqual(Expected, transpose:transpose(Input)).

'8_mixed_line_length_test'() ->
Input=[
"The longest line.",
"A long line.",
"A longer line.",
"A line."
],
Expected=[
"TAAA",
"h   ",
"elll",
" ooi",
"lnnn",
"ogge",
"n e.",
"glr",
"ei ",
"snl",
"tei",
" .n",
"l e",
"i .",
"n",
"e",
"."
],
?assertEqual(Expected, transpose:transpose(Input)).

'9_square_test'() ->
Input=[
"HEART",
"EMBER",
"ABUSE",
"RESIN",
"TREND"
],
Expected=[
"HEART",
"EMBER",
"ABUSE",
"RESIN",
"TREND"
],
?assertEqual(Expected, transpose:transpose(Input)).

'10_rectangle_test'() ->
Input=[
"FRACTURE",
"OUTLINED",
"BLOOMING",
"SEPTETTE"
],
Expected=[
"FOBS",
"RULE",
"ATOP",
"CLOT",
"TIME",
"UNIT",
"RENT",
"EDGE"
],
?assertEqual(Expected, transpose:transpose(Input)).

'11_triangle_test'() ->
Input=[
"T",
"EE",
"AAA",
"SSSS",
"EEEEE",
"RRRRRR"
],
Expected=[
"TEASER",
" EASER",
"  ASER",
"   SER",
"    ER",
"     R"
],
?assertEqual(Expected, transpose:transpose(Input)).``````
``````-module(transpose).

-export([transpose/1]).
% -export([map_lines_to_columns/2, transposed_line/2]).

transpose([]) -> [];

transpose(Lines) ->
% transposed columns have the length of biggest line
MaxLineLen =
lists:foldl(
fun (Line, MaxLen) ->
if
length(Line) > MaxLen ->
length(Line);
true ->
MaxLen
end
end,
0,
Lines),
% transforms each line into a column (an array)
Columns = map_lines_to_columns(Lines, MaxLineLen),
% concats the J element of every columns (e.g "ab" is the result for J=0, col1=["a","1"] and col2=["b","2"] )
[ transposed_line(Columns, J) || J <- lists:seq(0, MaxLineLen-1) ].

% Returns a transposed line
transposed_line(Columns, J) ->
% searches the last index to apply padding
{_, LastChrIndex} = lists:foldl(
fun (Column, {Index, MaxIndex}) ->
ChrStr = array:get(J, Column),
if
ChrStr /= "" ->
{Index+1, Index};
true ->
{Index+1, MaxIndex}
end
end,
{0, 0}, Columns),
% concats every J element applying padding when there is a char to the right
{_, TLine} = lists:foldl(
fun (Column, {Index, LineStr}) ->
ChrStr = array:get(J, Column),
if
% empty and there is a char to the right
ChrStr == "" andalso Index < LastChrIndex ->
% padds with whitespace
{Index+1, string:concat(LineStr, " ")};
true ->
{Index+1, string:concat(LineStr, ChrStr)}
end
end,
{0, ""}, Columns),
TLine.

% Returns list of columns: each column is an array (e.g line "a1" becomes array with "a" and "1")
map_lines_to_columns(Lines, MaxLineLen) ->
lists:foldl(
fun (Line, ColumnsList) ->
ColumnArrayEmpty = array:new([{size, MaxLineLen}, {default, ""}]),
% transforms line "a1" into array with "a" and "1" elements
{_, ColumnArray} =
lists:foldl(
fun (Chr, {IndexL, Column}) ->
{IndexL+1, array:set(IndexL, [Chr], Column)}
end,
{0, ColumnArrayEmpty},
Line),
ColumnsList ++ [ColumnArray]
end,
[],
Lines).``````