TL ; DR:
slices.Min(s) gives the smallest value of a slice.
slices.Max(s) gives the largest value.
Go has no sum function, so a loop adds the values up.
prices := []int{4, 9, 2, 7}
fmt.Println(slices.Min(prices)) // 2
fmt.Println(slices.Max(prices)) // 9
total := 0
for _, p := range prices {
total += p
}
fmt.Println(total) // 22
Full lesson:
In Maximum Number In Array, a loop found the largest value. Go 1.21 added that loop to the slices package. You used that package for slices.Contains.
slices.Min(s)gives the smallest value of the slices.slices.Max(s)gives the largest value.
The max(a, b) from Maximum Number In Array compares separate values. slices.Max looks at a whole slice.
Go has no function that adds up a slice. Write the loop from Sum of Numbers. Start total at 0, then add each value.
Watch out for an empty slice. slices.Min and slices.Max panic on it, and the program stops. An empty slice has no smallest value. Check len(s) > 0 first. The sum loop just leaves total at 0.
Your task
minutes holds how many minutes you practiced coding on each day of a week.
Store the shortest day in shortest and the longest day in longest. Store the week's total in total. Go has no sum function, so add the values up with a loop.
Use the built-ins from this lesson. The code at the bottom prints all three.
Expected output:
shortest = 15
longest = 60
total = 235Free plan: 30 tutor messages. PRO is unlimited.
