944,161 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 7059
  • Python RSS
Oct 29th, 2006
0

Reverse an Integer

Expand Post »
Now I am looking for the best way to reverse the digits in an integer. For instance x = 12345 should become y = 54321.
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 29th, 2006
0

Re: Reverse an Integer

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) # <reversed object at 0x009D2D70>
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]
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 30th, 2006
0

Re: Reverse an Integer

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?
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 30th, 2006
0

Re: Reverse an Integer

Click to Expand / Collapse  Quote originally posted by sneekula ...
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]
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Oct 30th, 2006
0

Re: Reverse an Integer

Could someone walk through how it does this:

[php]
x_string[::-1]
[/php]
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 30th, 2006
0

Re: Reverse an Integer

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]
Last edited by vegaseat; Oct 30th, 2006 at 11:30 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 31st, 2006
0

Re: Reverse an Integer

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]

Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 31st, 2006
0

Re: Reverse an Integer

Its all in the docs.
Here it is:

http://docs.python.org/ref/types.html
under the heading "Sequences"
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python for Birthday (but having tech difficulties)
Next Thread in Python Forum Timeline: Tkinter weirdness





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC