-
Notifications
You must be signed in to change notification settings - Fork 12
Add armstrong number checker by amandapanda00. #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amandapanda00
wants to merge
2
commits into
NeuroByte-Society:main
Choose a base branch
from
amandapanda00:amandapanda00/armstrong-number-checker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
week1_projects/amandapanda00/armstrong_number_checker/README.md
This file contains hidden or 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,84 @@ | ||
| Amandapanda00 – Armstrong Number Checker | ||
| What the project does: | ||
| This project determines whether a given integer is an Armstrong number—a number equal to the sum of the cubes of its digits equals the number itself (153 = 1³ + 5³ + 3³ = 153). | ||
|
|
||
| How to run it: | ||
| 1. Make sure Python is installed on your computer. | ||
| 2. Clone or download this repository. | ||
| 3. Open the project in an environment that supports Python such as VS code or PyCharm. | ||
| 4. Run the `main.py` file. | ||
| 5. When prompted, choose an input method: | ||
|
|
||
| * Keyboard input: Enter a number directly. | ||
|
|
||
| * File input: Enter a filename to check numbers from a file (e.g., numbers.txt). | ||
| The program will read each line and check if it is an Armstrong number. | ||
| Invalid lines are skipped with a message for the user. | ||
|
|
||
| Example input/output | ||
|
|
||
| Example Input: | ||
|
|
||
| 153 | ||
|
|
||
| Expected Output: | ||
|
|
||
| 153 is an Armstrong number | ||
|
|
||
| Another Example: | ||
|
|
||
| numbers.txt file | ||
|
|
||
| Line 1: abc: Raises a ValueError when converting to int. The program prints: | ||
| Invalid entry on line 1: 'abc' | ||
|
|
||
| Line 2: 123 = Successfully converted to an integer. Checked by is_armstrong(). Output: | ||
| 123 is not an Armstrong number. | ||
|
|
||
| Line 3: 153 = Successfully converted to an integer. Checked by is_armstrong(). Output: | ||
| 153 is an Armstrong number. | ||
|
|
||
| Line 4: 370 = Successfully converted to an integer. Checked by is_armstrong(). Output: | ||
| 370 is an Armstrong number. | ||
|
|
||
| Line 5: 456 = Successfully converted to an integer. Checked by is_armstrong(). Output: | ||
| 456 is not an Armstrong number. | ||
|
|
||
| Line 6: xyz Raises a ValueError. Output: | ||
| Invalid entry on line 6: 'xyz' | ||
|
|
||
| Line 7: 9474 = Successfully converted to an integer. Checked by is_armstrong(). Output: | ||
| 9474 is an Armstrong number. | ||
|
|
||
| This file demonstrates: | ||
|
|
||
| * Try/except blocks catching invalid data (abc and xyz). | ||
|
|
||
| * Data integrity: only valid numbers are processed. File "r" read only. | ||
|
|
||
| * File handling: reads multiple lines from a file. | ||
|
|
||
|
|
||
| Error Handling | ||
|
|
||
| try/except blocks are used to handle invalid input/value errors. | ||
|
|
||
| * This ensures the program is not going to crash if a user enters a non-integer value. | ||
|
|
||
| * When reading a file, invalid entries are skipped, and the line number is reported to help track and identify errors. | ||
|
|
||
| * File handling is done with "r" mode (read-only) to protect the file and prevent accidental changes. | ||
|
|
||
|
|
||
| Testing | ||
|
|
||
| * PyCharm Debugger: Used to step through code and verify logic. | ||
|
|
||
| * Pytest: Automatically tests the functionality of the Armstrong Number Checker, including both keyboard input and file input. Ensures that the program behaves correctly for a variety of cases. | ||
|
|
||
| * Flake8: Checks code against Python style guidelines (PEP 8). | ||
|
|
||
| * Pydocstyle: Checks that documentation is complete and clear, ensuring maintainability. | ||
|
|
||
|
|
||
|
|
100 changes: 100 additions & 0 deletions
100
week1_projects/amandapanda00/armstrong_number_checker/main.py
This file contains hidden or 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,100 @@ | ||
| # Armstrong Number Checker | ||
| # Author: amandapanda00 | ||
| # Date: 11/04/2025 | ||
|
|
||
| """ | ||
| This program checks whether a given integer is an Armstrong number. | ||
|
|
||
| An Armstrong number is equal to the sum of the cubes of its digits. | ||
| """ | ||
|
|
||
|
|
||
| def is_armstrong(number): | ||
| """ | ||
| Determine whether a given integer is an Armstrong number. | ||
|
|
||
| Parameter: | ||
| number (int): The integer to be tested. | ||
|
|
||
| Returns: | ||
| bool: True if the number is an Armstrong number, False otherwise. | ||
| """ | ||
| digits = [int(d) for d in str(number)] | ||
| n = len(digits) # number of digits | ||
| # og had ** 3 | ||
| # change to n to accommodate armstrong numbers more than 3 digits | ||
| total = sum(d ** n for d in digits) | ||
| return total == number | ||
|
|
||
|
|
||
| def process_result(number): | ||
| """Print the result.""" | ||
| if is_armstrong(number): | ||
| print(f"{number} is an Armstrong number.") | ||
| else: | ||
| print(f"{number} is not an Armstrong number.") | ||
|
|
||
|
|
||
| def process_file(filename="numbers.txt"): | ||
| """ | ||
| Read multiple lines from a file and check each one. | ||
|
|
||
| See if it is an Armstrong number. | ||
| """ | ||
| try: | ||
| with open(filename, "r") as file: | ||
| # read only to keep integrity of data | ||
| for line_num, line in enumerate(file, start=1): | ||
| # loop through data in file | ||
| # number each line in file starting with 1 | ||
| # keep track of bad data | ||
| line = line.strip() | ||
| # removes newline ect. cleans line | ||
|
|
||
| # Only process the line if it is not empty | ||
| if line: | ||
| try: | ||
| number = int(line) | ||
| process_result(number) | ||
| except ValueError: | ||
| print(f"Invalid entry on line {line_num}: '{line}'") | ||
| # If the line is empty do nothing | ||
| # print error for user clarity | ||
|
|
||
| except FileNotFoundError: | ||
| print(f"Error: File '{filename}' not found.") | ||
| except PermissionError: | ||
| print(f"Error: Not allowed to read '{filename}'.") | ||
|
|
||
|
|
||
| def main(): | ||
| """Run main.""" | ||
| print("Choose input method:") | ||
| print("1. Enter a number using the keyboard") | ||
| print("2. Read a number from a file") | ||
| # print user menu | ||
|
|
||
| choice = input("Enter choice (1 or 2): ").strip() | ||
| # get user choice | ||
| # strip cleans input-user errors, extra spaces | ||
|
|
||
| if choice == "1": | ||
| try: | ||
| num = int(input("Enter a number: ")) | ||
| # string to int | ||
| process_result(num) | ||
| # call function | ||
| except ValueError: | ||
| print("Invalid input. Please enter a valid integer.") | ||
| # value error to fail gracefully. abc != 123 | ||
|
|
||
| elif choice == "2": | ||
| filename = input("Enter filename: ").strip() | ||
| process_file(filename) | ||
|
|
||
| else: | ||
| print("Invalid choice.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
7 changes: 7 additions & 0 deletions
7
week1_projects/amandapanda00/armstrong_number_checker/numbers.txt
This file contains hidden or 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,7 @@ | ||
| abc | ||
| 123 | ||
| 153 | ||
| 370 | ||
| 456 | ||
| xyz | ||
| 9474 |
15 changes: 15 additions & 0 deletions
15
week1_projects/amandapanda00/armstrong_number_checker/test_main.py
This file contains hidden or 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,15 @@ | ||
| from main import is_armstrong | ||
|
|
||
|
|
||
| def test_3_digit_armstrong(): | ||
| assert is_armstrong(153) | ||
| assert not is_armstrong(123) | ||
|
|
||
|
|
||
| def test_4_digit_armstrong(): | ||
| assert is_armstrong(9474) | ||
| assert not is_armstrong(9475) | ||
|
|
||
|
|
||
| def test_1_digit_armstrong(): | ||
| assert is_armstrong(5) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @amandapanda00 — thanks for the contribution! I reviewed week1_projects/amandapanda00/armstrong_number_checker/main.py. Please make the following changes to improve correctness, robustness, and maintainability:
Suggested change:
def is_armstrong(number: int) -> bool:
if number < 0:
return False
digits = [int(d) for d in str(number)]
n = len(digits)
return sum(d ** n for d in digits) == number