Skip to content

Commit 2cb6282

Browse files
committed
Finished working on the Armstrong Numbers challenge
1 parent f527dc6 commit 2cb6282

6 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"python","exercise":"armstrong-numbers","id":"81eb97c25c094a65af41366d4f73d30b","url":"https://exercism.io/my/solutions/81eb97c25c094a65af41366d4f73d30b","handle":"sinanata","is_requester":true,"auto_approve":false}

armstrong-numbers/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Armstrong Numbers
2+
3+
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits.
4+
5+
For example:
6+
7+
- 9 is an Armstrong number, because `9 = 9^1 = 9`
8+
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1`
9+
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
10+
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
11+
12+
Write some code to determine whether a number is an Armstrong number.
13+
14+
15+
## Exception messages
16+
17+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
18+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
19+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
20+
a message.
21+
22+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
23+
`raise Exception`, you should write:
24+
25+
```python
26+
raise Exception("Meaningful message indicating the source of the error")
27+
```
28+
29+
## Running the tests
30+
31+
To run the tests, run `pytest armstrong_numbers_test.py`
32+
33+
Alternatively, you can tell Python to run the pytest module:
34+
`python -m pytest armstrong_numbers_test.py`
35+
36+
### Common `pytest` options
37+
38+
- `-v` : enable verbose output
39+
- `-x` : stop running tests on first failure
40+
- `--ff` : run failures from previous test before running other test cases
41+
42+
For other options, see `python -m pytest -h`
43+
44+
## Submitting Exercises
45+
46+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/armstrong-numbers` directory.
47+
48+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
49+
50+
For more detailed information about running tests, code style and linting,
51+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
52+
53+
## Source
54+
55+
Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)
56+
57+
## Submitting Incomplete Solutions
58+
59+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Binary file not shown.
Binary file not shown.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def is_armstrong_number(number):
2+
3+
order = len(str(number))
4+
5+
sum = 0
6+
temp = number
7+
8+
while temp > 0:
9+
digit = temp % 10
10+
sum += digit ** order
11+
temp //= 10
12+
13+
if number == sum:
14+
return True
15+
else:
16+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
3+
from armstrong_numbers import is_armstrong_number
4+
5+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
6+
7+
8+
class ArmstrongNumbersTest(unittest.TestCase):
9+
def test_zero_is_an_armstrong_number(self):
10+
self.assertIs(is_armstrong_number(0), True)
11+
12+
def test_single_digit_numbers_are_armstrong_numbers(self):
13+
self.assertIs(is_armstrong_number(5), True)
14+
15+
def test_there_are_no_2_digit_armstrong_numbers(self):
16+
self.assertIs(is_armstrong_number(10), False)
17+
18+
def test_three_digit_number_that_is_an_armstrong_number(self):
19+
self.assertIs(is_armstrong_number(153), True)
20+
21+
def test_three_digit_number_that_is_not_an_armstrong_number(self):
22+
self.assertIs(is_armstrong_number(100), False)
23+
24+
def test_four_digit_number_that_is_an_armstrong_number(self):
25+
self.assertIs(is_armstrong_number(9474), True)
26+
27+
def test_four_digit_number_that_is_not_an_armstrong_number(self):
28+
self.assertIs(is_armstrong_number(9475), False)
29+
30+
def test_seven_digit_number_that_is_an_armstrong_number(self):
31+
self.assertIs(is_armstrong_number(9926315), True)
32+
33+
def test_seven_digit_number_that_is_not_an_armstrong_number(self):
34+
self.assertIs(is_armstrong_number(9926314), False)
35+
36+
37+
if __name__ == "__main__":
38+
unittest.main()

0 commit comments

Comments
 (0)