From d905e593febc39e87edb84f2ece9fb775f4adb1d Mon Sep 17 00:00:00 2001 From: Arjit Arora <42044030+arjitarora26@users.noreply.github.com> Date: Sun, 23 Oct 2022 02:35:43 +0530 Subject: [PATCH 1/5] Add signum function --- maths/signum.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maths/signum.py diff --git a/maths/signum.py b/maths/signum.py new file mode 100644 index 000000000000..3392e937fb1c --- /dev/null +++ b/maths/signum.py @@ -0,0 +1,39 @@ +""" +Signum function + +Refer - https://en.wikipedia.org/wiki/Sign_function +""" + +def signum(num): + """ + Applies signum function on the number + + >>> signum(-10) + -1 + >>> signum(10) + 1 + >>> signum(0) + 0 + """ + if num < 0: + return -1 + elif num > 0: + return 1 + else: + return 0 + + +def test_signum(): + """ + Tests the signum function + """ + assert(signum(5) == 1) + assert(signum(-5) == -1) + assert(signum(0) == 0) + + +if __name__ == "__main__": + print(signum(12)) + print(signum(-12)) + print(signum(0)) + From ea518f06edb9f68ca5165ad374b1d4cf721b0a62 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Oct 2022 21:07:23 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/signum.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/signum.py b/maths/signum.py index 3392e937fb1c..e09d688880f0 100644 --- a/maths/signum.py +++ b/maths/signum.py @@ -4,6 +4,7 @@ Refer - https://en.wikipedia.org/wiki/Sign_function """ + def signum(num): """ Applies signum function on the number @@ -27,13 +28,12 @@ def test_signum(): """ Tests the signum function """ - assert(signum(5) == 1) - assert(signum(-5) == -1) - assert(signum(0) == 0) + assert signum(5) == 1 + assert signum(-5) == -1 + assert signum(0) == 0 if __name__ == "__main__": print(signum(12)) print(signum(-12)) print(signum(0)) - From 1daa8151c18adfa35fadb90669f30017453c264c Mon Sep 17 00:00:00 2001 From: Arjit Arora <42044030+arjitarora26@users.noreply.github.com> Date: Sun, 23 Oct 2022 02:38:25 +0530 Subject: [PATCH 3/5] Add typehints for functions --- maths/signum.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/maths/signum.py b/maths/signum.py index e09d688880f0..7bfce14bb179 100644 --- a/maths/signum.py +++ b/maths/signum.py @@ -1,11 +1,6 @@ -""" -Signum function +"""Signum function""" -Refer - https://en.wikipedia.org/wiki/Sign_function -""" - - -def signum(num): +def signum(num: float) -> int: """ Applies signum function on the number @@ -24,16 +19,17 @@ def signum(num): return 0 -def test_signum(): +def test_signum() -> None: """ Tests the signum function """ - assert signum(5) == 1 - assert signum(-5) == -1 - assert signum(0) == 0 + assert(signum(5) == 1) + assert(signum(-5) == -1) + assert(signum(0) == 0) if __name__ == "__main__": print(signum(12)) print(signum(-12)) print(signum(0)) + From f60da878271117ffa7be969b1d12e71aaeec9b41 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Oct 2022 21:09:43 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/signum.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/signum.py b/maths/signum.py index 7bfce14bb179..28865b175c46 100644 --- a/maths/signum.py +++ b/maths/signum.py @@ -1,5 +1,6 @@ """Signum function""" + def signum(num: float) -> int: """ Applies signum function on the number @@ -23,13 +24,12 @@ def test_signum() -> None: """ Tests the signum function """ - assert(signum(5) == 1) - assert(signum(-5) == -1) - assert(signum(0) == 0) + assert signum(5) == 1 + assert signum(-5) == -1 + assert signum(0) == 0 if __name__ == "__main__": print(signum(12)) print(signum(-12)) print(signum(0)) - From 3001f6a7138e413425bafcbf34900dcf52add214 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 23 Oct 2022 13:14:37 +0200 Subject: [PATCH 5/5] Update signum.py --- maths/signum.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/maths/signum.py b/maths/signum.py index 28865b175c46..148f931767c1 100644 --- a/maths/signum.py +++ b/maths/signum.py @@ -1,4 +1,6 @@ -"""Signum function""" +""" +Signum function -- https://en.wikipedia.org/wiki/Sign_function +""" def signum(num: float) -> int: @@ -14,10 +16,7 @@ def signum(num: float) -> int: """ if num < 0: return -1 - elif num > 0: - return 1 - else: - return 0 + return 1 if num else 0 def test_signum() -> None: