Skip to content

Instantly share code, notes, and snippets.

@MorganBorman
Created October 12, 2012 05:06
Show Gist options
  • Save MorganBorman/3877429 to your computer and use it in GitHub Desktop.
Save MorganBorman/3877429 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__":
def decimals():
for d in range(10):
yield str(d)
i = 0
for n in count(decimals):
print i, ":", n
i += 1
if i > 200:
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment