Skip to content

Instantly share code, notes, and snippets.

@hannahflaherty
Created August 6, 2019 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannahflaherty/efedcdf65f5bff79e238c19165347272 to your computer and use it in GitHub Desktop.
Save hannahflaherty/efedcdf65f5bff79e238c19165347272 to your computer and use it in GitHub Desktop.
library(lazyeval)
library(dplyr)
phone_knight_combinations <- function(n, starting_value = 1) {
mapping <- data.frame(this_value = c(1,1,6,6,6,7,7,2,2,9,9,4,4,4,3,3,8,8,0,0),
next_value = c(6,8,1,7,0,6,2,7,9,2,4,9,0,3,4,8,1,3,4,6)
)
combinations <- data.frame(value_1 = c(starting_value), this_value = c(starting_value))
for (i in 2:n) {
combinations <- merge(combinations, mapping)
mutate_call <- lazyeval::interp(~ a, a = as.name("next_value"))
combinations <- combinations %>%
mutate_(.dots = setNames(list(mutate_call), paste0("value_",i))) %>%
select(-this_value) %>%
rename(this_value = next_value)
}
return(dim(combinations)[1])
}
phone_knight_combinations(5, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment