forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsign.py
More file actions
40 lines (32 loc) · 637 Bytes
/
sign.py
File metadata and controls
40 lines (32 loc) · 637 Bytes
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
"""Sign Function."""
def sign(num: float) -> int:
"""
Return the sign of a number: -1 for negative, 0 for zero, 1 for positive.
>>> sign(-5)
-1
>>> sign(0)
0
>>> sign(10)
1
>>> sign(-0.5)
-1
"""
if num > 0:
return 1
elif num < 0:
return -1
return 0
def test_sign() -> None:
"""
>>> test_sign()
"""
assert sign(-5) == -1
assert sign(0) == 0
assert sign(10) == 1
assert sign(-0.001) == -1
assert sign(0.001) == 1
if __name__ == "__main__":
import doctest
doctest.testmod()
test_sign()
print(sign(-5)) # --> -1