iperm: Permutation Iterator

Description Usage Arguments Details Value Examples

View source: R/iperm.R

Description

Create an iterator for all permutations of size k of integers 1 to n.

Usage

1
2
3
iperm(n, k = n)

ipermv(values, k = length(values))

Arguments

n

positive integer

k

positive integer

values

iterable (subsettable by [)

Details

Value

iterator object

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
x <- iperm(3)
ct <- 0
while (hasNext(x))
{
  ct <- ct + 1
  i <- nextElem(x)
  cat(sprintf("%3d : %s\n", ct, paste0(i, collapse = " ")))
}


as.list(ipermv(c("R", "G", "B")))

Example output

  1 : 1 2 3
  2 : 1 3 2
  3 : 2 1 3
  4 : 2 3 1
  5 : 3 1 2
  6 : 3 2 1
[[1]]
[1] "R" "G" "B"

[[2]]
[1] "R" "B" "G"

[[3]]
[1] "G" "R" "B"

[[4]]
[1] "G" "B" "R"

[[5]]
[1] "B" "R" "G"

[[6]]
[1] "B" "G" "R"

combiter documentation built on May 2, 2019, 7:24 a.m.