Disarium Number 🟡
Contents
Disarium Number 🟡#
Examples:
Number |
Operation |
Is Disarium? |
---|---|---|
80 |
8^1 + 0^2 = 8 |
False |
89 |
8^1+9^2 = 89 |
True |
135 |
1^1 + 3^2 + 5^3 = 135 |
True |
Write a function that determines whether a number is a Disarium (True) or not (False).
Tip
Combining enumerate()
and List Comprehension
it is possible to solve this problem in just 1 line!
Useful concepts#
Python’s enumerate()
#
Imagine that you have a list of colours, like:
colours = ['blue', 'red', 'green']
You can use a loop structure to iterate over each item. For example, to print every colour:
for colour in colours:
print(colour)
the output:
blue
red
green
Now imagine that for each item you also need print the item position, we could do something like:
i = 1
for colour in colours:
print(colour, i)
i = i + 1
that would result in
blue, 1
red, 2
green, 3
enumerate()
can solve this problem easily: when you use enumerate()
, the function gives you back two loop variables:
The count of the current iteration
The value of the item at the current iteration
Using the example above, we could write:
for i, colour in enumerate(colours, start=1):
print(colour, i)
to produce the output:
blue, 1
red, 2
green, 3