Reverse an Integer

Thread Solved

Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Reverse an Integer

 
0
  #1
Oct 29th, 2006
Now I am looking for the best way to reverse the digits in an integer. For instance x = 12345 should become y = 54321.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,021
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Reverse an Integer

 
0
  #2
Oct 29th, 2006
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Reverse an Integer

 
0
  #3
Oct 30th, 2006
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?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 172
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Reverse an Integer

 
0
  #4
Oct 30th, 2006
Originally Posted by sneekula View Post
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]
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Reverse an Integer

 
0
  #5
Oct 30th, 2006
Could someone walk through how it does this:

[php]
x_string[::-1]
[/php]
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,021
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Reverse an Integer

 
0
  #6
Oct 30th, 2006
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Reverse an Integer

 
0
  #7
Oct 31st, 2006
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]

Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Reverse an Integer

 
0
  #8
Oct 31st, 2006
Its all in the docs.
Here it is:

http://docs.python.org/ref/types.html
under the heading "Sequences"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC