Skip to content

Instantly share code, notes, and snippets.

@RafaelBroseghini
Last active February 13, 2019 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RafaelBroseghini/d5b1328c71d2b0a88565f727634e9564 to your computer and use it in GitHub Desktop.
Save RafaelBroseghini/d5b1328c71d2b0a88565f727634e9564 to your computer and use it in GitHub Desktop.
Reverse an integer without casting as a string
def reverse_int(n: int) -> int:
num, reverse_num, size_of_num = n, 0, -1
copy = num
while copy >= 1:
copy //= 10
size_of_num += 1
while num >= 1:
rem = num % 10
reverse_num += (10 ** (size_of_num) * rem)
size_of_num -= 1
num //= 10
if __name__ == "__main__":
reverse_int(12345)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment