Problem overview
You complete the function isNegative(n: Int), which takes one integer and prints a word. It prints negative when n is less than zero and positive otherwise. The function doesn't return anything. Its job is the side effect of writing a line to the console.
Three examples show the rules:
n = 5printspositive.n = -2printsnegative.n = 0printspositive. Zero is not less than zero, so it counts with the non-negative side, even though it isn't strictly positive.
Approach
There are exactly two outcomes, and one yes-or-no question decides between them: is n < 0? That is what an if with an else is for. If the answer is yes, print one word. If it is no, print the other. Because every integer is either less than zero or not, one of the two branches always runs and never both.
Step by step
- Write the test. Start with an
ifwhose condition isn < 0and leave the body empty. Getting the comparison right first is the whole problem. Use<, not<=, so that zero does not match. - Handle the negative case. Inside the
ifblock, callprintln("negative"). - Handle everything else. Add an
elsebranch that callsprintln("positive"). Theelsehas no condition of its own. It runs for every value that failed theiftest: zero and all the positive numbers.
The solution in Kotlin
class Solution {
fun isNegative(n: Int) {
if (n < 0) {
println("negative")
} else {
println("positive")
}
}
}
A few Kotlin details are worth knowing:
- No return type. The function header ends with
(n: Int)and no: Type. In Kotlin that means the function returnsUnit, the "nothing useful" value. You don't writereturn. printlnadds the newline. Each call writes the text and moves to the next line, so each call produces exactly one line of output.- Strings use double quotes.
"negative"is aString. Kotlin's single quotes are for single characters, so'negative'would not compile. - The condition must be a
Boolean.n < 0is one. Kotlin doesn't accept anIntas a condition the way some languages treat zero as false.
Calling Solution().isNegative(-2) prints negative. Calling it with 5 or with 0 prints positive.
Edge cases and common mistakes
- Zero. Writing
n <= 0makes the function printnegativefor zero, which contradicts Example 2. Zero belongs in theelsebranch. - Two separate
ifstatements. One forn < 0and another forn >= 0gives the right output, but it asks two questions where one is enough, and it is easy to leave a gap or an overlap between them. Anelseguarantees that exactly one branch runs, whatever the value ofn. - Returning instead of printing. This lesson is checked by what the function prints. A version that returns the word, or one declared with a
Stringreturn type, prints nothing. - Quotes inside the text. The output should be the bare word. The quote marks in the code only mark where the string starts and ends, and they aren't printed.
- Big magnitudes.
Intholds values from about -2.1 billion to 2.1 billion, and the comparison with zero works for all of them. Nothing in this function can overflow.
Testing
Try these inputs in the editor above:
5printspositive.0printspositive.-2printsnegative.-1printsnegative, the closest negative value to zero.