Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Reverse Integer by Alex_sashaemelyanov09
def reverse_digits(num: int) -> int:
if len(str(num)) == 1:
return num
if "-" == str(num)[0]:
num_list = list(str(num)[1:])
num_list.reverse()
return int("-" + "".join(num_list))
num_list = list(str(num))
num_list.reverse()
return int("".join(num_list))
print("Example:")
print(reverse_digits(1234))
# 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!")
Oct. 10, 2023
Comments: