diff --git a/config.json b/config.json index 419d26e..3b1e32a 100644 --- a/config.json +++ b/config.json @@ -800,6 +800,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "transpose", + "name": "Transpose", + "uuid": "c28e01dc-7d5c-4390-bef1-7831e6111b76", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "wordy", "name": "Wordy", diff --git a/exercises/practice/transpose/.docs/instructions.md b/exercises/practice/transpose/.docs/instructions.md new file mode 100644 index 0000000..6033af7 --- /dev/null +++ b/exercises/practice/transpose/.docs/instructions.md @@ -0,0 +1,61 @@ +# Instructions + +Given an input text output it transposed. + +Roughly explained, the transpose of a matrix: + +```text +ABC +DEF +``` + +is given by: + +```text +AD +BE +CF +``` + +Rows become columns and columns become rows. +See [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: + +```text +ABC +DE +``` + +results in: + +```text +AD +BE +C +``` + +And transposing: + +```text +AB +DEF +``` + +results in: + +```text +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). + +[transpose]: https://en.wikipedia.org/wiki/Transpose diff --git a/exercises/practice/transpose/.meta/config.json b/exercises/practice/transpose/.meta/config.json new file mode 100644 index 0000000..32f18a3 --- /dev/null +++ b/exercises/practice/transpose/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "transpose.coffee" + ], + "test": [ + "transpose.spec.coffee" + ], + "example": [ + ".meta/example.coffee" + ] + }, + "blurb": "Take input text and output it transposed.", + "source": "Reddit r/dailyprogrammer challenge #270 [Easy].", + "source_url": "https://web.archive.org/web/20230630051421/https://old.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text/" +} diff --git a/exercises/practice/transpose/.meta/example.coffee b/exercises/practice/transpose/.meta/example.coffee new file mode 100644 index 0000000..9dc441d --- /dev/null +++ b/exercises/practice/transpose/.meta/example.coffee @@ -0,0 +1,14 @@ +class Transpose + @transpose: (text) -> + return '' unless text + + lines = text.split '\n' + maxLen = Math.max (line.length for line in lines)... + padded = lines.map (line) -> line + '\0'.repeat maxLen - line.length + + result = for i in [0...maxLen] + (row[i] for row in padded).join('').replace(/\0+$/, '').replace(/\0/g, ' ') + + result.join '\n' + +module.exports = Transpose diff --git a/exercises/practice/transpose/.meta/tests.toml b/exercises/practice/transpose/.meta/tests.toml new file mode 100644 index 0000000..32e366f --- /dev/null +++ b/exercises/practice/transpose/.meta/tests.toml @@ -0,0 +1,46 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[404b7262-c050-4df0-a2a2-0cb06cd6a821] +description = "empty string" + +[a89ce8a3-c940-4703-a688-3ea39412fbcb] +description = "two characters in a row" + +[855bb6ae-4180-457c-abd0-ce489803ce98] +description = "two characters in a column" + +[5ceda1c0-f940-441c-a244-0ced197769c8] +description = "simple" + +[a54675dd-ae7d-4a58-a9c4-0c20e99a7c1f] +description = "single line" + +[0dc2ec0b-549d-4047-aeeb-8029fec8d5c5] +description = "first line longer than second line" + +[984e2ec3-b3d3-4b53-8bd6-96f5ef404102] +description = "second line longer than first line" + +[eccd3784-45f0-4a3f-865a-360cb323d314] +description = "mixed line length" + +[85b96b3f-d00c-4f80-8ca2-c8a5c9216c2d] +description = "square" + +[b9257625-7a53-4748-8863-e08e9d27071d] +description = "rectangle" + +[b80badc9-057e-4543-bd07-ce1296a1ea2c] +description = "triangle" + +[76acfd50-5596-4d05-89f1-5116328a7dd9] +description = "jagged triangle" diff --git a/exercises/practice/transpose/transpose.coffee b/exercises/practice/transpose/transpose.coffee new file mode 100644 index 0000000..71249f2 --- /dev/null +++ b/exercises/practice/transpose/transpose.coffee @@ -0,0 +1,4 @@ +class Transpose + @transpose: (text) -> + +module.exports = Transpose diff --git a/exercises/practice/transpose/transpose.spec.coffee b/exercises/practice/transpose/transpose.spec.coffee new file mode 100644 index 0000000..0f88eb2 --- /dev/null +++ b/exercises/practice/transpose/transpose.spec.coffee @@ -0,0 +1,199 @@ +Transpose = require './transpose' + +describe 'Transpose', -> + it 'empty string', -> + lines = '' + expected = '' + expect(Transpose.transpose lines).toEqual expected + + xit 'two characters in a row', -> + lines = 'A1' + expected = "A\n1" + expect(Transpose.transpose lines).toEqual expected + + xit 'two characters in a column', -> + lines = "A\n1" + expected = 'A1' + expect(Transpose.transpose lines).toEqual expected + + xit 'simple', -> + lines = [ + 'ABC' + '123' + ].join '\n' + expected = [ + 'A1' + 'B2' + 'C3' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected + + xit 'single line', -> + lines = 'Single line.' + expected = [ + 'S' + 'i' + 'n' + 'g' + 'l' + 'e' + ' ' + 'l' + 'i' + 'n' + 'e' + '.' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected + + xit 'first line longer than second line', -> + lines = [ + 'The fourth line.' + 'The fifth line.' + ].join'\n' + expected = [ + 'TT' + 'hh' + 'ee' + ' ' + 'ff' + 'oi' + 'uf' + 'rt' + 'th' + 'h ' + ' l' + 'li' + 'in' + 'ne' + 'e.' + '.' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected + + xit 'second line longer than first line', -> + lines = [ + 'The first line.' + 'The second line.' + ].join('\n') + expected = [ + 'TT' + 'hh' + 'ee' + ' ' + 'fs' + 'ie' + 'rc' + 'so' + 'tn' + ' d' + 'l ' + 'il' + 'ni' + 'en' + '.e' + ' .' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected + + xit 'mixed line length', -> + lines = [ + 'The longest line.' + 'A long line.' + 'A longer line.' + 'A line.' + ].join '\n' + expected = [ + 'TAAA' + 'h ' + 'elll' + ' ooi' + 'lnnn' + 'ogge' + 'n e.' + 'glr' + 'ei ' + 'snl' + 'tei' + ' .n' + 'l e' + 'i .' + 'n' + 'e' + '.' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected + + xit 'square', -> + lines = [ + 'HEART' + 'EMBER' + 'ABUSE' + 'RESIN' + 'TREND' + ].join '\n' + expected = [ + 'HEART' + 'EMBER' + 'ABUSE' + 'RESIN' + 'TREND' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected + + xit 'rectangle', -> + lines = [ + 'FRACTURE' + 'OUTLINED' + 'BLOOMING' + 'SEPTETTE' + ].join '\n' + expected = [ + 'FOBS' + 'RULE' + 'ATOP' + 'CLOT' + 'TIME' + 'UNIT' + 'RENT' + 'EDGE' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected + + xit 'triangle', -> + lines = [ + 'T' + 'EE' + 'AAA' + 'SSSS' + 'EEEEE' + 'RRRRRR' + ].join '\n' + expected = [ + 'TEASER' + ' EASER' + ' ASER' + ' SER' + ' ER' + ' R' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected + + xit 'jagged triangle', -> + lines = [ + '11' + '2' + '3333' + '444' + '555555' + '66666' + ].join '\n' + expected = [ + '123456' + '1 3456' + ' 3456' + ' 3 56' + ' 56' + ' 5' + ].join '\n' + expect(Transpose.transpose lines).toEqual expected