Skip to content

Instantly share code, notes, and snippets.

@LarynQi
Last active June 29, 2020 06:14
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 LarynQi/66efe0ec97acc92cf9df75ea87bacf5a to your computer and use it in GitHub Desktop.
Save LarynQi/66efe0ec97acc92cf9df75ea87bacf5a to your computer and use it in GitHub Desktop.
CS61A SU20 Quiz 1.4 Solution
import doctest
# To run this solution, run the following command:
# python3 quizsol1.4.py -v
def reverse_digits(n):
"""Given a positive integer n, reverse its digits.
Do not use strings.
>>> d1 = 12345
>>> reverse_digits(d1)
54321
>>> d2 = 101
>>> reverse_digits(d2)
101
>>> d3 = 100
>>> reverse_digits(d3)
1
"""
result = 0
while n > 0:
result = 10 * result + n % 10
n //= 10
return result
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment