Disarmium Number Challenge in Python

Annika Noren
Nov 3, 2020

Refactoring my function down from 9 lines to 1 line

Woman jumping for joy
Photo by Konstantin Planinski on Unsplash

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.

First code attempt

Here are my examples:

x = 89
y = 88
z = 131
u = 175
q = 123658

And the results:

results of code

Then I decided to work in the enumerate method (which I had written about previously):

Second code attempt and use enumerate

I continued to refactor and rework the code and 5 versions later, came up with a one-line function:

A one-liner!

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.

--

--