Homework: Working with strings

HW
Week12
Author

Your Name

Published

April 19, 2024

Note: This assignment must be submitted in github classroom.

Complete the following tasks in R or Python.

These exercises are stolen from kbodwin’s decodeR repository.

Setup

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
message <- read.csv("./message.txt", stringsAsFactors = FALSE)$Word

Warm-up exercises

  1. How many characters are in the scrambled message?
  2. How many of these characters are white space?
  3. How many words are in the scrambled message?
  4. Show all the punctuation marks in the scrambled message.
  5. Print out, in all capitals, the longest word in the scrambled message.
  6. Print out every piece of a word that starts with the letter “m” and ends with the letter “z” in the scrambled message.

Decode a message

Now 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. Any 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. Hint: punctuation!

  7. Every word that starts with k, change that to a v. Hint: capitalization!

  8. Use str_c(str) or ' '.join(map(str, 1)) to recombine all your words into a message.

  9. Find the movie this quote is from.