Last active
December 21, 2015 07:08
-
-
Save nevill/6268603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
# -*- coding: utf-8 -*- | |
class MatrixCalc | |
def initialize(matrix) | |
@matrix = matrix | |
end | |
def input | |
@input ||= traverse | |
end | |
def sum(index) | |
input.take(index).reduce(:+) | |
end | |
def at(index) | |
input[index - 1] | |
end | |
def traverse | |
result = [] | |
x1, y1, x2, y2 = 0, 0, @matrix[0].size - 1, @matrix.size - 1 | |
matrix_iter = lambda do |direction| | |
w = x2 - x1 + 1 | |
h = y2 - y1 + 1 | |
if w <= 0 or h <= 0 | |
return | |
end | |
if direction == 0 | |
result += (x1..x2).map { |x| @matrix[y1][x] } | |
y1 += 1 | |
elsif direction == 1 | |
result += (y1..y2).map { |y| @matrix[y][x2] } | |
x2 -= 1 | |
elsif direction == 2 | |
result += x2.downto(x1).map { |x| @matrix[y2][x] } | |
y2 -= 1 | |
elsif direction == 3 | |
result += y2.downto(y1).map { |y| @matrix[y][x1] } | |
x1 += 1 | |
end | |
matrix_iter.call (direction + 1) % 4 | |
end | |
matrix_iter.call 0 | |
result | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
# -*- coding: utf-8 -*- | |
require 'test/unit' | |
require_relative 'calculation' | |
class CalculationTest < Test::Unit::TestCase | |
def setup | |
input = [ [12, 32, 9], [19, 5, 11], [43, 7, 34] ] | |
@matrix = MatrixCalc.new(input) | |
end | |
def test_sum_three | |
assert_equal 53, @matrix.sum(3) | |
end | |
def test_sum_eight | |
assert_equal 167, @matrix.sum(8) | |
end | |
def test_position_three | |
assert_equal 9, @matrix.at(3) | |
end | |
def test_position_seven | |
assert_equal 43, @matrix.at(7) | |
end | |
# def test_visited | |
# assert_equal false, @matrix.visited?(3, 3) | |
# end | |
end | |
class TraverseTest < Test::Unit::TestCase | |
def test_with_2x2 | |
input = [ [12, 32], [7, 34] ] | |
@matrix = MatrixCalc.new(input) | |
assert_equal [12, 32, 34, 7], @matrix.traverse | |
end | |
def test_with_3x3 | |
input = [ [12, 17, 9], [19, 28, 11], [43, 7, 34] ] | |
@matrix = MatrixCalc.new(input) | |
assert_equal [12, 17, 9, 11, 34, 7, 43, 19, 28], @matrix.traverse | |
end | |
def test_with_4x4 | |
# 12 32 09 11 | |
# 08 54 76 23 | |
# 27 18 25 09 | |
# 11 23 78 63 | |
input = [ [12, 32, 9, 11], [8, 54, 76, 23], [27, 18, 25, 9], [11, 23, 78, 63] ] | |
@matrix = MatrixCalc.new(input) | |
assert_equal [12, 32, 9, 11, 23, 9, 63, 78, 23, 11, 27, 8, 54, 76, 25, 18], @matrix.traverse | |
end | |
def test_with_5x5 | |
# 12 32 09 11 34 | |
# 08 54 76 23 07 | |
# 27 18 25 09 43 | |
# 11 23 78 63 19 | |
# 09 22 56 31 05 | |
input = [ [12, 32, 9, 11, 34], [8, 54, 76, 23, 7], [27, 18, 25, 9, 43], [11, 23, 78, 63, 19], [9, 22, 56, 31, 5] ] | |
@matrix = MatrixCalc.new(input) | |
assert_equal [12, 32, 9, 11, 34, 7, 43, 19, 5, 31, 56, 22, 9, 11, 27, 8, 54, 76, 23, 9, 63, 78, 23, 18, 25], @matrix.traverse | |
end | |
def test_with_3x2 | |
# 12 17 | |
# 9 11 | |
# 19 28 | |
input = [ [12, 17], [9, 11], [19, 28] ] | |
@matrix = MatrixCalc.new(input) | |
assert_equal [12, 17, 11, 28, 19, 9], @matrix.traverse | |
end | |
def test_with_4x6 | |
# 12 17 9 7 21 10 | |
# 19 28 11 25 2 6 | |
# 10 3 8 13 4 29 | |
# 5 12 9 14 31 18 | |
input = [ [12, 17, 9, 7, 21, 10], [19, 28, 11, 25, 2, 6], [10, 3, 8, 13, 4, 29], [5, 12, 9, 14, 31, 18]] | |
@matrix = MatrixCalc.new(input) | |
assert_equal [12, 17, 9, 7, 21, 10, 6, 29, 18, 31, 14, 9, 12, 5, 10, 19, 28, 11, 25, 2, 4, 13, 8, 3], @matrix.traverse | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment