Skip to content

Instantly share code, notes, and snippets.

@Frost
Last active January 18, 2018 16:39
Show Gist options
  • Select an option

  • Save Frost/631abdb8a997e632328d to your computer and use it in GitHub Desktop.

Select an option

Save Frost/631abdb8a997e632328d to your computer and use it in GitHub Desktop.
Spiral print

Given a matrix

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20

Unroll it as a spiral by printing first the top row, then the last column, then the bottom row backwards, etc.

The expected output of the above matrix is the following:

1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12

It is ok if the matrix is hardcoded in your program as a list of lists.

transpose = (a) -> Object.keys(a[0]).map (c) -> a.map (r) -> r[c]
spiralPrint = (matrix) ->
out = [].concat matrix[0]
if matrix.length isnt 1
matrix = transpose(matrix[1..matrix.length]).reverse()
Array::push.apply out, spiralPrint matrix
return out
import Data.List
spiral_print :: [[Integer]] -> [Integer]
spiral_print matrix
| null matrix = []
| otherwise = head matrix ++ recurse matrix
where
recurse = spiral_print . reverse . transpose . tail
main = do
let x = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]
in (putStrLn . show . spiral_print) x
def spiral(m):
if matrix == []:
return []
else:
return matrix[0] + spiral(map(list,zip(*matrix[1::]))[::-1])
require 'matrix'
x = [
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20]
]
# expected_output: "1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12"
def spiral_print(matrix)
spiral = []
until matrix.empty?
spiral << matrix.shift
matrix = Matrix[*matrix].column_vectors.reverse.map(&:to_a)
end
puts spiral * " "
end
spiral_print(x)
s=lambda m:[]if m==[]else m[0]+s(map(list,zip(*m[1::]))[::-1])
x = [
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20]
]
# expected_output: "1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12"
def rotate(matrix)
new_matrix = []
# manual matrix transpose
matrix.first.length.times do |j|
new_row = []
matrix.length.times do |i|
new_row << matrix[i][j]
end
new_matrix << new_row
end
new_matrix.reverse
end
def spiral_print(matrix)
if matrix.empty?
puts
else
print matrix.shift.join(" ")
return if matrix.empty?
print " "
spiral_print(rotate(matrix))
end
end
spiral_print(x)
require 'matrix'
x = [
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20]
]
# expected_output: "1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12"
def spiral_print(matrix)
puts [].tap do |spiral|
until matrix.empty?
spiral << matrix.shift
matrix = Matrix[*matrix].column_vectors.reverse.map(&:to_a)
end
end
end
spiral_print(x)
require 'matrix'
x = [
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20]
]
# expected_output: "1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12"
puts [].tap { |s| s << x.shift and x = Matrix[*x].column_vectors.reverse.map(&:to_a) until x.empty? } * " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment