 # rootulp's solution

## to Armstrong Numbers in the Python Track

Published at Nov 30 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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 the appropriate command below (why they are different):

• Python 2.7: `py.test armstrong_numbers_test.py`
• Python 3.4+: `pytest armstrong_numbers_test.py`

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`

### 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

# 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 get_digits(number):
return list(map(int, list(str(number))))

def is_armstrong(number):
digits = get_digits(number)
num_digits = len(digits)
digits_raised_to_num_digits = [digit ** num_digits for digit in digits]

return number == sum(digits_raised_to_num_digits)``````