iterator? way of generating all possible combinations?

akameswaran at gmail.com akameswaran at gmail.com
Sat May 27 00:45:22 EDT 2006


Ok, this is really irritating me.  I'm sure there are different ways of
doing this - I'm interested in the algo, not the practical solution,
I'm more trying to play with iterators and recursion.  I want to create
a program that generates every possible combination of a set of a n
dice, with s sides.

so I started with an iterator

class die(object):
    def __init__(self,sides):
        self.sides = range(1,sides+1)
    def __iter__(self):
        return self
    def next(self):
        self.sides = self.sides[1:] + [self.sides[0]]
        return self.sides[-1]


now my thought was to create a recursive function to iterate through
all the dice combinations.  Unfortunately... I'm coming up with a dead
end.  I've tried working it out with different version of the die, ie
one that doesn't loop infinitely, but instead takes a starting
position.  A vaiety of things, and yet I can't find a nice recursive
function that falls out of the die class.

Any ideas?  or better terms to google?  cuz I've tried.

Thanks




More information about the Python-list mailing list