Swap first and last… letter

[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 #317 — solved in R

Defining the Puzzle:

Today we are move single letters from starts to end of words. But with some exceptions.

Swap the first and last letter of each words in given names. But if either first or last letter or both are vowels, then don’t swap for that word.
Ex. Barack Obama
In first word, B and k will be swapped. In second word, O and a both are vowels, hence swapping will not be done for this word.
Hence, answer would be Karacb Obama.

Loading Data from Excel:

We need to load data and libraries.

library(tidyverse)aa
library(readxl)

input = read_excel(“Swap First and Last Letter in Words.xlsx”, range = “A1:A10”)
test = read_excel(“Swap First and Last Letter in Words.xlsx”, range = “B1:B10”)

Approach 1: Tidyverse with purrr

switch_consonants <- function(word) {
 vowels <- c(“a”, “e”, “i”, “o”, “u”, “A”, “E”, “I”, “O”, “U”)
 
 words <- str_split(word, “\\s+”)[[1]]
 new_words <- map_chr(words, function(w) {
 if (str_detect(w, “^\\w\\.$”)) {
 return(w)
 }
 
 first_char <- str_sub(w, 1, 1)
 last_char <- str_sub(w, nchar(w), nchar(w))
 
 if (!(first_char %in% vowels) && !(last_char %in% vowels)) {
 w <- paste0(last_char, str_sub(w, 2, nchar(w)-1), first_char)
 }
 return(w)
 })
 
 result = new_words %>%
 paste(collapse = “ “) %>%
 str_to_lower() %>%
 str_to_title()
 
 return(result)
 }
 
 result = input %>%
 mutate(`Answer Expected` = map_chr(Names, switch_consonants)) %>%
 select(-Names)

Approach 2: Base R

switch_consonants <- function(word) {
 vowels <- c(“a”, “e”, “i”, “o”, “u”, “A”, “E”, “I”, “O”, “U”)
 
 words <- unlist(strsplit(word, “\\s+”))
 new_words <- sapply(words, function(w) {
 if (grepl(“^\\w\\.$”, w)) {
 return(w)
 }
 
 first_char <- substr(w, 1, 1)
 last_char <- substr(w, nchar(w), nchar(w))
 
 if (!(first_char %in% vowels) && !(last_char %in% vowels)) {
 w <- paste0(last_char, substr(w, 2, nchar(w)-1), first_char)
 }
 return(w)
 })
 
 result <- paste(new_words, collapse = “ “)
 result <- tolower(result)
 result <- tools::toTitleCase(result)
 
 return(result)
}

input$`Answer Expected` <- sapply(input$Names, switch_consonants)
input <- input[ , !(names(input) %in% ‘Names’)]  %>% data.frame(`Answer Expected` = .)

Data.table approach:

It would change only by method of calling, so forgive me that I am not taking it.

Validation:

identical(result, test) 
#> [1] TRUE

identical(input$Answer.Expected, test$`Answer Expected`)
#> [1] TRUE

If you like my publications or have your own ways to solve those puzzles in R, Python or whatever tool you choose, let me know.


Swap first and last… letter 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)