Problem overview
The method negative receives a number n and returns its negative. If n is already negative, it comes back unchanged. The examples are negative(4) = -4, negative(15) = -15, negative(-4) = -4 and negative(0) = 0.
This is the usual answer to how to make a number negative in Python: put a minus sign in front of it. The twist in this lesson is that a negative input must not be flipped to positive, which is what the minus sign would do on its own.
How to make a number negative in Python
The unary minus operator - changes the sign of a number. It works on integers and floats and also on variables:
x = 5
x = -x
print(x) # -5
print(-(-5)) # 5
print(-2.5) # -2.5
The second line shows the problem. Applying - to a number that is already negative makes it positive. So -n works for positive inputs but would turn -4 into 4, and the lesson wants -4.
Approach: check the sign first
There are two cases, and an if / else statement separates them:
- If
n < 0, the number is already negative, so return it as it is. - Otherwise
nis zero or positive, and-nis the negative value. For zero,-0is0, which matches the last example.
Step by step
- Write the condition.
if n < 0:asks whether the input is already negative. - Return
nunchanged inside theif. - Add an
elsebranch that returns-n. It handles zero and all positive numbers.
The solution in Python
class Solution:
def negative(self, n: int) -> int:
if n < 0:
return n
else:
return -n
Python details to notice:
- Indentation marks the branches. The two
returnlines are indented underifandelse, and a missing indent is a syntax error. - The
elseis optional. Afterreturn ninside theif, the function has already ended, so the second return could stand alone withoutelse. Both forms behave the same. - The annotations do not enforce anything.
n: intand-> intare hints for readers and tools, and Python does not check them at run time. A float works too:negative(2.5)returns-2.5.
Other ways to write it
The built-in abs() returns the absolute value, the number without its sign. Negating that always gives a non-positive result:
print(-abs(4)) # -4
print(-abs(-4)) # -4
print(-abs(0)) # 0
print(min(4, -4)) # -4
So return -abs(n) solves the whole problem in one line, and so does return min(n, -n). The lesson uses if and else because it practises conditional statements, and the branches make the two cases visible.
Complexity
Time: O(1) and space: O(1). The method does one comparison and at most one negation.
Edge cases and common mistakes
- Zero.
negative(0)returns0. Putting zero in theelsebranch is fine, because-0equals0for integers. - Floating-point zero. For a float,
-0.0is a separate value that compares equal to0.0and prints as-0.0.negative(0.0)returns-0.0. - Always returning
-n. That turns-4into4. - Returning
nfor positive numbers. Mixing up the branches leaves positive inputs unchanged. - Using
<=.n <= 0also works, since zero returns zero either way, but the lesson'sn < 0is the cleaner statement of "already negative". - Negating text. A string such as
"5"has no unary minus, and-"5"raisesTypeError. Convert it first with-int("5"). - Operator precedence with powers.
-2 ** 2is-4, because**binds tighter than the unary minus, while(-2) ** 2is4.
Testing
negative(4)returns-4.negative(15)returns-15.negative(-4)returns-4.negative(0)returns0.
