Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spiral matrix - code review - ruby code - Feb. 25, 2018
def spiral_copy(inputMatrix)
# your code goes here
x_curr = 0
y_curr = 0
y_min = 0
y_max = inputMatrix[0].length - 1
x_min = 0
x_max = inputMatrix.length - 1
result = []
while x_min <= x_max and y_min <= y_max
x_curr = x_min
y_curr = y_min
while y_curr <= y_max
result.push(inputMatrix[x_curr][y_curr])
y_curr += 1
end
y_curr -= 1
x_curr += 1
while x_curr <= x_max
result.push(inputMatrix[x_curr][y_curr])
x_curr += 1
end
x_curr -= 1
y_curr -= 1
while y_curr >= y_min and x_min < x_max
result.push(inputMatrix[x_curr][y_curr])
y_curr -= 1
end
y_curr += 1
x_curr -= 1
while x_curr > x_min and y_min < y_max
result.push(inputMatrix[x_curr][y_curr])
x_curr -= 1
end
x_min += 1
x_max -= 1
y_min += 1
y_max -= 1
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment