Course outline
Course / The terminal and the language

Lowest and highest

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each 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 submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal

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 if to compare the numbers. Do not use anything beyond what the lessons covered.
  • Finish with a bare return (just the word return) so the function hands back whatever is in low and high.
  • 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

  1. The results low and high already exist as variables, set to 0, the moment the function starts. Start them both at a, then compare b and c against them.
  2. Write low, high = a, a (with =, not :=). Then use if to move low down when b or c is smaller, and move high up when b or c is bigger.
  3. low, high = a, a, then if b < low { low = b }, if c < low { low = c }, if b > high { high = b }, if c > high { high = c }, and finish with a bare return.

Something wrong with this lesson? Report a problem