From 0df4ef4db9aac6e7082059650c4b7ff136032300 Mon Sep 17 00:00:00 2001 From: Joy Khandelwal Date: Fri, 27 Dec 2024 22:43:24 +0530 Subject: [PATCH 1/4] Add additional doctests for the perfect number algorithm and fix grammatical errors. Contributes to #9943 --- maths/perfect_number.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index df6b6e3d91d8..27d5193cffb5 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -46,17 +46,27 @@ def perfect(number: int) -> bool: False >>> perfect(-1) False + >>> perfect(33550336) # Large perfect number + True + >>> perfect(33550337) # Just above a large perfect number + False + >>> perfect(1) # Edge case: 1 is not a perfect number + False + >>> perfect("123") # String representation of a number + Traceback (most recent call last): + ... + ValueError: number must be an integer >>> perfect(12.34) Traceback (most recent call last): ... - ValueError: number must an integer + ValueError: number must be an integer >>> perfect("Hello") Traceback (most recent call last): ... - ValueError: number must an integer + ValueError: number must be an integer """ if not isinstance(number, int): - raise ValueError("number must an integer") + raise ValueError("number must be an integer") if number <= 0: return False return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number @@ -70,8 +80,8 @@ def perfect(number: int) -> bool: try: number = int(input("Enter a positive integer: ").strip()) except ValueError: - msg = "number must an integer" + msg = "number must be an integer" print(msg) raise ValueError(msg) - print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") + print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") \ No newline at end of file From 662ffb0f4a97b21f512ae7b08e586497ae016c8c Mon Sep 17 00:00:00 2001 From: Joy Khandelwal Date: Fri, 27 Dec 2024 23:03:16 +0530 Subject: [PATCH 2/4] Added newline at End of file --- maths/perfect_number.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index 27d5193cffb5..8cdb157a05fd 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -84,4 +84,5 @@ def perfect(number: int) -> bool: print(msg) raise ValueError(msg) - print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") \ No newline at end of file + print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") + \ No newline at end of file From c9c63a8ea405725f2332e21822aa462c650a2207 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Dec 2024 17:48:21 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/perfect_number.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index 8cdb157a05fd..875a1ca3ac9f 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -85,4 +85,3 @@ def perfect(number: int) -> bool: raise ValueError(msg) print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") - \ No newline at end of file From 58b6759d64a84627d7f60b38b3f4cb0ffb4fc898 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sat, 28 Dec 2024 00:48:25 -0800 Subject: [PATCH 4/4] Apply suggestions from code review --- maths/perfect_number.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index 875a1ca3ac9f..52c816cc7895 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -81,7 +81,6 @@ def perfect(number: int) -> bool: number = int(input("Enter a positive integer: ").strip()) except ValueError: msg = "number must be an integer" - print(msg) raise ValueError(msg) print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")