ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

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:

```
0 1 2
|---------
0 | 9 8 7
1 | 5 3 2
2 | 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

To run the tests, run the command `busted`

from within the exercise directory.

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Warmup to the `saddle-points`

warmup. http://jumpstartlab.com

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

```
local Matrix = require('matrix')
describe('matrix', function()
it('should allow rows to be extracted', function()
local matrix = Matrix('1 2 3\n4 5 6\n7 8 9')
assert.same({ 1, 2, 3 }, matrix.row(1))
assert.same({ 4, 5, 6 }, matrix.row(2))
assert.same({ 7, 8, 9 }, matrix.row(3))
end)
it('should allow columns to be extracted', function()
local matrix = Matrix('1 2 3\n4 5 6\n7 8 9')
assert.same({ 1, 4, 7 }, matrix.column(1))
assert.same({ 2, 5, 8 }, matrix.column(2))
assert.same({ 3, 6, 9 }, matrix.column(3))
end)
it('should support values of varying sizes', function()
local matrix = Matrix('1 2 3\n4 12345 6\n7 8 9')
assert.same({ 4, 12345, 6 }, matrix.row(2))
assert.same({ 2, 12345, 8 }, matrix.column(2))
end)
it('should support matrices of varying sizes', function()
local matrix = Matrix('1 2\n3 4\n5 6')
assert.same({ 3, 4 }, matrix.row(2))
assert.same({ 2, 4, 6 }, matrix.column(2))
end)
end)
```

```
local function Matrix(input)
local row_table = {}
for line in input:gmatch('[%d ]+') do
table.insert(row_table, line)
end
local function row(index)
local result = {}
local row_string = row_table[index]
for w in row_table[index]:gmatch('%d+') do
table.insert(result, tonumber(w))
end
return result
end
local function column(index)
local rows
local result = {}
for i = 1, #row_table do
rows = row(i)
table.insert(result, rows[index])
end
return result
end
return { row = row, column = column }
end
return Matrix
```

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?

Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

Hmm, looks like an unused variable on line 10. Looks good otherwise.

looks straight forward - good job