Skip to content

Instantly share code, notes, and snippets.

@rakesh87
Created April 28, 2015 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakesh87/e969dfb791d5da5bdb53 to your computer and use it in GitHub Desktop.
Save rakesh87/e969dfb791d5da5bdb53 to your computer and use it in GitHub Desktop.
transverse spiral all element of matrix
module Matrix
class << self
def to_spiral(input_array)
array_length = input_array.length
# return element itself if array length is 1
return input_array[0][0] if array_length == 1
# array length greater than 1
i = 0
j = array_length-1
while( i < j)
k = i
while(k < j) do
print input_array[i][k]
k += 1
end
m = i
while(m < j) do
print input_array[m][j]
m += 1
end
n = j
while(n > i) do
print input_array[j][n]
n -= 1
end
o = j
while(o > i) do
print input_array[o][i]
o -= 1
end
i += 1
j -= 1
end
# mertix dimension check for even and odd
if array_length.even?
else
mid = array_length/2
print input_array[mid][mid]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment