🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of rootkowiak

rootkowiak's solution

to Transpose in the Erlang Track

Published at Jan 14 2021 · 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).

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

%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/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=[

    ],
    {"empty string",
     ?_assertEqual(Expected, transpose:transpose(Input))}.

'2_two_characters_in_a_row_test_'() ->
    Input=[
        "A1"
    ],
    Expected=[
        "A",
        "1"
    ],
    {"two characters in a row",
     ?_assertEqual(Expected, transpose:transpose(Input))}.

'3_two_characters_in_a_column_test_'() ->
    Input=[
        "A",
        "1"
    ],
    Expected=[
        "A1"
    ],
    {"two characters in a column",
     ?_assertEqual(Expected, transpose:transpose(Input))}.

'4_simple_test_'() ->
    Input=[
        "ABC",
        "123"
    ],
    Expected=[
        "A1",
        "B2",
        "C3"
    ],
    {"simple",
     ?_assertEqual(Expected, transpose:transpose(Input))}.

'5_single_line_test_'() ->
    Input=[
        "Single line."
    ],
    Expected=[
        "S",
        "i",
        "n",
        "g",
        "l",
        "e",
        " ",
        "l",
        "i",
        "n",
        "e",
        "."
    ],
    {"single line",
     ?_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.",
        "."
    ],
    {"first line longer than second line",
     ?_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",
        " ."
    ],
    {"second line longer than first line",
     ?_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",
        "."
    ],
    {"mixed line length",
     ?_assertEqual(Expected, transpose:transpose(Input))}.

'9_square_test_'() ->
    Input=[
        "HEART",
        "EMBER",
        "ABUSE",
        "RESIN",
        "TREND"
    ],
    Expected=[
        "HEART",
        "EMBER",
        "ABUSE",
        "RESIN",
        "TREND"
    ],
    {"square",
     ?_assertEqual(Expected, transpose:transpose(Input))}.

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

'11_triangle_test_'() ->
    Input=[
        "T",
        "EE",
        "AAA",
        "SSSS",
        "EEEEE",
        "RRRRRR"
    ],
    Expected=[
        "TEASER",
        " EASER",
        "  ASER",
        "   SER",
        "    ER",
        "     R"
    ],
    {"triangle",
     ?_assertEqual(Expected, transpose:transpose(Input))}.
-module(transpose).

-export([transpose/1]).

transpose([]) -> [];
transpose(Array) -> 
    trans(pad(Array)).

trans([[]|_]) -> [];
trans(M) -> 
    Heads = lists:map(fun([]) -> nil; (E) -> erlang:hd(E) end, M),
    H = lists:filter(fun(E) -> E /= nil end, Heads),
    Tails = lists:map(fun([]) -> nil; (E) -> erlang:tl(E) end, M),
    T = lists:filter(fun(E) -> E /= nil end, Tails),
    [H|trans(T)].

pad(Array) -> pad(lists:reverse(Array), []).

pad([H|[]], Acc) -> [H|Acc];
pad([H1|[H2|T]], Acc) -> 
    Upd = lists:flatten(string:pad(H2, length(H1))),
    pad([Upd|T], [H1|Acc]).

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?