Welcome, guest | Sign In | My Account | Store | Cart

making a program that will write a program to find all the combinaison of a string

Python, 37 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37``` ```import string #the string that we want to test the combination strng = "131" #the size of the string size = len(strng) #the string that will contain the program generated prog = "" #here we will stock the tabulation tab = "" #the variable that will count and b used for the meta variable i = 0 #the first str meta variable must be initialised str0 = strng # a meta variable that will stock the sum of the l meta variable result = "" while i < size : #we generate a for with l in str meta variable string prog+= """%sfor l%s in str%s:\n"""%(tab,i,i) if (i+1 != size): #then we substract one letter in the sequence prog+= """%sstr%s = string.replace(str%s,l%s,"",1)\n"""%(tab+" ",i+1,i,i) # here we stock the sum of the meta variable ex : l0+l1 etc ... result += "l%s+"%i prog += "%sprint %s\n"%(tab+" ",result[0:len(result)-1]) #at the end of the loop tab += " " i = i +1 #we print the program print prog #we execute the program exec(prog) print "the end" #please note that there could be some doublon in the resultat ```

Some times it's useful to test all the combination of a string and using meta programmation is truly a good exercice. It develops a different way of thinking about algorythm. I did it because for a math problem i had to find all the possibility of a sequence of number

 Created by bussiere bussiere on Mon, 24 Mar 2008 (PSF)