python - segmentation fault, need to prevent this and reduce the run time of my program -
i solved problem had find sum of numbers digits made of 4, 5 , 6.. numbers have @ x fours, @ y fives , @ z sixes. passed of sample tests. but, cannot past other test cases keep getting segfault errors. also, think programs run time long. in reducing run time, optimizing solution , -reventing segfault appreciated. here code in python:
from itertools import permutations x , y, z = raw_input().split(' ') x = int(x) y = int(y) z = int(z) max = '' in range(z): max += '6' b in range(y): max += '5' c in range(x): max += '4' perms = [''.join(p) p in permutations(max)] chances = [] def substring(string): possible = [] x in range(len(string) + 1): y in range(x, len(string) + 1): possible.append(string[x:y]) return possible d in perms: chances += list(set(substring(d))) chances = list(set(chances)) chances.remove('') sum = 0 nstr in chances: sum += int(nstr) print sum
it helpful know part of program consuming time. looking @ second half, after call permutations, see creating potentially huge lists (in chances , permutations. after building them convert set (to eliminate duplicates suppose) , again list. why not use single set instead, this:
chances = set() def substring(string): x in range(len(string) + 1): y in range(x, len(string) + 1): chances.add(string[x:y]) d in perms: substring(d) chances.remove('') sum = 0 nstr in chances: sum += int(nstr) print sum i don't know if solve problems, should help.
Comments
Post a Comment