I'm not going to write this because it will defeat the goal, but here's a hint: think about the conditions under which you stop recursing. Here's the key bit:
for char in chars: return char + recurse(chars, depth - 1)
Edit: This is something I can forget that Python is not made for this kind of thing. It needs smoothing.
The reason it doesn't work is because returning in the outer loop will end all this on the first call.
What you really want to do in your case looks more like this:
def resu(chars, depth = None, prefix=''): if depth is None: depth = len(chars) if depth == 0: print prefix return for ch in chars: resu(chars, depth - 1, ch + prefix)
Note that for even moderate chars this will result in LOT ( n! ) Lines. As already stated, this is not the best way to get this result in Python, but it is useful to learn about recursion.
source share