Python 2.x (<s>197</s> 212), Python 3.x (<s>182</s> 200) -- Related question, related answer. Gives the answer and an error if there is one, else prints "NO SOLUTION". Python 2.x Version: from itertools import * a=raw_input().split();c=1;b=len(a) for i in product('123456789',repeat=b): if eval(''.join(sum(zip([j+'.'for j in i],a),()))[:3*b-1])==int(a[-1]):print' '.join(i);c=0 print"NO SOLUTION"*c Python 3.x Version: I don't run Python 3.x, so this may give errors. from itertools import * a=raw_input().split();c=1;b=len(a) for i in product('123456789',repeat=b): if eval(''.join(sum(zip(i,a),()))[:3*b-1])==int(a[-1]):print(' '.join(i));c=0 print("NO SOLUTION"*c)