Created
April 28, 2015 18:44
-
-
Save rakesh87/e969dfb791d5da5bdb53 to your computer and use it in GitHub Desktop.
transverse spiral all element of matrix
This file contains 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
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