#!/usr/bin/python3.3 # -*- coding: utf-8 -*- import os from multiprocessing import Process,Queue,cpu_count from math import factorial #debugos.system('clear') def test(l,c1,doublon) : compteur_doublon=doublon liste2 = list(range(10)) liste2.remove(c1) c = list(range(11)) #c[1 à 10] will try every possible numbers c[1]=c1 for c[2] in liste2 : liste3 = liste2[:] liste3.remove(c[2]) for c[3] in liste3 : liste4 = liste3[:] liste4.remove(c[3]) for c[4] in liste4 : liste5 = liste4[:] liste5.remove(c[4]) for c[5] in liste5 : liste6 = liste5[:] liste6.remove(c[5]) for c[6] in liste6 : liste7 = liste6[:] liste7.remove(c[6]) for c[7] in liste7 : liste8 = liste7[:] liste8.remove(c[7]) for c[8] in liste8 : liste9 = liste8[:] liste9.remove(c[8]) for p in range(2) : c[9] = liste9[p] c[10]= liste9[1-p] if ((((c[l[1]]+c[l[6]]-c[l[11]])*10+c[l[2]]+c[l[7]]-c[l[12]])*10+c[l[3]]+c[l[8]]-c[l[13]])*10+c[l[4]]+c[l[9]]-c[l[14]])*10+c[l[5]]+c[l[10]]-c[l[15]] == 0 : compteur_doublon -= 1 if compteur_doublon == 0 : compteur_doublon = doublon solution.put(c) #found a list c that works print(c) #debug line print it now if __name__ == '__main__': solution = Queue() Ncoeur = cpu_count() Ncoeur = 1 #debug line to force mono-process print ("""Find solutions like "papa" + "maman" = "amour" where each letter is a different number and the addition will be right """) #debug add1 = input("1er terme (max 5 letters) : ") add1="papa" #debug line #debug add2 = input("2nd terme (max 5 letters) : ") add2="maman" #debug line #debug somme = input("Addition (max 5 letters) : ") somme="amour" #debug line lettres = (6-len(add1))*" "+add1+(5-len(add2))*" "+add2+(5-len(somme))*" "+somme #lettres will become the string " bisesbisesamour" (16 car) l = [0]*16 i=0 for a in range(1,16) : if lettres[a] not in lettres[0:a] : i = i+1 l[a] = i else : l[a] = l[lettres.index(lettres[a])] #l[i] give ana apparition order to each different letter print (i,"different letters") if i > 10 : print ("It's impossible") quit() #end of the program else : print ("I'm looking for solution...") print () doublon=factorial(10-i) compteur_solution=0 print ("solution.put(c) found and printed by each process :") #debug line c1=0 while c1 < 10 : for i in range(Ncoeur) : if c1 < 10 : p = Process(target=test,args=(l,c1,doublon,)) p.start() c1+=1 p.join() print() #debug line print ("solution.get() in the main programm :") #debug line fin=[0]*11 solution.put(fin) c=solution.get() while c != fin : print(c) #debug line #debug print (" ",c[l[1]],c[l[2]],c[l[3]],c[l[4]],c[l[5]]) #debug print ("+",c[l[6]],c[l[7]],c[l[8]],c[l[9]],c[l[10]]) #debug print ("-----------") #debug print ("=",c[l[11]],c[l[12]],c[l[13]],c[l[14]],c[l[15]]) #debug print() compteur_solution+=1 c=solution.get() print() #debug line print (compteur_solution," solutions") print () q=input("Press ENTER to quit ")