-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlasagna_master.go
63 lines (53 loc) · 1.87 KB
/
lasagna_master.go
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
package lasagna
// PreparationTime returns the estimate for the total preparation time based on the number of layers as an `int`.
// Accepts a slice of layers as a `[]string` and the average preparation time per layer in minutes as an `int`.
func PreparationTime(layers []string, avgPrepTime int) int {
if avgPrepTime <= 0 {
avgPrepTime = 2
}
return len(layers) * avgPrepTime
}
// Quantities returns two values of noodles as an `int` and sauce as a `float64`.
// Accepts a slice of layers as parameter as a `[]string`.
func Quantities(layers []string) (int, float64) {
var noodles int
var sauce float64
for _, layer := range layers {
if layer == "noodles" {
noodles += 50 // grams
} else if layer == "sauce" {
sauce += 0.2 // liters
}
}
return noodles, sauce
}
// AddSecretIngredient passes `MyList` by reference.
// Accepts two slices of ingredients of type `[]string`.
func AddSecretIngredient(friendList []string, myList []string) {
if myList[len(myList)-1] == "?" {
myList[len(myList)-1] = friendList[len(friendList)-1]
} else {
panic("You already know the secret ingredient!")
}
}
// ScaleRecipe returns a slice of `float64` of the amounts needed for the desired number of portions.
// Accepts a slice of `float64` amounts needed for 2 portions and the number of portions, `int`, you want to cook.
func ScaleRecipe(quantities []float64, portions int) []float64 {
singles, newQuantities := []float64{}, []float64{}
if portions == 2 || portions < 0 {
newQuantities = quantities
} else {
// These could be merged into a single loop and the dedicated singles slice isn't needed.
for _, quantity := range quantities {
singles = append(singles, quantity/2.0)
}
if portions == 1 {
newQuantities = singles
} else {
for _, quantity := range singles {
newQuantities = append(newQuantities, quantity*float64(portions))
}
}
}
return newQuantities
}