Mercurial > public > think_complexity
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:9f1fccc13991 | 10:aea27d10dd18 |
---|---|
1 """Chapter 2.7, exercise 7. | |
2 | |
3 Write a generator that yields an infinite sequence of alpha-numeric identifiers, | |
4 starting with a1 through z1, then a2 through z2, and so on. | |
5 | |
6 """ | |
7 import string | |
8 | |
9 def generate_identifier(): | |
10 num = 1 | |
11 while True: | |
12 for c in string.lowercase: | |
13 yield c + str(num) | |
14 num += 1 | |
15 | |
16 | |
17 if __name__ == '__main__': | |
18 try: | |
19 for name in generate_identifier(): | |
20 print name | |
21 except KeyboardInterrupt: | |
22 print "Control-C interrupt" |