Skip to content

Instantly share code, notes, and snippets.

@codephobia
Created November 24, 2018 10:03
Show Gist options
  • Select an option

  • Save codephobia/1dcb93e3bf3ba075de5468ebd3d6023d to your computer and use it in GitHub Desktop.

Select an option

Save codephobia/1dcb93e3bf3ba075de5468ebd3d6023d to your computer and use it in GitHub Desktop.
Josephus Permutation
package kata
func Josephus(items []interface{}, k int) []interface{} {
out := []interface{}{}
cnt := 0
last := 0
for len(items) > 0 {
for i, _ := range items {
if i < last {
continue
}
cnt++
if (cnt + k) % k == 0 {
out = append(out, items[i])
items = append(items[:i], items[i+1:]...)
if i == len(items) {
last = 0
} else {
last = i
}
break
}
if i == len(items) - 1 {
last = 0
}
}
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment