Skip to content
Merged
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions maths/signum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,31 @@ def signum(num: float) -> int:
"""
Applies signum function on the number

>>> signum(-10)
Comment thread
cclauss marked this conversation as resolved.
Custom test cases:
>>> signum(-20)
-1
>>> signum(10)
Comment thread
cclauss marked this conversation as resolved.
>>> signum(20)
1
>>> signum(0)
0
>>> signum("a")
0
Comment thread
cclauss marked this conversation as resolved.
Outdated
>>> signum([])
0
Comment thread
cclauss marked this conversation as resolved.
Outdated
>>> signum(-10)
-1
>>> signum(10)
1
"""
if num < 0:
return -1
return 1 if num else 0
if isinstance(num, (int, float)):
if num < 0:
return -1
elif num > 0:
return 1
else:
return 0
else:
return 0
Comment thread
cclauss marked this conversation as resolved.
Outdated


def test_signum() -> None:
Expand All @@ -26,6 +41,14 @@ def test_signum() -> None:
assert signum(5) == 1
assert signum(-5) == -1
assert signum(0) == 0
assert signum(10.5) == 1
assert signum(-10.5) == -1
assert signum(1e-6) == 1
assert signum(-1e-6) == -1
assert signum(123456789) == 1
assert signum(-123456789) == -1
Comment thread
cclauss marked this conversation as resolved.
assert signum("hello") == 0
assert signum([]) == 0


if __name__ == "__main__":
Expand Down