 # rootulp's solution

## to Matrix in the TypeScript Track

Published at May 17 2021 · 0 comments
Instructions
Test suite
Solution

Given a string representing a matrix of numbers, return the rows and columns of that matrix.

So given a string with embedded newlines like:

``````9 8 7
5 3 2
6 6 7
``````

representing this matrix:

``````    1  2  3
|---------
1 | 9  8  7
2 | 5  3  2
3 | 6  6  7
``````

your code should be able to spit out:

• A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows,
• A list of the columns, reading each column top-to-bottom while moving from left-to-right.

The rows for our example matrix:

• 9, 8, 7
• 5, 3, 2
• 6, 6, 7

And its columns:

• 9, 5, 6
• 8, 3, 6
• 7, 2, 7

## 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

Warmup to the `saddle-points` warmup. http://jumpstartlab.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### matrix.test.ts

``````import Matrix from './matrix'

describe('Matrix', () => {
it('extract row from one number matrix', () => {
const expected = 
expect(new Matrix('1').rows).toEqual(expected)
})

xit('can extract row', () => {
const expected = [3, 4]
expect(new Matrix('1 2\n3 4').rows).toEqual(expected)
})

xit('extract row where numbers have different widths', () => {
const expected = [10, 20]
expect(new Matrix('1 2\n10 20').rows).toEqual(expected)
})

xit('can extract row from non-square matrix', () => {
const expected = [7, 8, 9]
expect(new Matrix('1 2 3\n4 5 6\n7 8 9\n8 7 6').rows).toEqual(expected)
})

xit('extract column from one number matrix', () => {
const expected = 
expect(new Matrix('1').columns).toEqual(expected)
})

xit('can extract column', () => {
const expected = [3, 6, 9]
expect(new Matrix('1 2 3\n4 5 6\n7 8 9').columns).toEqual(expected)
})

xit('can extract column from non-square matrix', () => {
const expected = [3, 6, 9, 6]
expect(new Matrix('1 2 3\n4 5 6\n7 8 9\n8 7 6').columns).toEqual(
expected
)
})

xit('extract column where numbers have different widths', () => {
const expected = [1903, 3, 4]
expect(new Matrix('89 1903 3\n18 3 1\n9 4 800').columns).toEqual(
expected
)
})
})``````
``````class Matrix {

private matrix: number[][];
private transposed: number[][];
constructor(input: string) {
this.matrix = input.split('\n').map(row => row.split(' ')).map(row => row.map(val => Number.parseInt(val)))
this.transposed = this.transpose(this.clone(this.matrix))
}

public get rows(): number[][] {
return this.matrix;
}

public get columns(): number[][] {
return this.transposed;
}

private transpose(matrix: number[][]): number[][] {
return matrix.map((_, colIndex) => matrix.map(row => row[colIndex]));
}
private clone(matrix: number[][]): number[][] {
let clone: number[][] = [];
matrix.forEach(row => {
let clonedRow: number[] = [];
row.forEach(val => {
clonedRow.push(val)
})
clone.push(clonedRow);
});
return clone;
}
}

export default Matrix``````