Skip to content

Instantly share code, notes, and snippets.

@takeo-asai
Created February 13, 2016 09:14
Show Gist options
  • Save takeo-asai/6ee60588638fd9eb2d80 to your computer and use it in GitHub Desktop.
Save takeo-asai/6ee60588638fd9eb2d80 to your computer and use it in GitHub Desktop.
let a = [1,2,3]
a.permutaion(2)
// => [[1, 2], [2, 1], [1, 3], [3, 1], [2, 3], [3, 2]]
let a = [1,2,3]
a.combination(2)
// => [[1, 2], [1, 3], [2, 3]]
a.repeatedPermutation(2)
// => [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
let a = [1, 2, 3]
var p = PermutationGenerator(elements: a, indices: [1, 2, 0])
p.next()
// => Optional(2), =a[1]
p.next()
// => Optional(3), =a[2]
p.next()
// => Optional(1), =a[0]
p.next()
// => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment