Hello Kent,
I tried finding solution with using only strings but unfortunately reversing
a number has some interesting issues - For example: take following two
numbers -
002000 002000
though the final answer is 4 - arriving at it using string is stripping '0'
from both left and right of both numbers. I thought it was far easier to
convert it into an integer and reverse. Anyway I wasn't aware of string
reversal shortcut - [::-1] ... thanx :)
Following code performs in 0.50 seconds :
import sys
def rev(n) :
m = 0
while n > 0 :
r = n%10
m = m*10 + r
n = n/10
return m
def solve(line) :
nums = line.split(' ')
a = int(nums[0].strip('0')[::-1])
b = int(nums[1].strip('0')[::-1])
return rev( a + b )
if __name__ == '__main__' :
N = int(sys.stdin.readline())
lines = sys.stdin.readlines()
for i in xrange(N) :
print solve(lines[i])
I will start working on psyco ... though not sure what is it ?
Thanks again Kent.
Cheers
Aditya
On 9/27/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> Aditya Lal wrote:
>
> > def rev(n) :
> > m = 0
> > while n > 0 :
> > m = m*10 + n%10
> > n = n/10
> > return m
>
> I would try reversing the numbers as strings. s[::-1] will reverse a
> string s.
>
> > How do I improve the input reading or string parsing ? I know there
> > should be a better way because the best program (at cost of memory)
> > performs > 50% faster than mine. So he must have done something to
> > improve input processing.
>
> The programs that use a lot of memory are using psyco.
>
> Kent
>
--
Aditya
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor