Mailing List Archive

Reversing Long integers
I am attempting to write an efficient Python program which can add an integer to
its reverse. This is quite easy to do in Lisp and Mathematica but (mostly out of
curiosity) I wanted to see how one might do this in Python. Converting an
integer to a string and reversing it and converting back is quite easy (although
not all of these ops are primitives) but the fact that Long integers have the
letter L appended means that the loop must include moving the L to the end of
the reversed string prior to summing. Can anyone think of a particularly clever
way to do this?
Reversing Long integers [ In reply to ]
Ira H. Fuchs wrote:
> Can anyone think of a particularly clever way to [add a [long]
> integer to its reverse]

Nothing cleverer than

import string
def add_reverse(n):
x = map(None, str(long(n))[:-1])
x.reverse()
return n + long(string.join(x,''))


>>> add_reverse(11111111111111L)
22222222222222L
>>> add_reverse(11111111111119L)
102222222222230L
>>> add_reverse(876437643654354239321L)
1000370097110700973999L


The long(n) is to enforce that the L exists as otherwise this won't
work for regular integers.


> the loop must include moving the L to the end of the reversed string
> prior to summing.

No need, long("123456789123487") == 123456789123487L .

Andrew
dalke@acm.org
Reversing Long integers [ In reply to ]
"Ira H. Fuchs" <fuchs@princeton.edu> writes:

| I am attempting to write an efficient Python program which can add
| an integer to its reverse. This is quite easy to do in Lisp and
| Mathematica but (mostly out of curiosity) I wanted to see how one
| might do this in Python. Converting an integer to a string and
| reversing it and converting back is quite easy (although not all of
| these ops are primitives) but the fact that Long integers have the
| letter L appended means that the loop must include moving the L to
| the end of the reversed string prior to summing. Can anyone think of
| a particularly clever way to do this?

This isn't particularly clever, but it does the job. The
time spent dealing with checking for longs should be negligible
compared to the rest of it.

s = `i`
m = map (None, s) # Turn into an array of characters
is_long = 0
if m[-1] == 'L':
m, is_long = m[0:-1], 1
m.reverse()
s2 = string.join (m, '')
if (is_long):
i2 = long(s2)
else:
i2 = int(s2)

--
Dan Schmidt -> dfan@harmonixmusic.com, dfan@alum.mit.edu
Honest Bob & the http://www2.thecia.net/users/dfan/
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/
Reversing Long integers [ In reply to ]
> I am attempting to write an efficient Python program which
> can add an integer to
> its reverse. This is quite easy to do in Lisp and Mathematica
> but (mostly out of
> curiosity) I wanted to see how one might do this in Python.
> Converting an
> integer to a string and reversing it and converting back is
> quite easy (although
> not all of these ops are primitives) but the fact that Long
> integers have the
> letter L appended means that the loop must include moving the
> L to the end of
> the reversed string prior to summing. Can anyone think of a
> particularly clever
> way to do this?


Ok, I'll bite :) I don't know if this is particularly clever,
but it works and should be pretty fast...


import string
def addrev(num, join=string.join, atol=string.atol):
"""Add a long to its reverse"""
rev=list(str(num)[:-1])
rev.reverse()
return num + atol(join(rev, ''))



Brian Lloyd brian@digicool.com
Software Engineer 540.371.6909
Digital Creations http://www.digicool.com