Question
Extend the function `sentence.flipper()` so that it is vectorized, i.e., if the input `str` is a vector of strings, then this function should return a
Extend the function `sentence.flipper()` so that it is vectorized, i.e., if the input `str` is a vector of strings, then this function should return a vector where each element is a string that is flipped in accordance with the description above. Hint: there is certainly more than one way to modify `sentence.flipper()` so that it works over vectors. But look out for a simple strategy---you already know that `sentence.flipper()` works over single strings, so now just do something to apply this strategy over each element of a vector. Once this is done, your function should be producing outputs on the test cases below that match those described in the comments.
```{r, error=TRUE}
# Redefine sentence.flipper() here
sentence.flipper = function(str) {
paste(sapply(strsplit(strsplit(str, '\\s+')[[1]], ''),
function(x) paste(rev(x), collapse = '')), collapse = ' ')
}
# Should be "olleh ssenkrad ym dlo dneirf",
#"ev'i emoc ot kaeps htiw uoy niaga"
sentence.flipper(c("hello darkness my old friend",
"i've come to speak with you again"))
# Should be "reven annog evig uoy pu",
#"reven annog tel uoy nwod",
#"reven annog nur dnuora dna tresed uoy"
sentence.flipper(c("never gonna give you up",
"never gonna let you down",
"never gonna run around and desert you"))
```
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started