Course / The terminal and the language
Lowest and highest
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach lab: download it, edit the file named in the instructions, then submit.
gonomicon fetch week-1/12-04-lowest-and-highest
cd 12-04-lowest-and-highest
gonomicon submitGoal
Fill in LowHigh so it returns the lowest and the highest of its three numbers, using the named results low and high and a bare return.
Example
low, high := LowHigh(7, 2, 9)
fmt.Println(low, high) // 2 9
low, high = LowHigh(5, 5, 5)
fmt.Println(low, high) // 5 5
low, high = LowHigh(-3, -8, -1)
fmt.Println(low, high) // -8 -1
The lowest or the highest can be in any of the three positions, and the numbers may be negative or equal.
Rules
- Edit
lowhigh.go. The signature already names the results:(low, high int). Those two variables exist from the first line of the function. - Set them with
=, not:=. A:=would try to make a second variable with the same name. - Use
ifto compare the numbers. Do not use anything beyond what the lessons covered. - Finish with a bare
return(just the wordreturn) so the function hands back whatever is inlowandhigh. - Do not change the line
func LowHigh(a, b, c int) (low, high int) {. Do not add imports.
What you'll practice
Named results, a bare return, returning two values at once, and comparing numbers with if.
Run it
gonomicon fetch week-1/12-04-lowest-and-highest
cd 12-04-lowest-and-highest
gonomicon test
gonomicon submit
Hints
- The results
lowandhighalready exist as variables, set to0, the moment the function starts. Start them both ata, then comparebandcagainst them. - Write
low, high = a, a(with=, not:=). Then useifto movelowdown whenborcis smaller, and movehighup whenborcis bigger. low, high = a, a, thenif b < low { low = b },if c < low { low = c },if b > high { high = b },if c > high { high = c }, and finish with a barereturn.