On Tue, 23 Nov 2010 13:16:46 +0000 Jose Amoreira <ljmamore...@gmail.com> wrote:
> I read somewhere that for any recursive algorithm there is a > sequential one that is equivalent > [...] > Is there a more straightforward way of solving my specific problem > or, better yet, a general solution to the need of a variable number > of for loops? I am not 100% sure I got the terms of your problem and I am neither an official tutor of this list, so disregard my comment if it is irrelevant... ...yet, the way I normally understand the first sentence I quoted is that any recursive solution can be written iteratively, or in other words "flat" (i.e. having the same procedure called by the main program X number of times, rather than having the procedure calling itself, so effectively nesting itself X number of times). The code you wrote generates programs like: for l0 in alphabet: for l1 in alphabet: for l2 in alphabet: word = "".join([eval("l"+str(i)) for i in range(n)]) listOfWords.append(word) which are recursive in essence (although the recursion is hard-coded rather than dynamic). This is not bad (problems in the domain of combinatorics - like yours - get normally solved recursively) but I can't imagine what would the advantage of this code over dynamic recursion. As for a more straightforward way to solve your specific problem: I would suggest you take a look to the combinatoric generators in the itertools module (http://docs.python.org/library/itertools.html). HTH at least a bit! Mac. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor