Problem

Description

You are given a number and have to make it negative. However, if the number is already negative (or is 0), nothing needs to occur.

Test Cases

make_negative(1);  # return -1
make_negative(-5); # return -5
make_negative(0);  # return 0

Solution

My Solution

def make_negative( number ):
    return (number if number < 0 else -number)

Other Solutions

def make_negative( number ):
    return -abs(number)

Learning Experiences

  • Solution 1 uses the abs function, which does the same as the |x| in mathematics; returning the (positive) absolute value regardless of existing sign.
  • It then applies the negative to it. Python recognises that -0 is still 0, so it works.