Subtract the Product and Sum of Digits of an Integer
Real-world version: Crate Serial ChecksumThe statement here is our own: the same inputs, outputs and technique.
Every crate leaving the yard is stamped with a serial number serial, a positive whole number written in base ten with no leading zeros.
The yard's clerk works out a checksum for the crate by multiplying all the decimal digits of serial together, adding all those same digits up, and then subtracting the second tally from the first.
Return the checksum. It may be negative.
Examples
Example 1
- Input
- serial = 705
- Output
- -12
The digits are 7, 0 and 5. Their product is 0 and their sum is 12, so the checksum is 0 - 12.
Example 2
- Input
- serial = 48
- Output
- 20
The digits are 4 and 8. Their product is 32 and their sum is 12, so the checksum is 32 - 12.
Example 3
- Input
- serial = 9
- Output
- 0
A one-digit serial has a product of 9 and a sum of 9, so the checksum is 9 - 9.
Constraints
- 1 <= serial <= 10^5
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 digit_checksum(serial: int) -> int:public int digitChecksum(int serial)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 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