5

This is a pretty simple task i feel like i should be able to do- but just for the life of me, cant figure out.

I'm trying to write a recursive function to replicate the following:

chars = '0123456789abcdef'

for a in chars:
    for b in chars:
        for c in chars:
            for d in chars:
                print a+b+c+d

searching for an example hasn't proven very fruitful.

code thats not working:

chars = 'ABCDEF'

def resu(chars, depth = len(chars)):
    for char in chars:
        if depth == 0:
           return char
        return char + resu(chars, depth - 1)

print resu(chars)
2
  • 2
    Sounds like someone wants us to do their homework Commented May 6, 2011 at 17:08
  • 3
    nope, no homework, just curious. (wish i was still in school!) Commented May 6, 2011 at 17:09

4 Answers 4

5

You don't need recursion if you have itertools:

from itertools import product
for a,b,c,d in product('abc', repeat=4):
    print a+b+c+d
Sign up to request clarification or add additional context in comments.

2 Comments

Thats awesome to know about- but im really trying to understand recursion. Thanks :)
I don't think recursion is very natural here.
4

I'm not going to write it out, because that would defeat the purpose, 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 what I get for forgetting that Python isn't made for this sort of thing. It needs a flatten.

The reason it's not working is that return in the outermost loop will end the whole thing the first time it's called.

What you actually want to do in your case is 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 length chars, this will produce a LOT (n!) of lines. As has been pointed out, this is not the best way to get this result in Python, but it is useful to learn about recursion.

4 Comments

+1: depth-1 is an important hint for this specific kind of problem. The base case (depth==0) is the other important hint for understanding recursion. I think that adding the necessary if statement might help.
I've got the logic that knows when I am at the end of recursion, but I'm not sure what to do there- a return makes the entire stack return 00000 for example. a break causes the last function on the stack to return None and I get a type error. I don't know why I have such a hard time visualising this problem in my head :-/
@tMC: When you determine that this is the last time the function will be called, just return the character.
i tried that, i end up with just 00000. All the copies of the function return at once.
2

With @David Heffernan's answer, you also don't need writing your own combinations function if you have itertools:

from itertools import combinations_with_replacement
for i in combinations_with_replacement("0123",4): 
    print(i)

1 Comment

+1 very nice indeed, itertools is a veritable treasure trove!
1
chars = '0123456789abcdef'

def get_combinations(cur_combo=''):
    for char in chars:
        new_combo = cur_combo+char
        if len(new_combo) == 4:
            print new_combo
        else:
            get_combinations(new_combo)

get_combinations()

Disclaimer:

May not be a good example, but it is recursive and gives the correct result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.