Disarmium Number Challenge in Python
--
Refactoring my function down from 9 lines to 1 line
Several months ago I wrote about my adventure of coding the Fibonacci number. I love the Fibonacci number — any number puzzle actually — so it was with great joy when I came across the Disarmium number. A number is a Disarmium number if the sum of the digits raised to the power of their respective position in the original number is equal. For example, 35 is not a Disarmium number:
3¹ + 5² equals 28 (3 + 25) and 28 does not equal 35
Another example, 89 is a Disarmium number:
8¹ + 9² = 89 and 89 equals 89
Such fun!
My first attempt at solving the challenge was nine lines long.
Here are my examples:
x = 89
y = 88
z = 131
u = 175
q = 123658
And the results:
Then I decided to work in the enumerate method (which I had written about previously):
I continued to refactor and rework the code and 5 versions later, came up with a one-line function:
The lesson learned on this number problem? Nothing really earth-shattering — I was just having some fun with a new number type and seeing how “Pythonic” could I make my code. Mission achieved.