Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active April 8, 2018 23:39
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 primaryobjects/6a8ea01ce0c36087f5046c23ed122655 to your computer and use it in GitHub Desktop.
Save primaryobjects/6a8ea01ce0c36087f5046c23ed122655 to your computer and use it in GitHub Desktop.
getEncoding <- function(letter1, letter2) {
# Find the number for each letter.
index1 <- match(letter1, letters)
index2 <- match(letter2, letters)
# Find the index number within the shifted letters by adding the two indices and taking the remainder from the number of letters.
index <- (index1 + index2 - 1) %% length(letters)
index <- ifelse(index == 0, 26, index)
letters[index]
}
encode <- function(str, secret) {
index <- 1
keys <- unlist(strsplit(secret, ''))
len <- length(keys)
result <- sapply(unlist(strsplit(str, '')), function(ch) {
e <- getEncoding(ch, keys[index])
index <<- index + 1
if (index > len) {
index <<- 1
}
e
})
paste(result, collapse='')
}
lumicjcnoxjhkomxpkwyqogywq
uvrufrsryherugdxjsgozogpjralhvg
flrlrkfnbuxfrqrgkefckvsa
zhvpsyksjqypqiewsgnexdvqkncdwgtixkx
print(encode('thepackagehasbeendelivered', 'snitch'))
print(encode('theredfoxtrotsquietlyatmidnight', 'bond'))
print(encode('murderontheorientexpress', 'train'))
print(encode('themolessnuckintothegardenlastnight', 'garden'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment