954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reverse an Integer

Now I am looking for the best way to reverse the digits in an integer. For instance x = 12345 should become y = 54321.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

When I first looked at this problem, I thought it would be very simple routine stuff. To my surprise this thing is loaded with programming possibilities.
[php]# reverse integer digit by digit, the long way ...
x = 12345
x_string = str(x) # "12345"
x_list = list(x_string) # ['1', '2', '3', '4', '5']
# uses reversed on the list
x_reversedlist = reversed(x_list) # ['5', '4', '3', '2', '1']
x_reversedstring = "".join(x_reversedlist) # "54321"
x_reversed = int(x_reversedstring) # 54321
print x_reversed
[/php]Why use the list as a middleman when you can call reversed() on the string?
[php]# reverse integer digit by digit, slightly modified ...
x = 12345
x_string = str(x) # "12345"
# uses reversed() directly on the string
x_reversedobject = reversed(x_string) #
x_reversedlist = list(x_reversedobject) # ['5', '4', '3', '2', '1']
x_reversedstring = "".join(x_reversedlist) # "54321"
x_reversed = int(x_reversedstring) # 54321
print x_reversed
[/php]That looks like the setup for a one-liner:
[php]# reverse integer digit by digit, as a one liner ...
x = 12345
x_reversed = int("".join(list(reversed(str(x)))))
print "%d reversed --> %d" % (x, x_reversed) # 12345 reversed --> 54321
[/php]String slicing should cut that really short:
[php]# reverse integer digit by digit, simpler with slicing ...
x = 12345
x_reversed = int(str(x)[::-1])
print "%d reversed --> %d" % (x, x_reversed) # 12345 reversed --> 54321
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thank you! I am still trying to digest the one liners, particularly:

x_reversed = int(str(x)[::-1])


Can anybody break that down for me so I can see the flow?

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Thank you! I am still trying to digest the one liners, particularly:

x_reversed = int(str(x)[::-1])
Can anybody break that down for me so I can see the flow?


[php]x = 12345
x_string = str(x) # "12345"
x_reversedstring = x_string[::-1] # "54321"
x_reversed = int(x_reversedstring) # 54321
[/php]

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

Could someone walk through how it does this:

[php]
x_string[::-1]
[/php]

LaMouche
Posting Whiz in Training
269 posts since Oct 2006
Reputation Points: 83
Solved Threads: 39
 

You need to fill in the defaults to get a better feeling for slicing:
[php]
str1 = "madam"
begin = 0 # default is 0
end = len(str1) # end is exclusive, default is length of string
step = -1 # - sign --> from the end in steps of 1, default is 1
reversed_string = str1[begin:end:step]
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
int(str(x)[::-1])


What the ... I never knew you could do that!!!

LOL!

Jeff

Testing results:
[php]
>>> s = "my test string"
>>> s[::-1]
'gnirts tset ym'
>>> s[::2]
'm etsrn'
>>> s[::-2]
'git sty'
>>>
[/php]
:cool:

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

Its all in the docs.
Here it is:

http://docs.python.org/ref/types.html
under the heading "Sequences"

ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You