Simpsons String

Quickly now!

Time for a head count. Children line up according to height.

Uh, no wait! No, the sum total of the letters in your last name if each letter is assigned a numeric value according to its place in the alphabet, quickly now!

  • Principal Skinner

Here are the students:

springfield_elementary
## # A tibble: 30 × 1
##    name           
##    <chr>          
##  1 Allison Taylor 
##  2 Bart Simpson   
##  3 Cosine Tangent 
##  4 Database       
##  5 Dolph Starbeam 
##  6 Dubya Spuckler 
##  7 Erik           
##  8 Jake Boyman    
##  9 Janey Powell   
## 10 Jessica Lovejoy
## # … with 20 more rows

Let’s do the calculations:

  • Split up a string into two parts by spaces (“Milhouse Van Houten” → “Milhouse” “Van Houten”), take the last item
  • Split up the last name, make sure it is lowercase, deal with accents, use base letters to get the number of the alphabet, and reduce the whole thing together.
# split up name, get last name
get_last_name = function(name) {
  splt = str_split(name, pattern = " ", n=2)
  result = tail(splt[[1]], n = 1)
  return(result)
}

# split up last name, parse, and sum result 
letter_to_number_place <- function(input) {
  unpacked_name <- str_split(input, pattern = "")
  lowercase <- map(unpacked_name[[1]], tolower)
  letter_places <- map(lowercase, ~match(stringi::stri_trans_general(., "Latin-ASCII"), letters, nomatch = 0)) # Uter!
  total <- reduce(letter_places, sum)
  return(total)
}
# quickly now!
ordering <- springfield_elementary %>% 
  filter(str_detect(name, " ")) %>% 
  select(name) %>% 
  mutate(last_name = map_chr(name, get_last_name)) %>% 
  mutate(total = map_dbl(last_name, letter_to_number_place))

ordering %>% 
  arrange(total)
## # A tibble: 28 × 3
##    name            last_name total
##    <chr>           <chr>     <dbl>
##  1 Report Card     Card         26
##  2 Lewis Clark     Clark        45
##  3 Michael D'Amico D'Amico      45
##  4 Nikki McKenna   McKenna      61
##  5 Jimbo Jones     Jones        63
##  6 Martin Prince   Prince       65
##  7 Jake Boyman     Boyman       70
##  8 Dolph Starbeam  Starbeam     79
##  9 Rod Flanders    Flanders     79
## 10 Todd Flanders   Flanders     79
## # … with 18 more rows

Till next time!

Image Credit

building by Zach Bogart from the Noun Project

Zach Bogart
Zach Bogart
Data Explorer

Science, Design, & Data. I’ll know it when I see it.

Related