Last active
February 13, 2019 16:09
-
-
Save RafaelBroseghini/d5b1328c71d2b0a88565f727634e9564 to your computer and use it in GitHub Desktop.
Reverse an integer without casting as a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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