Need help with 3rd question of GCJ qualifiers

I was trying this question yesterday. Was only interested in completely solving it as by solving first I was already qualified for the next round. But I am unable to understand why was my python code giving Runtime Error in GCJ. Please help me

import math
def gcd(a,b): 
    if(b==0): 
        return a 
    else: 
        return gcd(b,a%b) 
        
def getNumber(a):
    n = 0
    for i in a:
        n = n * 10
        n = n + ord(i) - ord('0')
    return n
def solve(t):
    inp = raw_input().split()
    arr = []
    for i in inp:
        arr.append(getNumber(i))
    arr1 = []
    arr2 = []
    for i in range(1,len(arr)):
        if arr[i] != arr[i-1]:
            if i-2 >= 0:
                arr1 = arr[0:i-2]
            arr2 = arr[i-1:]
            # print(i)
            # print(arr2)
            break  
    el2 = []
    el1 = []
    tmp = arr2[0]/gcd(arr2[0],arr2[1])
    el2.append(tmp)
    for i in arr2:
        el2.append(i/el2[len(el2)-1])
    if len(arr1) > 0:
        el1.append(arr1[(len(arr1)-1)]/el2[0])
        i = len(arr)-2
        while i >= 0:
            el1.append(arr1[i]/el1[len(el1)-1])
            i = i-1
        el1 = el1[::-1]
    el = []
    for i in el1:
        el.append(i)
    for i in el2:
        el.append(i)
    d_el = []
    for i in el:
        if i in d_el:
            continue
        else:
            d_el.append(i)
    d_el.sort()
    dic = {}
    j = 0
    for i in d_el:
        dic[i] = chr(ord('A') + j)
        j = j+1
    s = ""
    for i in el:
        s = s+dic[i]
    print("Case #"+str(t)+": "+s)
t = int(input())
for i in range(t):
    arr = raw_input()
    solve(i+1)

Happened to me as well :-
Consider this sequence:- 6 6 4
Answer can(is actually) be : 2 3 2 2
But sometimes,the program output(s):- 3 2 3 …(ooops)… Now 3 is not properly divisible by (4) and this can give different types of error including runtime error(s) as 3/4=0…after sometime,you might calculate x/0 which can give runtime error.
To handle this,you should take care for this corner-case!

1 Like