4

I am solving SPOJ -ADDREV problem in short the question is :

Reversed number is For example, 1245 becomes 5421 and 2314 becomes 4132 .

Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21).
Your task is to add two reversed numbers and output their reversed sum.

Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).

Input

The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.

Output

For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.

Example

Sample input:

3
24 1
4358 754
305 794

Sample output:

34
1998
1

my code is :

ctr=0
up=raw_input()
tocal=raw_input()
while ctr!=int(up):
    l=tocal.split(' ')
    num1=int(str(l[0])[::-1])
    num2=int(str(l[1])[::-1])
    while num1%10 ==0 or num2%10==0:
        if num1%10==0:
            num1=num1[:-1]
        elif num2%10==0:
            num2=num2[:-1]


    sum=int(num1)+int(num2)

    rsum=int(str(sum)[::-1])

    print rsum
    ctr+=1
    tocal=raw_input()

while testing in SPOJ it returned Runtime error and in ideone it shows

Traceback (most recent call last):
File "prog.py", line 2, in <module>
EOFError: EOF when reading a line

I checked my code on my pc and the results were correct I don't know what is happening here (I am new to SPOJ and may not know ways of writing code for these type of sites) unlike here and here I have not written any code within raw_input() then what is happening?

Also my code runs 1 more than desired call(i.e the raw_input() call) And I have not found any way to fix it .

13
  • It doesn't look like you provided any input to your program. After correcting that with your ideone here I get another error because you're trying to use strings num1 and num2 as integers. Commented Mar 17, 2016 at 19:45
  • @KurtStutsman I wrote this in codeskulptor and there everything works fine I checked it myself with different inputs Commented Mar 17, 2016 at 19:48
  • Not really familiar with codeskulptor, but a quick examination says it runs Python code in browser which means javascript. Javascript is a loosely typed language so it has no problem converting strings into integers on the fly. This represents a bug in their conversion of Python to Javascript. Your code is flawed as it is now. Commented Mar 17, 2016 at 19:53
  • @KurtStutsman can you correct the flaw in my code you can edit my code or can give your's Commented Mar 17, 2016 at 19:54
  • 1
    You don't need that because int() will already do it for you. Commented Mar 17, 2016 at 20:08

4 Answers 4

3

I think this can be much more simple:

n1 = 754
n2 = 4358
print(int(str(int(str(n1)[::-1]) + int(str(n2)[::-1]))[::-1]))

Core:

int(str(n1)[::-1]) # convert integer to string, reverse string, convert string to integer

You have to do this 2x, once for each integer, then sum, then convert to string, reverse, and back to int.

The 'int' function will automatically strip the leading zeros

Sign up to request clarification or add additional context in comments.

2 Comments

This is a natural approach, but if the goal is to quickly do 10,000 cases then it is possible that all the conversion between ints and lists of strings will be too time-consuming. Hard to know without timing it.
Takes 0.4 seconds to compute and print 10k cases (rand ints btwn 1 and 10000), and I'm still rocking the T61
0

A function is required to do all the job for one integer:

input = """\
3
24 1
4358 754
305 794
"""

def rev(s):     ## s is an integer
    return int(''.join(list(str(s))[::-1]))

print [ rev(sum(map(rev, x.split()))) if len(x.split()) == 2 else '' for x in input.splitlines() ]

Output:

['', 34, 1998, 1]

Comments

0

Here is a one liner:

>>> s='4358 754'
>>> int(str(reduce(lambda x,y: x+y, map(int, [e[::-1] for e in s.split()])))[::-1])
1998

Comments

0
t = int(input()) #Taking number of test cases
if (t > 0): #If test cases are greater than zero
    for _ in range(t): #for all numbers in range of test cases
        n1, n2 = map(int, input().split()) #taking two input numbers in a single line
        print (int(str(int(str(n1)[::-1]) + int(str(n2)[::-1]))[::-1]))
                #convert integer to string, reverse string, convert string to integer and print the sum of these two integers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.