|
Description:
making a program that will write a program to find all the combinaison of a string
Source: Text Source
import string
strng = "131"
size = len(strng)
prog = ""
tab = ""
i = 0
str0 = strng
result = ""
while i < size :
prog+= """%sfor l%s in str%s:\n"""%(tab,i,i)
if (i+1 != size):
prog+= """%sstr%s = string.replace(str%s,l%s,"",1)\n"""%(tab+" ",i+1,i,i)
result += "l%s+"%i
prog += "%sprint %s\n"%(tab+" ",result[0:len(result)-1])
tab += " "
i = i +1
print prog
exec(prog)
print "the end"
Discussion:
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
|