forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e906db8
commit 08bf491
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for identifying perfect numbers in a given range | ||
Created on 06 Jan 2025 | ||
@author: Arthur (Mr-Glucose) | ||
""" | ||
|
||
|
||
def get_divisors(n): | ||
"""Checks if a list of divisors of n, excluding n itself""" | ||
if n <= 0: | ||
raise ValueError("Input must be a positive integer greater than 0") | ||
|
||
return [i for i in range(1, n) if n % i == 0] | ||
|
||
|
||
def is_perfect_number(n): | ||
""" | ||
Checks if a given number is perfect. | ||
""" | ||
if n <= 0: | ||
raise ValueError("Input must be a positive integer greater than 0") | ||
|
||
return sum(get_divisors(n)) == n | ||
|
||
|
||
def perfect_numbers_in_range(start, end): | ||
""" | ||
Returns all perfect numbers in a specified range. | ||
""" | ||
if start <= 0 or end <= 0: | ||
raise ValueError("Both start and end must be positive integers") | ||
if start > end: | ||
raise ValueError("Start should not be greater than end") | ||
|
||
return [num for num in range(start, end + 1) if is_perfect_number(num)] | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
# Example usage | ||
found_perfect_numbers = perfect_numbers_in_range(1, 1000) | ||
print("Perfect numbers between 1 and 1000:", found_perfect_numbers) | ||
except ValueError as e: | ||
print(f"Error: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for identifying perfect numbers in a given range | ||
Created on 06 Jan 2025 | ||
@author: Arthur (Mr-Glucose) | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.challenge_28_p.perfect_numbers import ( | ||
get_divisors, | ||
is_perfect_number, | ||
perfect_numbers_in_range, | ||
) | ||
|
||
|
||
class TestPerfectNumbers(unittest.TestCase): | ||
""" | ||
Test suite for the Perfect Numbers module. | ||
This class tests the functionality of the functions: | ||
get_divisors, is_perfect_number, and perfect_numbers_in_range. | ||
""" | ||
|
||
def test_get_divisors_valid_input(self): | ||
""" | ||
Tests the get_divisors function with valid input. | ||
Checks that the divisors of a number are correctly returned. | ||
""" | ||
self.assertEqual(get_divisors(6), [1, 2, 3]) | ||
self.assertEqual(get_divisors(28), [1, 2, 4, 7, 14]) | ||
|
||
def test_is_perfect_number(self): | ||
""" | ||
Tests the is_perfect_number function. | ||
Checks whether a number is a perfect number. | ||
""" | ||
self.assertTrue(is_perfect_number(6)) | ||
self.assertTrue(is_perfect_number(28)) | ||
self.assertFalse(is_perfect_number(12)) | ||
|
||
def test_perfect_numbers_in_range(self): | ||
""" | ||
Tests the perfect_numbers_in_range function. | ||
Checks whether the function returns perfect numbers in a given range. | ||
""" | ||
self.assertEqual(perfect_numbers_in_range(1, 100), [6, 28]) | ||
self.assertEqual(perfect_numbers_in_range(1, 1000), [6, 28, 496]) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |