【Python generator yield】 and 【Python built-in cheat】
Generator using yield\n\n\nclass CombinationIterator(object):\n\n    def __init__(self, characters, length):\n        self.generator = self.gen(characters, leng
## **Generator using yield**\n\n```\nclass CombinationIterator(object):\n\n    def __init__(self, characters, length):\n        self.generator = self.gen(characters, length)\n        self.buffer = next(self.generator)\n        \n    def gen(self, characters, length):\n        if length == 0:\n            yield ""\n        elif len(characters) == length:\n            yield characters\n        elif len(characters) > length:\n            for tail in self.gen(characters[1:], length - 1):\n                yield characters[0] + tail\n            for tail in self.gen(characters[1:], length):\n                yield tail\n        \n    def next(self):\n        res = self.buffer\n        try:\n            self.buffer = next(self.generator)\n        except StopIteration:\n            self.buffer = None\n        return res\n        \n    def hasNext(self):\n        return self.buffer is not None\n```\n\n---\n\n## **Python cheat**\n\n```\nfrom itertools import combinations\nclass CombinationIterator(object):\n\n    def __init__(self, characters, combinationLength):\n        self.it = combinations(characters, combinationLength)\n        self.buffer = "".join(next(self.it)) if characters else None\n        \n\n    def next(self):\n        res = self.buffer\n        try:\n            self.buffer = "".join(next(self.it))\n        except:\n            self.buffer = None\n        return res\n        \n\n    def hasNext(self):\n        return self.buffer is not None\n```
[]