Add Digits
Real-world version: Single Digit Label CheckThe statement here is our own: the same inputs, outputs and technique.
Every parcel that leaves the depot carries a tracking number num, and the printer stamps a one-digit check mark next to it.
The check mark is produced like this: add up the digits of num. If that total is still two digits or longer, add up its digits as well, and keep folding the number that way until only one digit is left. That surviving digit is the check mark.
Given num, return its check mark.
Examples
Example 1
- Input
- num = 4917
- Output
- 3
4 + 9 + 1 + 7 = 21, and 2 + 1 = 3. Three is a single digit, so the check mark is 3.
Example 2
- Input
- num = 99
- Output
- 9
9 + 9 = 18, then 1 + 8 = 9.
Example 3
- Input
- num = 12345
- Output
- 6
The digits total 15, and 1 + 5 = 6.
Example 4
- Input
- num = 2147483647
- Output
- 1
The ten digits total 46, then 4 + 6 = 10, then 1 + 0 = 1. This is the largest tracking number the constraints allow.
Constraints
- 0 <= num <= 2^31 - 1
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def add_digits(num: int) -> int:public int addDigits(int num)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's