Skip to content

Instantly share code, notes, and snippets.

@bgschiller
Forked from MorganBorman/count.py
Created October 12, 2012 05:30
Show Gist options
  • Save bgschiller/3877484 to your computer and use it in GitHub Desktop.
Save bgschiller/3877484 to your computer and use it in GitHub Desktop.
Counting using a generator which returns the digits of an arbitrary number system
def count(digitgen):
"Takes an iterator which yields the digits of a number system and counts using it."
def subcount(digitgen, places):
if places == 1:
for d in digitgen():
yield d
else:
for d in digitgen():
for ld in subcount(digitgen, places - 1):
yield d + ld
for d in digitgen():
yield d
places = 2
while True:
first = True
for d in digitgen():
if first:
first = False
continue
for ld in subcount(digitgen, places - 1):
yield d + ld
places += 1
if __name__ == "__main__":
import string
def labelelements():
for char in string.ascii_lowercase:
yield char
for char in string.ascii_uppercase:
yield char
def base2():
for d in range(2):
yield str(d)
i = 0
for label in count(labelelements):
print i, ":", label
i += 1
if i > 200:
break;
i = 0
for b2 in count(base2):
print i, ':', b2
i += 1
if i > 200:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment