R/enumerate.permutations.R

Defines functions enumerate.permutations

Documented in enumerate.permutations

#' Enumerate Permutations of Items
#'
#' This function produces a matrix whose rows provide all possible permutations
#' of the set \eqn{{1, 2, ..., n}}.
#'
#' @param nItems The size of the set \eqn{{1, 2, ..., n}}, i.e., \eqn{n}.
#'
#' @return A matrix of integers, where each row is a permutation.
#'
#' @export
#' @examples
#' enumerate.permutations(5)
#'
enumerate.permutations <- function(nItems) {
  if ( nItems == 0 ) return(matrix(integer(), nrow=0, ncol=0))
  engine <- function(x) {
    if ( length(x) == 1 ) x
    else {
      result <- matrix(nrow=0, ncol=length(x))
      for ( i in seq_along(x) ) {
        result <- rbind(result, cbind(x[i], Recall(x[-i])))
      }
      result
    }
  }
  engine(as.integer(1:nItems))
}

Try the salso package in your browser

Any scripts or data that you put into this service are public.

salso documentation built on July 26, 2023, 5:32 p.m.