PA 5.1: Scrambled Message

stringr + Regular Expressions

Download starter qmd file

library(tidyverse)

Pick your poison!

You may choose to work through Q1 - Q6 using a data.frame message_data and follow the dplyr pipeline syntax…

message_data <- read_csv("https://github.com/earobinson95/stat331-calpoly/raw/master/practice-activities/data/scrambled_message.txt")

class(message_data)
[1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame" 

…or you might prefer to work with the character vector message and use indexing – e.g. message[1] gives you the first element.

message <- message_data |> 
  pull(Word)

class(message)
[1] "character"
Tip

In this activity, a “word” is a set of characters with no white space. That is, even though many of elements of the scrambled mess vector are nonsense, and some have punctuation, you can consider each element to be a “word”.

Warm-up exercises

  1. How many characters are in the scrambled message?

  2. How many words are in the scrambled message?

  3. Print out every word in the scrambled message that starts with the letter “m”.

  4. Print out every word in the scrambled message that ends with a punctuation mark.

  5. Print out the longest word in the scrambled message.

  6. Print out the punctuation symbols that are present in the scrambled message. (This one is the trickiest! You will need to use a number of steps and stringr functions.)

Decode the Message

Caution

You likely want to work with the message character vector for decoding. You should still use piping to chain the steps together!

Complete the following steps to decode the message.

  1. Remove any spaces before or after each word.

  2. No word should be longer than 16 characters. Drop all extra characters off the end of each word.

  3. Every time you see the word “ugh”, with any number of h’s, followed by a punctuation mark, delete this.

  4. Replace all instances of exactly 2 a’s with exactly 2 e’s.

  5. Replace all z’s with t’s.

  6. Every word that ends in b, change that to a y.

  7. Every word that starts with k (or K), change that to a v.

  8. Recombine all your words into a message with a stringr function.

  9. Find the movie this quote is from.

Canvas Quiz Submission

What is the name of the movie the quote is from?