Last active
December 21, 2015 04:59
-
-
Save mark-adams/6253206 to your computer and use it in GitHub Desktop.
A simple generator that returns a incrementing string of characters a-z, aa-zz, aaa-zzz, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import string | |
import math | |
default_alphabet = string.uppercase | |
def product_depth_first(alphabet=default_alphabet): | |
alphabet_list = [alphabet] | |
while (True): | |
for result in itertools.product(*alphabet_list): | |
yield ''.join(result) | |
alphabet_list.append(alphabet) | |
def product_straight(cols, alphabet=default_alphabet): | |
alphabet_list = [alphabet for x in range(cols)] | |
for result in itertools.product(*alphabet_list): | |
yield ''.join(result) | |
find_maxlen_straight = lambda num_items, alphabet=default_alphabet: int(math.ceil(math.log(num_items,len(alphabet)))) | |
if __name__ == '__main__': | |
items = range(678) | |
max_len = find_maxlen_straight(len(items)) | |
letter_factory = product_straight(max_len) | |
for i in items: | |
print letter_factory.next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment