Skip to content

Alternative approach to WordScrambler #10

@mrdwab

Description

@mrdwab

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 = " "))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions