-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
The three rules of Ruby Q.:
-
Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have elapsed from the time this message was
sent. -
Support Ruby Q. by submitting ideas and responses
as often as you can!
Visit: http://rubyquiz.strd6.com/suggestions -
Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby T. follow the discussion. Please reply to
the original quiz message, if you can.
RSS Feed: http://rubyquiz.strd6.com/quizzes.rss
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Matrix Rotator (#209)
ЗдравÑтвуйте Rubyists,
This week’s quiz is write ruby method that will rotate a matrix 90
degrees counter-clockwise (or π/2 radians).
Ex:
0 0 0 0
0 X 0 0
X X X 0
0 0 0 0
0 0 0 0
0 0 X 0
0 X X 0
0 0 X 0
I have attached the following test suite for your convenience:
require 'test/unit'
require 'solution'
class RotationTest < Test::Unit::TestCase
def test_square_rotation
square = [
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 0, 0]
]
square_rotated = [
[0, 0, 0, 0],
[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 0]
]
assert_equal Matrix.rotate(square), square_rotated
end
def test_rectangular_rotation
rectangle = [
[0, 1, 0],
[1, 1, 1]
]
rectangle_rotated = [
[0, 1],
[1, 1],
[0, 1]
]
assert_equal Matrix.rotate(rectangle), rectangle_rotated
end
end
Have Fun!
–
-Daniel
http://rubyquiz.strd6.com
P. S. This may come in handy on a future quiz.