Created
June 28, 2016 05:00
-
-
Save watzon/44d63092a5da5e6f8911b7cb9177ab91 to your computer and use it in GitHub Desktop.
Example of a recursive rotation function in Go
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
package main | |
import "fmt" | |
func rotate(index int, args []int) []int { | |
if len(args) == 0 { | |
return []int{} | |
} | |
if index == -1 { | |
return args | |
} | |
if index == 0 { | |
args[0], args[len(args)-1] = args[len(args)-1], args[0] | |
return rotate(1, args) | |
} | |
if index == len(args)-1 { | |
return rotate(-1, args) | |
} | |
args[index], args[len(args)-index-1] = args[len(args)-index-1], args[index] | |
return rotate(index+1, args) | |
} | |
func main() { | |
mylist := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} | |
rotated := rotate(0, mylist) | |
fmt.Println(rotated) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment