Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import ceil, log
- ''' type checks since dealing with python '''
- def revere_digits_forward(n):
- assert isinstance(n, int)
- sign = 1 if n >= 0 else -1
- n = abs(n)
- res = 0
- length = num_digits(n)
- for i in range(length):
- shift = length -1 - i
- first_digit = n // 10 ** shift
- res += first_digit * 10 ** i
- n %= 10 ** shift
- return sign * res
- def num_digits(n):
- assert isinstance(n, int) and n >= 0
- # log(1) -> 0,
- # log(0) -> -inf
- return ceil(log(n) / log(10)) if n > 1 else 1
- tests = [0, 1, 12345, 54321, -12345, -1]
- for t in tests:
- print(t, revere_digits_forward(t))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement