-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path06-sum-of-digits.py
More file actions
36 lines (30 loc) · 1 KB
/
06-sum-of-digits.py
File metadata and controls
36 lines (30 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Challenge: Sum of Digits
Difficulty: Beginner
Concepts: integers, modulo, floor division, loops
Time: 10 minutes
Given a non-negative integer, return the sum of its digits.
Do not convert the integer to a string -- use arithmetic only.
Examples:
>>> sum_of_digits(123)
6
>>> sum_of_digits(9999)
36
"""
def sum_of_digits(n: int) -> int:
"""Sum all digits of a non-negative integer using arithmetic. Implement this function."""
# Hint: Use n % 10 to get the last digit and n // 10 to remove it.
pass
# --- Tests (do not modify) ---
if __name__ == "__main__":
# Test 1: Basic number
assert sum_of_digits(123) == 6, "Basic number failed"
# Test 2: All nines
assert sum_of_digits(9999) == 36, "All nines failed"
# Test 3: Zero
assert sum_of_digits(0) == 0, "Zero failed"
# Test 4: Single digit
assert sum_of_digits(7) == 7, "Single digit failed"
# Test 5: Large number
assert sum_of_digits(10001) == 2, "Large number failed"
print("All tests passed!")