Mercurial > public > think_complexity
annotate ch2ex7.py @ 10:aea27d10dd18
Chapter 2.7, exercise 7; a generator to create identifiers.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 03 Dec 2012 19:49:23 -0600 |
parents | |
children |
rev | line source |
---|---|
bgneal@10 | 1 """Chapter 2.7, exercise 7. |
bgneal@10 | 2 |
bgneal@10 | 3 Write a generator that yields an infinite sequence of alpha-numeric identifiers, |
bgneal@10 | 4 starting with a1 through z1, then a2 through z2, and so on. |
bgneal@10 | 5 |
bgneal@10 | 6 """ |
bgneal@10 | 7 import string |
bgneal@10 | 8 |
bgneal@10 | 9 def generate_identifier(): |
bgneal@10 | 10 num = 1 |
bgneal@10 | 11 while True: |
bgneal@10 | 12 for c in string.lowercase: |
bgneal@10 | 13 yield c + str(num) |
bgneal@10 | 14 num += 1 |
bgneal@10 | 15 |
bgneal@10 | 16 |
bgneal@10 | 17 if __name__ == '__main__': |
bgneal@10 | 18 try: |
bgneal@10 | 19 for name in generate_identifier(): |
bgneal@10 | 20 print name |
bgneal@10 | 21 except KeyboardInterrupt: |
bgneal@10 | 22 print "Control-C interrupt" |