Useless_R_functions icon indicating copy to clipboard operation
Useless_R_functions copied to clipboard

Alternative approach to WordScrambler

Open mrdwab opened this issue 4 years ago • 0 comments

Hi Tomaž. I know the typical idiom for scrambling letters in R involves strsplit.

Here's another approach: use charToRaw and rawToChar instead.

# Helper function. Can also be placed inside of WordScrambler() instead or be an
# anonymous function in the `tapply` step.
fun <- function(x) rawToChar(sample(x))

WordScrambler <- function(text) {
    w <- charToRaw(text)
    words <- cumsum(w == "20")
    tolower(paste(tapply(w[w != "20"], words[w != "20"], fun), collapse = " "))
}

mrdwab avatar Dec 25 '20 01:12 mrdwab