Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second - Iterative Solution solution in Clear category for Reverse Integer by freeman_lex
def reverse_digits(num: int) -> int:
reversed_num, absolute_num = 0, abs(num)
while absolute_num:
reversed_num = reversed_num * 10 + absolute_num % 10
absolute_num //= 10
return reversed_num if num >= 0 else -reversed_num
print("Example:")
print(reverse_digits(32))
# These "asserts" are used for self-checking
assert reverse_digits(1234) == 4321
assert reverse_digits(0) == 0
assert reverse_digits(-123) == -321
assert reverse_digits(5) == 5
assert reverse_digits(-9) == -9
assert reverse_digits(100) == 1
assert reverse_digits(-100) == -1
assert reverse_digits(54321) == 12345
assert reverse_digits(1111) == 1111
assert reverse_digits(987654321) == 123456789
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 23, 2023