-
-
Notifications
You must be signed in to change notification settings - Fork 663
Transpose #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Transpose #364
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Transpose | ||
|
|
||
| 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 <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: | ||
|
|
||
| ```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). | ||
|
|
||
| ## Setup | ||
|
|
||
| Go through the setup instructions for ECMAScript to | ||
| install the necessary dependencies: | ||
|
|
||
| http://exercism.io/languages/ecmascript | ||
|
|
||
| ## Requirements | ||
|
|
||
| Install assignment dependencies: | ||
|
|
||
| ```bash | ||
| $ npm install | ||
| ``` | ||
|
|
||
| ## Making the Test Suite Pass | ||
|
|
||
| Execute the tests with: | ||
|
|
||
| ```bash | ||
| $ npm test | ||
| ``` | ||
|
|
||
| In the test suite, all tests but the first have been skipped. | ||
|
|
||
| Once you get a test passing, you can enable the next one by | ||
| changing `xtest` to `test`. | ||
|
|
||
| ## Source | ||
|
|
||
| Reddit r/dailyprogrammer challenge #270 [Easy]. [https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text](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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export default function transpose(text) { | ||
| const result = []; | ||
| text.forEach((line, lineNo) => { | ||
| [...line].forEach((value, key) => { | ||
| if (typeof result[key] === 'undefined') { | ||
| result[key] = ' '.repeat(lineNo); | ||
| } | ||
| result[key] += value; | ||
| }); | ||
| }); | ||
| return result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| { | ||
| "name": "xecmascript", | ||
| "version": "0.0.0", | ||
| "description": "Exercism exercises in ECMAScript 6.", | ||
| "author": "Katrina Owen", | ||
| "private": true, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/exercism/xecmascript" | ||
| }, | ||
| "devDependencies": { | ||
| "babel-jest": "^20.0.3", | ||
| "babel-plugin-transform-builtin-extend": "^1.1.2", | ||
| "babel-preset-env": "^1.4.0", | ||
| "eslint": "^3.19.0", | ||
| "eslint-config-airbnb": "^15.0.1", | ||
| "eslint-plugin-import": "^2.2.0", | ||
| "eslint-plugin-jsx-a11y": "^5.0.1", | ||
| "eslint-plugin-react": "^7.0.1", | ||
| "jest": "^20.0.4" | ||
| }, | ||
| "jest": { | ||
| "modulePathIgnorePatterns": [ | ||
| "package.json" | ||
| ] | ||
| }, | ||
| "babel": { | ||
| "presets": [ | ||
| "env" | ||
| ], | ||
| "plugins": [ | ||
| [ | ||
| "babel-plugin-transform-builtin-extend", | ||
| { | ||
| "globals": [ | ||
| "Error" | ||
| ] | ||
| } | ||
| ], | ||
| [ | ||
| "transform-regenerator" | ||
| ] | ||
| ] | ||
| }, | ||
| "scripts": { | ||
| "test": "jest --no-cache ./*", | ||
| "watch": "jest --no-cache --watch ./*", | ||
| "lint": "eslint .", | ||
| "lint-test": "eslint . && jest --no-cache ./* " | ||
| }, | ||
| "eslintConfig": { | ||
| "parserOptions": { | ||
| "ecmaVersion": 6, | ||
| "sourceType": "module" | ||
| }, | ||
| "env": { | ||
| "es6": true, | ||
| "node": true, | ||
| "jest": true | ||
| }, | ||
| "extends": "airbnb", | ||
| "rules": { | ||
| "import/no-unresolved": "off", | ||
| "import/extensions": "off" | ||
| } | ||
| }, | ||
| "licenses": [ | ||
| "MIT" | ||
| ], | ||
| "dependencies": {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import transpose from './transpose'; | ||
|
|
||
| describe('Transpose', () => { | ||
| test('test empty string', () => { | ||
| expect(transpose([])).toEqual([]); | ||
| }); | ||
|
|
||
| xtest('test two characters in a row', () => { | ||
| const input = ['A1']; | ||
| const expected = ['A', '1']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test two characters in a column', () => { | ||
| const input = ['A', '1']; | ||
| const expected = ['A1']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test simple', () => { | ||
| const input = ['ABC', '123']; | ||
| const expected = ['A1', 'B2', 'C3']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test single line', () => { | ||
| const input = ['Single line.']; | ||
| const expected = ['S', 'i', 'n', 'g', 'l', 'e', ' ', 'l', 'i', 'n', 'e', '.']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test first line longer than second line', () => { | ||
| const input = ['The fourth line.', 'The fifth line.']; | ||
| const expected = ['TT', 'hh', 'ee', ' ', 'ff', 'oi', 'uf', 'rt', 'th', 'h ', ' l', 'li', 'in', 'ne', 'e.', '.']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test second line longer than first line', () => { | ||
| const input = ['The first line.', 'The second line.']; | ||
| const expected = ['TT', 'hh', 'ee', ' ', 'fs', 'ie', 'rc', 'so', 'tn', ' d', 'l ', 'il', 'ni', 'en', '.e', ' .']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test square', () => { | ||
| const input = ['HEART', 'EMBER', 'ABUSE', 'RESIN', 'TREND']; | ||
| const expected = ['HEART', 'EMBER', 'ABUSE', 'RESIN', 'TREND']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test rectangle', () => { | ||
| const input = ['FRACTURE', 'OUTLINED', 'BLOOMING', 'SEPTETTE']; | ||
| const expected = ['FOBS', 'RULE', 'ATOP', 'CLOT', 'TIME', 'UNIT', 'RENT', 'EDGE']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test triangle', () => { | ||
| const input = ['T', 'EE', 'AAA', 'SSSS', 'EEEEE', 'RRRRRR']; | ||
| const expected = ['TEASER', ' EASER', ' ASER', ' SER', ' ER', ' R']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| xtest('test many lines', () => { | ||
| const input = ['Chor. Two households, both alike in dignity,', 'In fair Verona, where we lay our scene,', 'From ancient grudge break to new mutiny,', 'Where civil blood makes civil hands unclean.', 'From forth the fatal loins of these two foes', 'A pair of star-cross\'d lovers take their life;', 'Whose misadventur\'d piteous overthrows', 'Doth with their death bury their parents\' strife.', 'The fearful passage of their death-mark\'d love,', 'And the continuance of their parents\' rage,', 'Which, but their children\'s end, naught could remove,', 'Is now the two hours\' traffic of our stage;', 'The which if you with patient ears attend,', 'What here shall miss, our toil shall strive to mend.']; | ||
| const expected = ['CIFWFAWDTAWITW', 'hnrhr hohnhshh', 'o oeopotedi ea', 'rfmrmash cn t', '.a e ie fthow ', ' ia fr weh,whh', 'Trnco miae ie', 'w ciroitr btcr', 'oVivtfshfcuhhe', ' eeih a uote ', 'hrnl sdtln is', 'oot ttvh tttfh', 'un bhaeepihw a', 'saglernianeoyl', 'e,ro -trsui ol', 'h uofcu sarhu ', 'owddarrdan o m', 'lhg to\'egccuwi', 'deemasdaeehris', 'sr als t ists', ',ebk \'phool\'h,', ' reldi ffd ', 'bweso tb rtpo', 'oea ileutterau', 't kcnoorhhnatr', 'hl isvuyee\'fi ', ' atv es iisfet', 'ayoior trr ino', 'l lfsoh ecti', 'ion vedpn l', 'kuehtteieadoe ', 'erwaharrar,fas', ' nekt te rh', 'ismdsehphnnosa', 'ncuse ra-tau l', ' et tormsural', 'dniuthwea\'g t ', 'iennwesnr hsts', 'g,ycoitkrttet', 'n,l rs\'a anr', 'ief \'dgcgdi', 'taol eoe,v', 'yneisl,u;e', ',.sftol ', ' ervdt', ' ;ie o', ' f,r ', ' eem', ' .me', ' on', ' vd', ' e.', ' ,']; | ||
| expect(transpose(input)).toEqual(expected); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all looks good, but it did make me wonder if it's possible to do this entirely functionally-- IE, without having an initial
resultarray that gets mutated. Not critical, but just thinking out loud...