From 1ad36a05618d6d4e4a2238ac42b887d58f419957 Mon Sep 17 00:00:00 2001 From: Alice D Date: Thu, 24 Dec 2020 17:29:22 -0500 Subject: [PATCH 1/2] method passing 6/10 assertions --- lib/hourglass.rb | 34 +++++++++++++++++++++++++++++++++- test/hourglass_test.rb | 27 ++++++++++++++++----------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/lib/hourglass.rb b/lib/hourglass.rb index 4186b23..2b39780 100644 --- a/lib/hourglass.rb +++ b/lib/hourglass.rb @@ -1,4 +1,36 @@ def hourglass(matrix) - raise NotImplementedError, "The matrix method has not been implemented yet!" + row = 1 + column = 1 + max = nil + while row <= 4 + while (column >= 1 && column <= 4) + puts "row # #{row}, column # #{column}" + sum = matrix[row][column] + matrix[row - 1][column] + matrix[row + 1][column] + matrix[row][column - 1] + matrix[row][column + 1] + puts "hourglass sum is: #{matrix[row][column]} + #{matrix[row - 1][column]} + #{matrix[row + 1][column]} + #{matrix[row][column - 1]} + #{matrix[row][column + 1]}" + puts "which equals #{sum}" + + if (max == nil || sum > max) + max = sum + puts "new max is #{max}" + end + column += 1 + puts "column is now #{column}" + + # if column == 5 + # column = 0 + # puts "resetting column to be #{column}" + # end + puts "------------------end of inner column loop" + end + if column == 5 + column = 1 + puts "resetting column to be #{column}" + end + row += 1 + puts "row is #{row}" + puts "---------------------------------end of outer row loop" + end + puts "final max is #{max}" + return max end \ No newline at end of file diff --git a/test/hourglass_test.rb b/test/hourglass_test.rb index 03d7eb2..6abe1f1 100644 --- a/test/hourglass_test.rb +++ b/test/hourglass_test.rb @@ -10,6 +10,7 @@ describe "Hourglass" do it "will return 7 for the 1st example matrix" do # Arrange + puts "test # 1" matrix = [ [1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0], @@ -28,9 +29,10 @@ it "will return 28 for the 2nd example matrix" do # Arrange + puts "test # 2" matrix = [ [-9, -9, -9, 1, 1, 1], - [ 0, -9, 0, 4, 3, 2], + [ 0, -9, 0, 4, 3, 2], [-9, -9, -9, 1, 2, 3], [ 0, 0, 8, 6, 6, 0], [ 0, 0, 0, -2, 0, 0], @@ -46,6 +48,7 @@ it "will return 19 for the 3rd example matrix" do # Arrange + puts "test # 3" matrix = [ [ 1, 1, 1, 0, 0, 0], [ 0, 1, 0, 0, 0, 0], @@ -63,7 +66,8 @@ end it "will return 0 for a matrix of 0s" do - # Arrange + # Arrange + puts "test # 4" matrix = [ [ 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0], @@ -81,15 +85,15 @@ end it "will return 13 for a this matrix" do - # Arrange + # Arrange + puts "test # 5" matrix = [ - [1, 1, 1, 0, 0, 0] - [0, 1, 0, 0, 0, 0] - [1, 1, 1, 0, 0, 0] - [0, 9, 2, -4, -4, 0] - [0, 0, 0, -2, 0, 0] - [0, 0, -1, -2, -4, 0,] - ] + [1, 1, 1, 0, 0, 0], + [0, 1, 0, 0, 0, 0], + [1, 1, 1, 0, 0, 0], + [0, 9, 2, -4, -4, 0], + [0, 0, 0, -2, 0, 0], + [0, 0, -1, -2, -4, 0]] # Act answer = hourglass(matrix) @@ -99,7 +103,8 @@ end it "will return -19 for a this matrix" do - # Arrange + # Arrange + puts "test # 6" matrix = [ [ 0, -4, -6, 0, -7, -6], [-1, -2, -6, -8, -3, -1], From 055222d574341dc2e7b7d68a23e93ad5b61068cd Mon Sep 17 00:00:00 2001 From: Alice D Date: Thu, 24 Dec 2020 17:48:54 -0500 Subject: [PATCH 2/2] removed puts statements and changed from crosses to hourglasses; all tests passing --- lib/hourglass.rb | 18 +----------------- test/hourglass_test.rb | 2 +- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/lib/hourglass.rb b/lib/hourglass.rb index 2b39780..24dcec8 100644 --- a/lib/hourglass.rb +++ b/lib/hourglass.rb @@ -5,32 +5,16 @@ def hourglass(matrix) max = nil while row <= 4 while (column >= 1 && column <= 4) - puts "row # #{row}, column # #{column}" - sum = matrix[row][column] + matrix[row - 1][column] + matrix[row + 1][column] + matrix[row][column - 1] + matrix[row][column + 1] - puts "hourglass sum is: #{matrix[row][column]} + #{matrix[row - 1][column]} + #{matrix[row + 1][column]} + #{matrix[row][column - 1]} + #{matrix[row][column + 1]}" - puts "which equals #{sum}" - + sum = matrix[row][column] + matrix[row - 1][column - 1] + matrix[row - 1][column] + matrix[row - 1][column + 1] + matrix[row + 1][column - 1] + matrix[row + 1][column] + matrix[row + 1][column + 1] if (max == nil || sum > max) max = sum - puts "new max is #{max}" end column += 1 - puts "column is now #{column}" - - # if column == 5 - # column = 0 - # puts "resetting column to be #{column}" - # end - puts "------------------end of inner column loop" end if column == 5 column = 1 - puts "resetting column to be #{column}" end row += 1 - puts "row is #{row}" - puts "---------------------------------end of outer row loop" end - puts "final max is #{max}" return max end \ No newline at end of file diff --git a/test/hourglass_test.rb b/test/hourglass_test.rb index 6abe1f1..4c3b28b 100644 --- a/test/hourglass_test.rb +++ b/test/hourglass_test.rb @@ -118,6 +118,6 @@ answer = hourglass(matrix) # Assert - expect(answer).must_equal -19 + expect(answer).must_equal -19 end end \ No newline at end of file