Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. from math import ceil, log
  2.  
  3. ''' type checks since dealing with python '''
  4.  
  5. def revere_digits_forward(n):
  6. assert isinstance(n, int)
  7. sign = 1 if n >= 0 else -1
  8. n = abs(n)
  9. res = 0
  10. length = num_digits(n)
  11. for i in range(length):
  12. shift = length -1 - i
  13. first_digit = n // 10 ** shift
  14. res += first_digit * 10 ** i
  15. n %= 10 ** shift
  16. return sign * res
  17.  
  18. def num_digits(n):
  19. assert isinstance(n, int) and n >= 0
  20. # log(1) -> 0,
  21. # log(0) -> -inf
  22. return ceil(log(n) / log(10)) if n > 1 else 1
  23.  
  24.  
  25. tests = [0, 1, 12345, 54321, -12345, -1]
  26. for t in tests:
  27. print(t, revere_digits_forward(t))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement