Skip to content

Instantly share code, notes, and snippets.

@AkselA
Last active June 30, 2019 23:31
Show Gist options
  • Save AkselA/793763566736874df9e6777984316b99 to your computer and use it in GitHub Desktop.
Save AkselA/793763566736874df9e6777984316b99 to your computer and use it in GitHub Desktop.
funfun <- function() {
x <- readline("What's your name?\n")
x0 <- sample(strsplit(tolower(x), NULL)[[1]])
# number of possible unique permutations, taking account of
# non-distinct objects
np <- factorial(length(x0))/prod(factorial(table(x0)))
count <- 0
rfunfun(x, np, count)
}
rfunfun <- function(x, np, count) {
count <- count + 1
if (count == 1) {
s <- c("I", "try.")
} else {
s <- c("I've", "tries.")
}
x <- sample(strsplit(tolower(x), NULL)[[1]])
x[1] <- toupper(x[1])
x <- paste(x, collapse="")
n <- paste0("You said ", x, "?\n")
a <- readline(n)
if (tolower(a) == "yes") {
cat(paste("Good. So nice to meat you,", x))
} else {
cat(paste(
"Too bad, but", s[1], "only had a",
paste0(round(1 - (((np-1)/np)^count), 6)*100, "%"),
"chance of getting that right in", count, s[2], "So...\n"))
Recall(x, np, count)
}
}
source(paste0("https://gist.githubusercontent.com/AkselA/",
"793763566736874df9e6777984316b99/raw/",
"3ce113f56637d57079f4a1e7b96297c2ef2313a8/funfun.r"))
source(paste0("https://gist.githubusercontent.com/AkselA/",
"793763566736874df9e6777984316b99/raw/",
"52298746b0f932eac7c13054196e9b67a0b0d578/rfunfun.r"))
funfun()
# What's your name?
# Aksel
# You said Lasek?
# no
# Too bad, but I only had a 0.8333% chance of getting that right in 1 try. So...
# You said Seakl?
# no
# Too bad, but I've only had a 1.6597% chance of getting that right in 2 tries. So...
# You said Alsek?
# no
# Too bad, but I've only had a 2.4792% chance of getting that right in 3 tries. So...
# You said Aelsk?
# no
# Too bad, but I've only had a 3.2919% chance of getting that right in 4 tries. So...
# You said Aeksl?
# no
# Too bad, but I've only had a 4.0978% chance of getting that right in 5 tries. So...
# You said Kesal?
# yes
# Good. So nice to meat you, Kesal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment