An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.
9 = 9^1 = 9
10 != 1^2 + 0^2 = 1
153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190
Write some code to determine whether a number is an Armstrong number.
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
raise Exception, you should write:
raise Exception("Meaningful message indicating the source of the error")
To run the tests, run the appropriate command below (why they are different):
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
python -m pytest armstrong_numbers_test.py
-v: enable verbose output
-x: stop running tests on first failure
--ff: run failures from previous test before running other test cases
For other options, see
python -m pytest -h
Note that, when trying to submit an exercise, make sure the solution is in the
You can find your Exercism workspace by running
exercism debug and looking for the line that starts with
For more detailed information about running tests, code style and linting, please see the help page.
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
import unittest from armstrong_numbers import is_armstrong # Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 class ArmstrongNumbersTest(unittest.TestCase): def test_single_digit_numbers_are_armstrong_numbers(self): self.assertIs(is_armstrong(5), True) def test_there_are_no_two_digit_armstrong_numbers(self): self.assertIs(is_armstrong(10), False) def test_three_digit_number_that_is_an_armstrong_number(self): self.assertIs(is_armstrong(153), True) def test_three_digit_number_that_is_not_an_armstrong_number(self): self.assertIs(is_armstrong(100), False) def test_four_digit_number_that_is_an_armstrong_number(self): self.assertIs(is_armstrong(9474), True) def test_four_digit_number_that_is_not_an_armstrong_number(self): self.assertIs(is_armstrong(9475), False) def test_seven_digit_number_that_is_an_armstrong_number(self): self.assertIs(is_armstrong(9926315), True) def test_seven_digit_number_that_is_not_an_armstrong_number(self): self.assertIs(is_armstrong(9926314), False) if __name__ == '__main__': unittest.main()
def is_armstrong(number): digits = list(str(number)) power = len(digits) digit_sum = 0 for num in digits: digit_sum += int(num)**power return digit_sum == number
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.