LEXF: Enumerating k-mers Lexicographically | Ben Cunningham

LEXF: Enumerating k-mers Lexicographically

Problem by Rosalind · on July 1, 2012

Assume that an alphabet has a predetermined order; that is, we write the alphabet as a permutation , where . For instance, the English alphabet is organized as .

Given two strings and having the same length , we say that precedes in the lexicographic order (and write ) if the first symbol that doesn’t match satisfies in .

Given: A collection of at most 10 symbols defining an ordered alphabet, and a positive integer ().

Return: All strings of length that can be formed from the alphabet, ordered lexicographically (use the standard order of symbols in the English alphabet).

Sample Dataset

A C G T
2

Sample Output

AA
AC
AG
AT
CA
CC
CG
CT
GA
GC
GG
GT
TA
TC
TG
TT

R

library(readr)
library(gtools)
Error in library(gtools): there is no package called 'gtools'
library(magrittr)

f <- "lexf.txt"

raw <-
  read_file(f) %>%
  strsplit(split = "\n") %>%
  unlist()

a <-
  raw[1] %>%
  strsplit(split = " ") %>%
  unlist()
n <- as.numeric(raw[2])

p <- permutations(length(a), n, v = a, repeats.allowed = TRUE)
Error in permutations(length(a), n, v = a, repeats.allowed = TRUE): could not find function "permutations"
apply(p, 1, paste, collapse = "") %>%
  cat(sep = "\n")
Error in apply(p, 1, paste, collapse = ""): object 'p' not found