1 / 6
Jul 2007

I have not worked with judge programs programs before, and I am not sure why this code doesn't work.

#http://www.spoj.pl/problems/ARITH/
import string
import re
def parser(lines):
    re_nums = re.compile("[0-9]*")
    re_sign = re.compile("[+,\-,*]")
    for ii in string.split(lines, '\n'):
        nums = re_nums.findall(ii) #finds the 2 numbers
        sign = re_sign.findall(ii) #finds the sign
        if re_sign.search(ii) and len(nums) == 4: #checks that there is 2 numbers and a sign
        if sign[0] == "+":
            add(int(nums[0]),int( nums[2]))
        elif sign[0] == "-":
            subtract(int(nums[0]),int( nums[2]))
        elif sign[0] == "*":
            multiply(int(nums[0]),int(nums[2]))
def multiply(vala, valb):
    product = vala * valb
    rr = max(len(`vala`), len(`valb`) +1, len(`product`)) #how far right to align the calculation
    print
    print string.rjust(`vala`, rr)
    print string.rjust('*' + `valb`, rr)
    print string.rjust('-' * max(len(`vala`), len(`valb`)+1), rr)
    if len(`valb`) > 1: #if there is more then one digit in the multiplyer do split and sum
        for xx in ''.join(reversed(`valb`)): #revese the string to make digits sum correct
            print string.rjust(str(int(xx) * vala), rr)
            rr = rr - 1
        print "-" * len(`product`)
    print product
def add(vala, valb):
    sum = vala + valb
    rr = max(len(`vala`), len(`valb`) +1, len(`sum`)) #how far right to align the calculation
    print 
    print string.rjust(`vala`, rr)
    print string.rjust('+' + `valb`, rr)
    print string.rjust('-' * rr, rr)
    print string.rjust(`sum`, rr)
def subtract(vala, valb):
    sum = vala - valb
    rr = max(len(`vala`), len(`valb`) +1, len(`sum`)) #how far right to align the calculation
    print 
    print string.rjust(`vala`, rr)
    print string.rjust('-' + `valb`, rr)
    print string.rjust('-' * rr, rr)
    print string.rjust(`sum`, rr)   
if __name__ == '__main__':
    input = ''
    items = int(raw_input())
    ii = 0
    while ii < items:
        input = input + str(raw_input()) + '\n'
        ii = ii + 1
    parser(input)

I/O
5
100+5568
2365-8854
656582*6565
55984+23
52525*65
  100
+5568
-----
 5668
 2365
-8854
-----
-6489
     656582
  *6565
 ------
3282910
   3939492
  3282910
 3939492
----------
4310460830
55984
  +23
-----
56007
  52525
    *65
  -----
 262625
315150
-------
3414125

It runs on my computer and I have tried a lot of different cases and the only way I can make it error is to give it a non int on the first line. The judge keeps giving me the "runtime error (NZEC)". Is there a way to get any more information out of the judge, or does any one see the problem?

This might be a silly question, as I don't know python, but will your program handle numbers with 500 digits?

I don't know. Thats a good question I will try modifying it to use long ints and see what happens.

As I said, I don't know python, but I really doubt long ints will be big enough.

Now I just get the wrong answer smile
Not sure why the output matches perfectly, oh well.

#http://www.spoj.pl/problems/ARITH/
import string
import re
def parser(lines):
    re_nums = re.compile("[0-9]*")
    re_sign = re.compile("[+,\-,*]")
    for ii in string.split(lines, '\n'):
        nums = re_nums.findall(ii) #finds the 2 numbers
        sign = re_sign.findall(ii) #finds the sign
        if len(sign) == 1 and len(nums) == 4: #checks that there is 2 numbers and a sign
            if sign[0] == "+":
                add(long(nums[0]),long( nums[2]))
            elif sign[0] == "-":
                subtract(long(nums[0]),long( nums[2]))
            elif sign[0] == "*":
                multiply(long(nums[0]),long(nums[2]))
def multiply(vala, valb):
    product = vala * valb
    rr = max(len(str(vala)), len(str(valb)) +1, len(str(product))) #how far right to align the calculation
    print
    print string.rjust(str(vala), rr)
    print string.rjust('*' + str(valb), rr)
    print string.rjust('-' * max(len(str(vala)), len(str(valb))+1), rr)
    if len(str(valb)) > 1: #if there is more then one digit in the multiplyer do split and sum
        for xx in ''.join(reversed(str(valb))): #revese the string to make digits sum correct
            print string.rjust(str(long(xx) * vala), rr)
            rr = rr - 1
        print "-" * len(str(product))
    print product
def add(vala, valb):
    sum = vala + valb
    rr = max(len(str(vala)), len(str(valb)) +1, len(str(sum))) #how far right to align the calculation
    print 
    print string.rjust(str(vala), rr)
    print string.rjust('+' + str(valb), rr)
    print string.rjust('-' * rr, rr)
    print string.rjust(str(sum), rr)
def subtract(vala, valb):
    sum = vala - valb
    rr = max(len(str(vala)), len(str(valb)) +1, len(str(sum))) #how far right to align the calculation
    print 
    print string.rjust(str(vala), rr)
    print string.rjust('-' + str(valb), rr)
    print string.rjust('-' * rr, rr)
    print string.rjust(str(sum), rr)   
if __name__ == '__main__':
    items = raw_input()
    is_num =  re.compile('[0-9]+')
    while not is_num.match(items): items = raw_input()
    input = ''
    ii = 0
    while ii < int(items):
        input = input + str(raw_input()) + '\n'
        ii = ii + 1
    parser(input)
    print

 12345
+67890
------
 80235
 324
-111
----
 213
    325
  *4405
  -----
   1625
     0
 1300
1300
-------
1431625

heres what the manual said

I guess long has no limit. I don't know. I am new to python. I am using the site to get some practice.