From ef2be4c088dad2c8bdc653322abc8836960c461e Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Fri, 25 Nov 2022 10:21:58 +0530 Subject: [PATCH 1/2] feat: Add hexagonal number --- maths/hexagonal_number.py | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 maths/hexagonal_number.py diff --git a/maths/hexagonal_number.py b/maths/hexagonal_number.py new file mode 100644 index 000000000000..567a212d705c --- /dev/null +++ b/maths/hexagonal_number.py @@ -0,0 +1,47 @@ +""" +== Hexagonal Number == +The nth hexagonal number hn is the number of distinct dots +in a pattern of dots consisting of the outlines of regular +hexagons with sides up to n dots, when the hexagons are +overlaid so that they share one vertex. + +https://en.wikipedia.org/wiki/Hexagonal_number +""" + +# Author : Akshay Dubey (https://github.com/itsAkshayDubey) + +def hexagonal(number: int) -> int: + """ + :param number: nth hexagonal number to calculate + :return: the nth hexagonal number + Note: A hexagonal number is only defined for positive integers + >>> hexagonal(4) + 28 + >>> hexagonal(11) + 231 + >>> hexagonal(22) + 946 + >>> hexagonal(0) + Traceback (most recent call last): + ... + ValueError: Input must be a positive integer + >>> hexagonal(-1) + Traceback (most recent call last): + ... + ValueError: Input must be a positive integer + >>> hexagonal(11.0) + Traceback (most recent call last): + ... + TypeError: Input value of [number=11.0] must be an integer + """ + if not isinstance(number, int): + raise TypeError(f"Input value of [number={number}] must be an integer") + if number < 1: + raise ValueError("Input must be a positive integer") + return number*(2*number - 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 1eb128482930002b129d9fd0bc03015b1b690821 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 25 Nov 2022 04:53:56 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/hexagonal_number.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maths/hexagonal_number.py b/maths/hexagonal_number.py index 567a212d705c..28735c638f80 100644 --- a/maths/hexagonal_number.py +++ b/maths/hexagonal_number.py @@ -1,8 +1,8 @@ """ == Hexagonal Number == -The nth hexagonal number hn is the number of distinct dots -in a pattern of dots consisting of the outlines of regular -hexagons with sides up to n dots, when the hexagons are +The nth hexagonal number hn is the number of distinct dots +in a pattern of dots consisting of the outlines of regular +hexagons with sides up to n dots, when the hexagons are overlaid so that they share one vertex. https://en.wikipedia.org/wiki/Hexagonal_number @@ -10,6 +10,7 @@ # Author : Akshay Dubey (https://github.com/itsAkshayDubey) + def hexagonal(number: int) -> int: """ :param number: nth hexagonal number to calculate @@ -38,7 +39,7 @@ def hexagonal(number: int) -> int: raise TypeError(f"Input value of [number={number}] must be an integer") if number < 1: raise ValueError("Input must be a positive integer") - return number*(2*number - 1) + return number * (2 * number - 1) if __name__ == "__main__":