Vigenere Cypher Grid

[This article was first published on Numbers around us - Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Excel BI’s Excel Challenge #302— solved in R

Introduction

Today’s Excel BI puzzle is available here. It is pure generational work today. We have to create from scratch so called Vigenere grid. Exercise file today is purely for comparison reason: file.

Defining the Puzzle

The puzzle in question revolves around the Vigenere Cipher Grid, a cryptographic tool. The objective is to generate this grid, which appears to be a matrix of letters shifted in various ways.

Loading data from Excel

As I already mention we need test data only today.

library(tidyverse)
library(readxl)

test = read_excel(“Vigenere Cipher Grid.xlsx”, range = “A2:AA28”) 
  %>% column_to_rownames(‘…1’)

Tidyverse Approach

df <- tibble(base = LETTERS)

vigenere_grid <- bind_cols(df, map_dfc(0:24, ~ {
 shifted <- LETTERS[(seq_along(LETTERS) + .x) %% 26 + 1]
 tibble(!!LETTERS[.x + 1] := shifted)
}))

rownames(vigenere_grid) <- LETTERS
colnames(vigenere_grid) <- LETTERS

vigenere_grid = as.data.frame(vigenere_grid)

Base R Approach

vigenere_grid_base <- matrix(NA, 26, 26)
rownames(vigenere_grid_base) <- LETTERS
colnames(vigenere_grid_base) <- LETTERS

for (i in 1:26) {
 vigenere_grid_base[i,] <- LETTERS[(seq_along(LETTERS) + (i — 2)) %% 26 + 1]
}

vigenere_grid_base = as.data.frame(vigenere_grid_base)

Data.table Approach

library(data.table)

vigenere_grid_dt <- data.table(base = LETTERS)

for (i in 0:24) {
 shifted <- LETTERS[(seq_along(LETTERS) + i) %% 26 + 1]
 vigenere_grid_dt[, LETTERS[i + 1] := shifted]
}

setDT(vigenere_grid_dt)[, LETTERS := LETTERS]

vigenere_grid_dt = as.data.frame(vigenere_grid_dt) %>% 
 column_to_rownames(“LETTERS”)

colnames(vigenere_grid_dt) <- LETTERS

Validating Solutions

identical(vigenere_grid, test)
# [1] TRUE

identical(vigenere_grid_base, test)
# [1] TRUE

identical(vigenere_grid_dt, test)
# [1] TRUE

Tips for possible optimization

Approach 1: Tidyverse with purrr

  1. Avoiding Repetitive Operations
  2. Vectorize

Approach 2: Base R

  1. Vectorize
  2. Avoid Growing Objects

Approach 3: Data.table

  1. Use .SD and .SDcols
  2. Use set Function
  3. Avoid Copying

Thank you for taking the time to explore these methodologies with me! I’d love to hear your thoughts on the puzzle, the solutions provided, or any optimizations you might have in mind. Feel free to share your solutions and insights!


Vigenere Cypher Grid was originally published in Numbers around us on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: Numbers around us - Medium.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)