Reverse Integer
Real-world version: Legacy Register FlipThe statement here is our own: the same inputs, outputs and technique.
An old flow meter transmits its counter as reading, a signed whole number. The meter's firmware was wired back to front, so the decimal digits arrive in the opposite order to the one intended, and the decoder has to undo that: keep the sign as it is and read the digits the other way round. Any zeros that end up in front simply vanish, since the decoded counter is a number rather than a run of characters, so a reading of 4100 decodes to 14.
The decoder is a small board with one signed 32-bit register, so it can only ever hold a value from -2^31 through 2^31 - 1. Nothing wider is available: no 64-bit slot, no unbounded arithmetic, and no scratch buffer of characters. If the decoded counter would fall outside what the register holds, the decoder gives up and reports 0. Return the decoded counter.
Examples
Example 1
- Input
- reading = 8104
- Output
- 4018
Reading the digits 8, 1, 0, 4 the other way round gives 4, 0, 1, 8.
Example 2
- Input
- reading = -3570
- Output
- -753
The digits flip to 0, 7, 5, 3 and the leading zero drops away, while the sign rides along unchanged.
Example 3
- Input
- reading = 1999999999
- Output
- 0
The flipped digits spell a counter above 2^31 - 1, which the register cannot hold, so the decoder reports failure.
Constraints
- -2^31 <= reading <= 2^31 - 1
- No value outside the signed 32-bit range may be held at any point, so overflow has to be ruled out before it happens
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 flip_register(reading: int) -> int:public int flipRegister(int reading)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 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