ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# n0mn0m's solution

## to Armstrong Numbers in the Python Track

Published at Jan 29 2021 · 0 comments
Instructions
Test suite
Solution

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

• 9 is an Armstrong number, because `9 = 9^1 = 9`
• 10 is not an Armstrong number, because `10 != 1^2 + 0^2 = 1`
• 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
• 154 is not an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`

Write some code to determine whether a number is an Armstrong number.

## Exception messages

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")
``````

## Running the tests

To run the tests, run `pytest armstrong_numbers_test.py`

Alternatively, you can tell Python to run the pytest module: `python -m pytest armstrong_numbers_test.py`

### Common `pytest` options

• `-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`

## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `\$EXERCISM_WORKSPACE/python/armstrong-numbers` directory.

You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.

For more detailed information about running tests, code style and linting, please see Running the Tests.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### armstrong_numbers_test.py

``````import unittest

from armstrong_numbers import is_armstrong_number

class ArmstrongNumbersTest(unittest.TestCase):
def test_zero_is_an_armstrong_number(self):
self.assertIs(is_armstrong_number(0), True)

def test_single_digit_numbers_are_armstrong_numbers(self):
self.assertIs(is_armstrong_number(5), True)

def test_there_are_no_2_digit_armstrong_numbers(self):
self.assertIs(is_armstrong_number(10), False)

def test_three_digit_number_that_is_an_armstrong_number(self):
self.assertIs(is_armstrong_number(153), True)

def test_three_digit_number_that_is_not_an_armstrong_number(self):
self.assertIs(is_armstrong_number(100), False)

def test_four_digit_number_that_is_an_armstrong_number(self):
self.assertIs(is_armstrong_number(9474), True)

def test_four_digit_number_that_is_not_an_armstrong_number(self):
self.assertIs(is_armstrong_number(9475), False)

def test_seven_digit_number_that_is_an_armstrong_number(self):
self.assertIs(is_armstrong_number(9926315), True)

def test_seven_digit_number_that_is_not_an_armstrong_number(self):
self.assertIs(is_armstrong_number(9926314), False)

if __name__ == "__main__":
unittest.main()``````
``````def is_armstrong_number(number):
ns = str(number)
armstrong_pow = len(ns)
r = sum([pow(int(base), armstrong_pow) for base in ns])
return r == number``````