-
Notifications
You must be signed in to change notification settings - Fork 1
/
033_Return Negative.py
63 lines (43 loc) · 1.25 KB
/
033_Return Negative.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Codewars Coding Challenge
Return Negative
In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?
Examples
make_negative(1); # return -1
make_negative(-5); # return -5
make_negative(0); # return 0
Notes
The number can be negative already, in which case no change is required.
Zero (0) is not checked for any specific sign. Negative zeros make no mathematical sense.
def make_negative( number ):
pass
https://www.codewars.com/kata/55685cd7ad70877c23000102/train/python
"""
# My Solution
def make_negative( number ):
if number > 0:
return -number
else:
return number
"""
Sample Test
import codewars_test as test
from solution import make_negative
@test.describe("Fixed Tests")
def basic_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
for n, expected in ((42,-42), (-9,-9), (0,0)):
actual = make_negative(n)
message = f"For n = {n}, expected {expected} but got {actual}"
test.assert_equals(actual, expected, message)
"""
"""
Perfect Solution From Codewars
=1=
def make_negative( number ):
return -abs(number)
=2=
def make_negative( number ):
return -number if number>0 else number
"""