Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire - Alice D #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/hourglass.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@

def hourglass(matrix)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "The matrix method has not been implemented yet!"
row = 1
column = 1
max = nil
while row <= 4
while (column >= 1 && column <= 4)
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
end
column += 1
end
if column == 5
column = 1
end
row += 1
end
return max
end
29 changes: 17 additions & 12 deletions test/hourglass_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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)
Expand All @@ -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],
Expand All @@ -113,6 +118,6 @@
answer = hourglass(matrix)

# Assert
expect(answer).must_equal -19
expect(answer).must_equal -19
end
end