Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative approach to WordScrambler #10

Open
mrdwab opened this issue Dec 25, 2020 · 0 comments
Open

Alternative approach to WordScrambler #10

mrdwab opened this issue Dec 25, 2020 · 0 comments

Comments

@mrdwab
Copy link

mrdwab commented Dec 25, 2020

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 = " "))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant