Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple recursive function

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)
like image 918
tMC Avatar asked Dec 16 '22 14:12

tMC


1 Answers

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
like image 179
David Heffernan Avatar answered Dec 28 '22 08:12

David Heffernan