Skip to content
Exercise · From a file to a project · lesson 9 of 10

Exercise: fix it from the test report

Clamp(x, lo, hi int) int must return x clamped to the range from lo to hi: lo when x is below lo, hi when x is above hi, otherwise x itself. It has a bug. Run the tests, find the subtest with a --- FAIL line in the report, and fix the function.

  • Clamp(5, 0, 10) → 5
  • Clamp(-3, 0, 10) → 0
  • Clamp(15, 0, 10) → 10

Constraints: lo ≤ hi, and lo may equal hi; every number is from -1000000 to 1000000.

Do not add extra imports unless the task says so explicitly.

clamp.go — go
go · 12 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way