diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ch2ex7.py	Mon Dec 03 19:49:23 2012 -0600
@@ -0,0 +1,22 @@
+"""Chapter 2.7, exercise 7.
+
+Write a generator that yields an infinite sequence of alpha-numeric identifiers,
+starting with a1 through z1, then a2 through z2, and so on.
+
+"""
+import string
+
+def generate_identifier():
+    num = 1
+    while True:
+        for c in string.lowercase:
+            yield c + str(num)
+        num += 1
+
+
+if __name__ == '__main__':
+    try:
+        for name in generate_identifier():
+            print name
+    except KeyboardInterrupt:
+        print "Control-C interrupt"