[Python] SPOJ: Simple Arithmetics (ARITH) giving Wrong Answer

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    class Main:
    def addition(self, num1: int, num2: int):
    ans = str(num1 + num2)
    s1 = str(num1)
    s2 = str(num2)
    s2 = "+" + s2
    l = max(len(ans), len(s1), len(s2))
    ans = " "*(l-len(ans)) + ans
    s1 = " "*(l-len(s1)) + s1
    s2 = " "*(l-len(s2)) + s2
    print(s1)
    print(s2)
    print("-"*l)
    print(ans)

    def subtraction(self, num1: int, num2: int):
    ans = str(num1 - num2)
    s1 = str(num1)
    s2 = str(num2)
    s2 = "-" + s2
    l = max(len(ans), len(s1), len(s2))
    ans = " "*(l-len(ans)) + ans
    s1 = " "*(l-len(s1)) + s1
    s2 = " "*(l-len(s2)) + s2
    print(s1)
    print(s2)
    print("-"*l)
    print(ans)

    def multiplication(self, num1: int, num2: int):
    s1 = str(num1)
    s2 = str(num2)
    s2 = "*" + s2
    n = len(s2)
    arr = [0 for i in range(n+2)]
    arr[0] = num1
    arr[1] = num2
    sum = 0
    for i in range(n-1,0,-1):
    calc = num1*(ord(s2) - ord('0'))
    arr[n-i+1] = calc
    sum += calc * 10**(n-i-1)
    arr[n+1] = sum
    l=0
    for i in range(n+2):
    l = max(l, len(str(arr)))
    if len(s1) == 1:
    l += 1
    print(" "*(l-len(s1)) + s1)
    print(" "*(l-len(s2)) + s2)
    m = max(len(s1), len(s2))
    print(" "*(l - m)+"-"*m)

    # if num1 == 0 or (num2==0 and len(s1) == 1):
    # l+=1
    for i in range(2, n+1):
    temp = str(arr)
    print(" "*(l-len(temp) - i + 2) + temp)
    print("-"*l)
    # if num1 == 0:
    # l-=1
    temp = str(arr[n+1])
    print(" "*(l-len(temp)) + temp)

    T = int(input())
    while T:
    T -= 1
    s = input()
    obj = Main()
    if '+' in s:
    num1, num2 = s.split("+")
    obj.addition(int(num1), int(num2))
    if '-' in s:
    num1, num2 = s.split("-")
    obj.subtraction(int(num1), int(num2))
    if '*' in s:
    num1, num2 = s.split("*")
    obj.multiplication(int(num1), int(num2))


    This code is giving wrong answer, but I can't find incorrect test cases. Could anyone explain why? and maybe provide more test cases. I have solved so many edge cases but still this has trouble.

    Input format: There is a single positive integer T on the first line of input (equal to about 1000). It stands for the number of expressions to follow. Each expression consists of a single line containing a positive integer number, an operator (one of +, - and *) and the second positive integer number. Every number has at most 500 digits. There are no spaces on the line. If the operation is subtraction, the second number is always lower than the first one. No number will begin with zero.

    Output format: For each expression, print two lines with two given numbers, the second number below the first one, last digits (representing unities) must be aligned in the same column. Put the operator right in front of the first digit of the second number. After the second number, there must be a horizontal line made of dashes (-).

    For each addition or subtraction, put the result right below the horizontal line, with last digit aligned to the last digit of both operands.

    For each multiplication, multiply the first number by each digit of the second number. Put the partial results one below the other, starting with the product of the last digit of the second number. Each partial result should be aligned with the corresponding digit. That means the last digit of the partial product must be in the same column as the digit of the second number. No product may begin with any additional zeros. If a particular digit is zero, the product has exactly one digit -- zero. If the second number has more than one digit, print another horizontal line under the partial results, and then print the sum of them.

    There must be minimal number of spaces on the beginning of lines, with respect to other constraints. The horizontal line is always as long as necessary to reach the left and right end of both numbers (and operators) directly below and above it. That means it begins in the same column where the leftmost digit or operator of that two lines (one below and one above) is. It ends in the column where is the rightmost digit of that two numbers. The line can be neither longer nor shorter than specified.

    Print one blank line after each test case, including the last one.

    Here is the question link: https://www.spoj.com/problems/ARITH/cstart=30

    Continue reading...

Compartilhe esta Página