Skip to content

Commit 626c456

Browse files
committed
Add type hints and doctests for is_disarium function
1 parent ccbd0d2 commit 626c456

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
def is_disarium(num):
1+
def is_disarium(num: int) -> bool:
22
"""
33
Check if a number is a Disarium number.
4+
45
A Disarium number is a number in which the sum of its digits
56
powered with their respective positions is equal to the number itself.
67
7-
Example:
8-
135 -> 1¹ + 3² + 5³ = 135 ✅
8+
Args:
9+
num (int): The number to check.
10+
11+
Returns:
12+
bool: True if num is a Disarium number, False otherwise.
13+
14+
Examples:
15+
>>> is_disarium(135)
16+
True
17+
>>> is_disarium(89)
18+
True
19+
>>> is_disarium(75)
20+
False
21+
>>> is_disarium(9)
22+
True
923
"""
1024
digits = str(num)
1125
total = 0
@@ -16,9 +30,6 @@ def is_disarium(num):
1630
position += 1
1731

1832
return total == num
19-
20-
2133
if __name__ == "__main__":
22-
test_numbers = [89, 135, 175, 518, 9, 10]
23-
for n in test_numbers:
24-
print(f"{n}{'Disarium' if is_disarium(n) else 'Not Disarium'}")
34+
import doctest
35+
doctest.testmod()

0 commit comments

Comments
 (0)