Skip to content

Instantly share code, notes, and snippets.

@itsanna
Created March 21, 2017 01:19
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 itsanna/785ce6936e7a212eabb87e0940424c4c to your computer and use it in GitHub Desktop.
Save itsanna/785ce6936e7a212eabb87e0940424c4c to your computer and use it in GitHub Desktop.
Print 2-D array in spiral order in Golang
package main
import "fmt"
/*
Print 2-D array in spiral order
numbers := [
[2, 4, 6, 8],
[5, 9, 12, 16],
[1, 11, 5, 9],
[3, 2, 1, 8],
]
=> 2, 4, 6, 8, 5, 9, 12, 16, 1, 11, 5, 9, 3, 2, 1, 8
*/
func PrintSpiral(list [][]int, rows, cols int) {
// initialize variables
var T, B, L, R, dir int = 0, rows - 1, 0, cols - 1, 0
// the top-most must be less than or equal to bottom-most AND the left-most must be less than or equal to right-most
for {
if T >= B || L >= R {
break
}
// 0 - traverse right (going Left to Right)
if dir == 0 {
fmt.Println("going right")
for i := 0; i <= R; i++ {
fmt.Println(list[T][i])
}
T++
dir = 1
}
// 1 - traverse down (going Top to Bottom)
if dir == 1 {
fmt.Println("going down")
for i := T; i <= B; i++ {
fmt.Println(list[i][R])
}
R--
dir = 2
}
// 2 - traverse left
if dir == 2 {
fmt.Println("going left")
for i := R; i >= L; i-- {
//fmt.Println("---> ", R, i)
fmt.Println(list[B][i])
}
B--
dir = 3
}
// 3 - traverse up
if dir == 3 {
for i := B; i >= T; i-- {
fmt.Println("going up")
fmt.Println(list[i][L])
}
L++
dir = 0
}
}
fmt.Printf("wooo")
}
func main() {
l := [][]int{
{2, 4, 6, 8},
{5, 9, 12, 16},
{1, 11, 5, 9},
{3, 2, 1, 8},
}
m := 4 // number of rows
n := 4 // number of columns
PrintSpiral(l, m, n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment