-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrazify.R
48 lines (41 loc) · 1.41 KB
/
crazify.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
reverseLetters <- function(word){
vec <- strsplit(word, "")
vec <- rev(unlist(vec))
revStr <- paste(vec, collapse = "")
return(revStr)
}
reverseWords <- function(phrase){
vec <- strsplit(phrase, " ")
vec <- rev(unlist(vec))
revStr <- paste(vec, collapse = " ")
}
crazify <- function(phrase){
revPhrase <- reverseWords(phrase)
vec <- strsplit(revPhrase, " ")
for(i in 1:length(vec)){
vec[i] <- reverseLetters(vec[i]) #vec[i]
}
return(paste(vec, collapse = " "))
}
crazify("here i am")
# Challenge: try calling confuseTheCustomers() on the myMenu object below.
# It fails! Try using the debugger to fix it
# The output should look like:
# Titles Descriptions
# 1 Sandwich ## hcihwdnas otamot dna ecuttel ,nocaB ##
# 2 Soup ## puos keel dna moorhsum fo maerC ##
#' print a crazified version of the descriptions in a menu
#' stored as a dataframe, but not the titles of the entries in the menu
#' @param dataframe where column 1 is the name of the food and column 2 is the description
confuseTheCustomers<-function(menu){
colnames(menu)<-c("Titles","Descriptions")
menu$Descriptions <- crazify(menu$Descriptions)
print(menu)
}
myMenu <- data.frame(
titles = c("Sandwich","Soup"),
descriptions = c("## Bacon, lettuce and tomato sandwhich ##",
"## Cream of mushroom and leek soup ##")
)
#print(myMenu)
#confuseTheCustomers(myMenu)