This should work:
from math import log10
def rev(num):
if num < 10:
return num
else:
ones = num % 10
rest = num // 10
#print ones, rest, int(log10(rest) + 1), ones * 10 ** int(log10(rest) + 1)
return ones * 10 ** int(log10(rest) + 1) + rev(rest)
print rev(9000), rev(1234), rev(1234567890123456789)
You could also reduce the number of times you call log10 and number of math operations by using a nested recursive function:
def rev(num):
def rec(num, tens):
if num < 10:
return num
else:
return num % 10 * tens + rec(num // 10, tens // 10)
return rec(num, 10 ** int(log10(num)))
Answer from John Gaines Jr. on Stack OverflowVideos
This should work:
from math import log10
def rev(num):
if num < 10:
return num
else:
ones = num % 10
rest = num // 10
#print ones, rest, int(log10(rest) + 1), ones * 10 ** int(log10(rest) + 1)
return ones * 10 ** int(log10(rest) + 1) + rev(rest)
print rev(9000), rev(1234), rev(1234567890123456789)
You could also reduce the number of times you call log10 and number of math operations by using a nested recursive function:
def rev(num):
def rec(num, tens):
if num < 10:
return num
else:
return num % 10 * tens + rec(num // 10, tens // 10)
return rec(num, 10 ** int(log10(num)))
It's running multiple times, but it only returns the left-most digit. It's pretty easy to see that's the case, if you consider under what circumstance the if will be true.
To make this work, you need to add in the digits that you skipped as you pass out of the recursion. The following works by simply tacking the passed digits to the left of the previous result. I'd rather do it without the string conversion, but I couldn't come up with an elegant way to do that...
def reverseDisplay(number):
if number<10:
return number
else:
return int(str(number%10) + str(reverseDisplay(number//10)))
def main():
number=int(input("Enter a number:"))
print(number%10,end='')
print(reverseDisplay(number))
main()
I'm not going to give you the answer, but I'll give some hints. It looks like you don't want to convert it to a string -- this makes it a more interesting problem, but will result in some funky behavior. For example, reverseDisplay(100) = 1.
However, if you don't yet have a good handle on recursion, I would strongly recommend that you convert the input to a string and try to recursively reverse that string. Once you understand how to do that, an arithmetic approach will be much more straightforward.
Your base case is solid. A digit reversed is that same digit.
def reverseDisplay(n):
if n < 10:
return n
last_digit = # ??? 12345 -> 4
other_digits = # ??? You'll use last_digit for this. 12345 -> 1234
return last_digit * 10 ** ??? + reverseDisplay(???)
# ** is the exponent operator. If the last digit is 5, this is going to be 500...
# how many zeroes do we want? why?
If you don't want to use any string operations whatsoever, you might have to write your own function for getting the number of digits in an integer. Why? Where will you use it?
Imagine that you have a string 12345.
reverseDisplay(12345) is really
5 + reverseDisplay(1234) ->
4 + reverseDisplay(123) ->
3 + reverseDisplay(12) ->
2 + reverseDisplay(1) ->
1
Honestly, it might be a terrible idea, but who knows may be it will help:
- Convert it to string.
- Reverse the string using the recursion. Basically take char from the back, append to the front.
- Parse it again.
Not the best performing solution, but a solution...
Otherwise there is gotta be some formula. For instance here: https://math.stackexchange.com/questions/323268/formula-to-reverse-digits