2

I am trying to write a Python function that get a number as input and returns its reversed number as output. for example: 1234 returns 4321.

this is what I try, but it return only ''

def reverse(num):
  L=[]
  x=str(num)
  L1=list(x)

  for i in L1:
    L.insert(0,i)
  print 'the reversed num is:'

  x=''
  for i in L:
    x.join(i)
    return x

any ideas?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Amir Chazan
  • 173
  • 1
  • 7
  • read about `pythonic way` and `zen of python` (type `import this` in interpreter) – Alexey Astahov Feb 08 '16 at 10:18
  • @AlexeyAstahov that's not very helpful. – timgeb Feb 08 '16 at 10:21
  • @timgeb write 9 lines instead of one line is not very helpful. Python is not C/C++ or smth. Topicstarter must learn how to think more pythonic. And my helpful answer is here - http://stackoverflow.com/a/35267069/5198288 – Alexey Astahov Feb 08 '16 at 10:26
  • "Think more pythonic" is *not* a helpful answer to a specific question. I did not see that you posted a helpful answer as well - sorry 'bout that. – timgeb Feb 08 '16 at 10:30

7 Answers7

3
def reverse(num):
    return str(num)[::-1]

or one line:

lambda x: str(x)[::-1]
Alexey Astahov
  • 203
  • 1
  • 7
2

Well, the easy solution is this one:

>>> int(str(1234)[::-1])
4321

Your code can be fixed by changing the part

for i in L:
    x.join(i)
    return x

to

for i in L:
    x += i
return x

Alternatively, just replace that section by

return ''.join(L)

What was wrong with your code? Because of wrong indentation, you returned in the first iteration of the for loop. You never assigned a name to x.join(i) so the return value was lost. What you expected join to do I do not know.

timgeb
  • 76,762
  • 20
  • 123
  • 145
1

First, there is an easier way by converting to string, slicing the string and converting it back to a number.

def reverse(num):
    return int(str(num)[::-1])

Second, there are multiple errors in your code:

1) your return statement is in the loop, so it will return after the first iteration;

2) x does not change because x.join() creates a new string and does not modify the string x (which is immutable by the way)

3) no need to convert the string into a list since you can directly iterate over the string (for i in x: ...)

4) join() takes an iterator as an argument. No need for the second loop: return ''.join(L)

Julien Spronck
  • 15,069
  • 4
  • 47
  • 55
1

thank you all for the helpful ideas.

here is my solution:

         def reverse(n):
            reverse=0
            while(n>0):
                    dig=n%10
                    reverse=reverse*10
                    reverse=reverse+dig
                    n=n/10
            return reverse
Amir Chazan
  • 173
  • 1
  • 7
0
def reverse(num)
    return str(num)[::-1]

Reverse a string in Python

Community
  • 1
  • 1
Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
0

Other users already gave good answers. Here is a different one, for study purposes.

num = 1234
print "".join(reversed(str(num)))
# 4321

You can convert to int afterwards.

Rockybilly
  • 2,938
  • 1
  • 13
  • 38
0

Try this,

reverse=0
while(n>=1):
    dig=n%10
    reverse=reverse*10
    reverse=reverse+dig
    n=n//10
print(reverse)
Darsh Modi
  • 289
  • 1
  • 9