-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_answers.R
More file actions
42 lines (31 loc) · 801 Bytes
/
R_answers.R
File metadata and controls
42 lines (31 loc) · 801 Bytes
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
library(stringr)
library(dplyr)
## Principle Financial Vending Machine Question
vm_string = "A1, Lays, 1.50
A2, Dorritos, 2.00
A3, Pringles, 1.00
B1, Banana, 0.50
B2, Apple, 1.25
B3, Pear, 1.75
C1, Oreos, 3.00
C2, Chips Ahoy, 2.75
C3, Nutella, 4.00"
items = unlist(str_split(vm_string, "\n"))
df <- as.data.frame(items)
vm <- df %>%
rowwise %>%
mutate(values = (str_split(items, ", ")),
key = values[1],
product = values[2],
cost = values[3])
vendingmachine <- function(k){
if (k %in% vm$key) {
finding <- vm %>% filter(k == key)
print(paste("Product at", k, "is", finding$product, "and the cost is", finding$cost))
} else {
print("No product is associated with given key")
}
}
vendingmachine("A1")
vendingmachine("C3")
vendingmachine("A7")