Skip to content

Comments

Add armstrong number checker by amandapanda00.#22

Open
amandapanda00 wants to merge 2 commits intoNeuroByte-Society:mainfrom
amandapanda00:amandapanda00/armstrong-number-checker
Open

Add armstrong number checker by amandapanda00.#22
amandapanda00 wants to merge 2 commits intoNeuroByte-Society:mainfrom
amandapanda00:amandapanda00/armstrong-number-checker

Conversation

@amandapanda00
Copy link

main.py, test_main.py, numbers.txt, readme.md

 main.py, test_main.py, numbers.txt, readme.md
Copy link
Contributor

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:

  1. Fix module docstring
  • Change "sum of the cubes of its digits" to the general definition: an Armstrong number equals the sum of each digit raised to the power of the number of digits.
  1. Handle negative input
  • Reject negative integers (return False) or explicitly document that only non-negative integers are supported. Current code will crash on negative input because of the '-' character in str(number).

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

  1. Improve file handling
  • Open files with explicit encoding (encoding="utf-8").
  • Prefer pathlib.Path for clearer checks (exists/is_file).
  • Keep existing FileNotFoundError/PermissionError handling but check path first to provide clearer messages.
  1. Make functions more testable
  • Keep is_armstrong pure (it already is).
  • Have process_result return the formatted string instead of printing directly; main() can print it. This makes unit testing easier.
  1. Input robustness and UX
  • Catch KeyboardInterrupt and EOFError in main to exit gracefully.
  • Validate filename input (strip and ignore empty input).
  • Consider rejecting lines that contain non-digit characters (or handle leading '+') and report which lines are invalid.
  1. Clean up comments and add type hints
  • Remove noisy inline comments like "# og had ** 3".
  • Add type hints for public functions (e.g., process_file(filename: str) -> None).
  • Improve docstrings (PEP 257 style).
  1. Tests and CI
  • Add unit tests (pytest or unittest) covering:
    • Typical Armstrong numbers: 153, 370, 9474
    • Non-Armstrong numbers
    • 0
    • Negative values
    • File input with invalid lines
  • Consider adding a simple GitHub Actions workflow to run tests.
  1. Optional improvements (nice-to-have)
  • Use argparse for CLI options (single number vs filename).
  • Limit maximum allowed digits/value to avoid CPU/memory DoS from huge inputs and report if input is too large.

updated main, testing, and read me
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants