 # theJian's solution

## to Transpose in the TypeScript Track

Published at May 20 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).

## Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

## Requirements

Install assignment dependencies:

``````\$ yarn install
``````

## Making the test suite pass

Execute the tests with:

``````\$ yarn test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xit` to `it`.

## 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.test.ts

``````import Transpose from './transpose'

describe('Transpose', () => {
it('empty string', () => {
const expected: string[] = []
expect(Transpose.transpose([])).toEqual(expected)
})

xit('two characters in a row', () => {
const expected = ['A', '1']
expect(Transpose.transpose(['A1'])).toEqual(expected)
})

xit('two characters in a column', () => {
const expected = ['A1']
expect(Transpose.transpose(['A', '1'])).toEqual(expected)
})

xit('simple', () => {
const expected = ['A1', 'B2', 'C3']
expect(Transpose.transpose(['ABC', '123'])).toEqual(expected)
})

xit('single line', () => {
const expected = ['S', 'i', 'n', 'g', 'l', 'e', ' ', 'l', 'i', 'n', 'e', '.']
expect(Transpose.transpose(['Single line.'])).toEqual(expected)
})

xit('first line longer than second line', () => {
const expected = ['TT', 'hh', 'ee', '  ', 'ff', 'oi', 'uf', 'rt', 'th', 'h ', ' l', 'li', 'in', 'ne', 'e.', '.']
expect(Transpose.transpose(['The fourth line.', 'The fifth line.'])).toEqual(expected)
})

xit('second line longer than first line', () => {
const expected = ['TT', 'hh', 'ee', '  ', 'fs', 'ie', 'rc', 'so', 'tn', ' d', 'l ', 'il', 'ni', 'en', '.e', ' .']
expect(Transpose.transpose(['The first line.', 'The second line.'])).toEqual(expected)
})

xit('square', () => {
const expected = ['HEART', 'EMBER', 'ABUSE', 'RESIN', 'TREND']
expect(Transpose.transpose(['HEART', 'EMBER', 'ABUSE', 'RESIN', 'TREND'])).toEqual(expected)
})

xit('rectangle', () => {
const expected = ['FOBS', 'RULE', 'ATOP', 'CLOT', 'TIME', 'UNIT', 'RENT', 'EDGE']
expect(Transpose.transpose(['FRACTURE', 'OUTLINED', 'BLOOMING', 'SEPTETTE'])).toEqual(expected)
})

xit('triangle', () => {
const expected = ['TEASER', ' EASER', '  ASER', '   SER', '    ER', '     R']
expect(Transpose.transpose(['T', 'EE', 'AAA', 'SSSS', 'EEEEE', 'RRRRRR'])).toEqual(expected)
})

xit('test many lines', () => {
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.transpose(['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.'])).toEqual(expected)
})
})``````
``````export default {
transpose(input: string[]) {
let output = []
let len = Math.max(0, ...input.map(i => i.length))
for (let i = 0; i < len; ++i) {
output.push(
input.reduce((prev, cur) =>
prev + (cur[i] || (/^\s*\$/.test(prev) ? ' ' : '')), '')
)
}

if (len - 1 >= 0) {
output[len - 1] = output[len - 1].replace(/\s+\$/, '')
}

return output;
}
}``````