-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathgive_candygrams.R
More file actions
101 lines (65 loc) · 1.91 KB
/
give_candygrams.R
File metadata and controls
101 lines (65 loc) · 1.91 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#' Announces the number of candygrams for a person.
#'
#' @param person The candygram recipient
#' @param number How many grams they got
#' @param extra_message A string giving extra commentary.
#'
#' @return A candy gram announcement
#'
#' @importFrom stringr str_detect str_to_title
#' @importFrom english as.english
#'
#' @export
give_candygrams <- function(person, number,
extra_message = NULL) {
stopifnot(number > 0)
if (str_detect(person, "Gretchen")) {
return(cat("None for Gretchen Weiners."))
}
if (is.null(extra_message)) {
extra_message <- add_commentary(person, number)
}
number <- str_to_title(as.english(number))
glue::glue("{number} for {person}.{extra_message}")
}
#' Announces the number of candygrams for 1 or more people.
#'
#' @param people The candygram recipients
#' @param numbers How many grams they got
#' @param extra_message A vector of possible extra commentary.
#'
#' @return A candy gram announcement
#'
#' @importFrom stringr str_detect str_to_title
#' @importFrom english as.english
#'
#' @export
give_many_candygrams <- function(people, numbers,
extra_messages = NULL) {
for (i in 1:length(people)) {
if(is.na(extra_messages[i])){
print(give_candygrams(people[i], numbers[i]))
}
else {
print(give_candygrams(people[i], numbers[i], extra_messages[i]))
}
}
}
people <- c("Ryan", "Lukas", "Richard", "David")
numbers <- c(10, 10, 3, 20)
extra_messages <- c("Incredible", NULL, NULL, NULL)
#' Tacks commentary on to candygram announcement
#'
#' @param person The candygram recipient
#' @param number How many grams they got
#'
#' @return A string (possibly blank)
add_commentary <- function(person, number) {
if (stringr::str_detect(person, "Aaron")) {
return("They are from Regina.")
}
if (number > 3) {
return(glue::glue("You go, {person}!"))
}
return("")
}