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

lujanfernaud's solution

to Matrix in the Ruby Track

Published at Mar 16 2019 · 2 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby matrix_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride matrix_test.rb
``````

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.rb

``````require 'minitest/autorun'
require_relative 'matrix'

class MatrixTest < Minitest::Test
def test_extract_a_row
matrix = Matrix.new("1 2\n10 20")
assert_equal [1, 2], matrix.rows[0]
end

def test_extract_same_row_again
skip
matrix = Matrix.new("9 7\n8 6")
assert_equal [9, 7], matrix.rows[0]
end

def test_extract_other_row
skip
matrix = Matrix.new("9 8 7\n19 18 17")
assert_equal [19, 18, 17], matrix.rows[1]
end

def test_extract_other_row_again
skip
matrix = Matrix.new("1 4 9\n16 25 36")
assert_equal [16, 25, 36], matrix.rows[1]
end

def test_extract_a_column
skip
matrix = Matrix.new("1 2 3\n4 5 6\n7 8 9\n 8 7 6")
assert_equal [1, 4, 7, 8], matrix.columns[0]
end

def test_extract_another_column
skip
matrix = Matrix.new("89 1903 3\n18 3 1\n9 4 800")
assert_equal [1903, 3, 4], matrix.columns[1]
end
end``````
``````class Matrix
def initialize(matrix)
@matrix = matrix
end

def rows
@rows ||= @matrix.each_line.map { |item| item.split.map(&:to_i) }
end

def columns
rows.transpose
end
end``````

Find this solution interesting? Ask the author a question to learn more.

Thanks to this solution I realised I've missed doing better, namely - to calculate @rows lazily, on first invocation (@rows ||= ...) Just one note -- you can optimise columns method the same way: @colums ||= row.transpose

Solution Author
commented about 2 years ago

Thanks for your comment.

I didn't memoize `#columns` thinking that it's not called a second time in this class. But actually, being a public method, it may make sense to also do it.

What can you learn from this solution?

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?