I solved a problem where I had to find sum of numbers whose digits are made of only 4, 5 and 6.. The numbers have at most x fours, at most y fives and at most z sixes. I successfully passed some of the sample tests. But, I cannot get past other test cases as i keep getting segfault errors. Also, I think my programs run time is seriously long. Any help in reducing the run time, optimizing the solution and -reventing segfault will be appreciated. Here is my code in Python:
from itertools import permutations
x , y, z = raw_input().split(' ')
x = int(x)
y = int(y)
z = int(z)
max = ''
for a in range(z):
max += '6'
for b in range(y):
max += '5'
for c in range(x):
max += '4'
perms = [''.join(p) for p in permutations(max)]
chances = []
def substring(string):
possible = []
for x in range(len(string) + 1):
for y in range(x, len(string) + 1):
possible.append(string[x:y])
return possible
for d in perms:
chances += list(set(substring(d)))
chances = list(set(chances))
chances.remove('')
sum = 0
for nstr in chances:
sum += int(nstr)
print sum