Hello, I wrote a code for a problem and submitted to an online judge, a program that runs my program against a set of predefined input data.
I wrote it for exercise my python writing. In don't want to discuss the algorithm here! I dare to ask for help because the answer was : crashed with Runtime Error: NZEC (non-zero exit code). I heard that is a general error for most interpreters and elsewhere that NZEC error code has to do with exceptions. So, i'm seeking for a possible problem with the coding, not with the algorithm. Here, the code. (l also attached it in a file) Using characters of the string, the program should construct the maximum number which divides by fifteen without remainder. example *Input:* 1 02041 *Output:* 4200 [code] import sys def write(number): # assamble the number # assure the 5 divisibility if number[0] != 0: number[0] -= 1 last_d = 0 else: number[5] -= 1 last_d = 5 zero = True for i in reversed(range(1,10)): for j in range(digits[i]): sys.stdout.write(i) zero = False # leading zeroes should be omitted if zero == False: for i in range(digits[0]): sys.stdout.write(0) print last_d def remove_item(d,item): if type(item) != list: d[item] -= 1 else: d[item[0]] -= 1 d[item[1]] -= 1 def case5(d,item): # return True if i try to remove the only five that assure the divisibility with 5 return item == 5 and d[0] == 0 and d[item] == 1 def exist(d,item): found = False if type(item) != list: if d[item] != 0 and case5(d,item) == False: found = True else: p1 = d[item[0]] != 0 and case5(d,d[item[0]]) == False d[item[0]] -= 1 p2 = d[item[1]] != 0 and case5(d,d[item[1]]) == False d[item[0]] += 1 if p1 == True and p2 == True: found = True return found def remove(d, mult): found = False for item in mult: if exist(d,item) == True: remove_item(d,item) found = True break if found == False: d = [] return d def remove_all(digits,rest): # if the number is the form 3k+1, i try to remove one 3k+1 digit or two 3k+2 digits # if the number is the form 3k+2, i try to remove one 3k+2 digit or two 3k+2 digits one = [1,4,7,[2,2],[2,5],[2,8],[5,5],[5,8],[8,8]] two = [2,5,8,[1,1],[1,4],[1,7],[4,4],[4,7],[7,7]] if rest == 1: d = remove(digits, one) else: d = remove(digits, two) return d def resolve(digits): suma = 0 for i in range(10): suma += i*digits[i] rest = suma % 3 if rest != 0: # if it's not divisible with 3 i try to remove some digits digits = remove_all(digits,rest) return digits t = int(raw_input()) for tt in range(t): line = raw_input() digits = [0]*10 # counts the frequence of digits in the string for i in range(len(line)): digits[int(line[i])] += 1 # if it's not divisible with 5 if digits[0] == 0 and digits[5] == 0: print("impossible") else: number = resolve(digits) if(number == []): print("impossible") else: write(number) [/code] Any alternatives for the personal rambling(if it's a wrong way to do it, of course): L = [1,2,[3,4],[5,5]] for item in L: if type(item) == list: print L[item[0]], L[item[1]] else: print L[item] Thanks, Andrei
import sys def write(number): # assamble the number # assure the 5 divisibility if number[0] != 0: number[0] -= 1 last_d = 0 else: number[5] -= 1 last_d = 5 zero = True for i in reversed(range(1,10)): for j in range(digits[i]): sys.stdout.write(i) zero = False # leading zeroes should be omitted if zero == False: for i in range(digits[0]): sys.stdout.write(0) print last_d def remove_item(d,item): if type(item) != list: d[item] -= 1 else: d[item[0]] -= 1 d[item[1]] -= 1 def case5(d,item): # return True if i try to remove the only five that assure the divisibility with 5 return item == 5 and d[0] == 0 and d[item] == 1 def exist(d,item): found = False if type(item) != list: if d[item] != 0 and case5(d,item) == False: found = True else: p1 = d[item[0]] != 0 and case5(d,d[item[0]]) == False d[item[0]] -= 1 p2 = d[item[1]] != 0 and case5(d,d[item[1]]) == False d[item[0]] += 1 if p1 == True and p2 == True: found = True return found def remove(d, mult): found = False for item in mult: if exist(d,item) == True: remove_item(d,item) found = True break if found == False: d = [] return d def remove_all(digits,rest): # if the number is the form 3k+1, i try to remove one 3k+1 digit or two 3k+2 digits # if the number is the form 3k+2, i try to remove one 3k+2 digit or two 3k+2 digits one = [1,4,7,[2,2],[2,5],[2,8],[5,5],[5,8],[8,8]] two = [2,5,8,[1,1],[1,4],[1,7],[4,4],[4,7],[7,7]] if rest == 1: d = remove(digits, one) else: d = remove(digits, two) return d def resolve(digits): suma = 0 for i in range(10): suma += i*digits[i] rest = suma % 3 if rest != 0: # if it's not divisible with 3 i try to remove some digits digits = remove_all(digits,rest) return digits t = int(raw_input()) for tt in range(t): line = raw_input() digits = [0]*10 # counts the frequence of digits in the string for i in range(len(line)): digits[int(line[i])] += 1 # if it's not divisible with 5 if digits[0] == 0 and digits[5] == 0: print("impossible") else: number = resolve(digits) if(number == []): print("impossible") else: write(number)
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor