Mercurial > cpython-lru_cache-weakref
annotate Demo/comparisons/sortingtest.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 015d891f3873 |
| children | 97d7cd365dd5 7d16b42fcdbd |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
2651
diff
changeset
|
1 #! /usr/bin/env python |
| 2651 | 2 |
| 3 # 2) Sorting Test | |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
4 # |
| 2651 | 5 # Sort an input file that consists of lines like this |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
6 # |
| 2651 | 7 # var1=23 other=14 ditto=23 fred=2 |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
8 # |
| 2651 | 9 # such that each output line is sorted WRT to the number. Order |
| 10 # of output lines does not change. Resolve collisions using the | |
| 11 # variable name. e.g. | |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
12 # |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
13 # fred=2 other=14 ditto=23 var1=23 |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
14 # |
| 2651 | 15 # Lines may be up to several kilobytes in length and contain |
| 16 # zillions of variables. | |
| 17 | |
| 18 # This implementation: | |
| 19 # - Reads stdin, writes stdout | |
| 20 # - Uses any amount of whitespace to separate fields | |
| 21 # - Allows signed numbers | |
| 22 # - Treats illegally formatted fields as field=0 | |
| 23 # - Outputs the sorted fields with exactly one space between them | |
| 24 # - Handles blank input lines correctly | |
| 25 | |
|
28789
015d891f3873
Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents:
28788
diff
changeset
|
26 import re |
| 2651 | 27 import string |
| 28 import sys | |
| 29 | |
| 30 def main(): | |
|
28789
015d891f3873
Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents:
28788
diff
changeset
|
31 prog = re.compile('^(.*)=([-+]?[0-9]+)') |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
32 def makekey(item, prog=prog): |
|
28789
015d891f3873
Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents:
28788
diff
changeset
|
33 match = prog.match(item) |
|
015d891f3873
Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents:
28788
diff
changeset
|
34 if match: |
|
015d891f3873
Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents:
28788
diff
changeset
|
35 var, num = match.group(1, 2) |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
36 return string.atoi(num), var |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
37 else: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
38 # Bad input -- pretend it's a var with value 0 |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
39 return 0, item |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
40 while 1: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
41 line = sys.stdin.readline() |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
42 if not line: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
43 break |
|
28789
015d891f3873
Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents:
28788
diff
changeset
|
44 items = line.split() |
|
28788
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
45 items = map(makekey, items) |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
46 items.sort() |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
47 for num, var in items: |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
48 print "%s=%s" % (var, num), |
|
558689b6d7f0
Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents:
4573
diff
changeset
|
49 print |
| 2651 | 50 |
| 51 main() |
