cpython/Demo/comparisons/sortingtest.py

46 lines
1.2 KiB
Python
Raw Normal View History

#! /usr/bin/env python
1995-04-10 08:40:26 -03:00
# 2) Sorting Test
#
1995-04-10 08:40:26 -03:00
# Sort an input file that consists of lines like this
#
1995-04-10 08:40:26 -03:00
# var1=23 other=14 ditto=23 fred=2
#
1995-04-10 08:40:26 -03:00
# such that each output line is sorted WRT to the number. Order
# of output lines does not change. Resolve collisions using the
# variable name. e.g.
#
# fred=2 other=14 ditto=23 var1=23
#
1995-04-10 08:40:26 -03:00
# Lines may be up to several kilobytes in length and contain
# zillions of variables.
# This implementation:
# - Reads stdin, writes stdout
# - Uses any amount of whitespace to separate fields
# - Allows signed numbers
# - Treats illegally formatted fields as field=0
# - Outputs the sorted fields with exactly one space between them
# - Handles blank input lines correctly
import re
1995-04-10 08:40:26 -03:00
import sys
def main():
prog = re.compile('^(.*)=([-+]?[0-9]+)')
def makekey(item, prog=prog):
match = prog.match(item)
if match:
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r73206 | georg.brandl | 2009-06-04 11:15:12 +0200 (Do, 04 Jun 2009) | 1 line #3584: ignore trailing newlines when placing the caret for a SyntaxError location. ........ r73232 | georg.brandl | 2009-06-04 20:59:58 +0200 (Do, 04 Jun 2009) | 1 line Add test for #3684. ........ r73299 | georg.brandl | 2009-06-08 20:41:36 +0200 (Mo, 08 Jun 2009) | 1 line Typo fix. ........ r73683 | georg.brandl | 2009-06-29 16:44:49 +0200 (Mo, 29 Jun 2009) | 1 line Fix error handling in PyCode_Optimize, by Alexander Schremmer at EuroPython sprint. ........ r74020 | georg.brandl | 2009-07-16 09:18:07 +0200 (Do, 16 Jul 2009) | 1 line #5910: fix kqueue for calls with more than one event. ........ r74185 | georg.brandl | 2009-07-23 11:17:09 +0200 (Do, 23 Jul 2009) | 1 line Fix the "pylocals" gdb command. ........ r74544 | georg.brandl | 2009-08-24 19:12:30 +0200 (Mo, 24 Aug 2009) | 1 line #6775: fix python.org URLs in README. ........ r74643 | georg.brandl | 2009-09-04 08:59:20 +0200 (Fr, 04 Sep 2009) | 2 lines Issue #2666: Handle BROWSER environment variable properly for unknown browser names in the webbrowser module. ........ r74647 | georg.brandl | 2009-09-04 10:17:04 +0200 (Fr, 04 Sep 2009) | 2 lines Issue #5275: In Cookie's Cookie.load(), properly handle non-string arguments as documented. ........ r74817 | georg.brandl | 2009-09-16 11:05:11 +0200 (Mi, 16 Sep 2009) | 1 line Make deprecation notices as visible as warnings are right now. ........ r74838 | georg.brandl | 2009-09-16 18:22:12 +0200 (Mi, 16 Sep 2009) | 1 line Remove some more boilerplate from the actual tests in test_pdb. ........ r74839 | georg.brandl | 2009-09-16 18:36:39 +0200 (Mi, 16 Sep 2009) | 1 line Make the pdb displayhook compatible with the standard displayhook: do not print Nones. Add a test for that. ........ r74865 | georg.brandl | 2009-09-17 09:49:37 +0200 (Do, 17 Sep 2009) | 1 line #6912: add "with" block support to pindent. ........ r74946 | georg.brandl | 2009-09-19 10:43:16 +0200 (Sa, 19 Sep 2009) | 1 line Update bug tracker reference. ........ r75402 | georg.brandl | 2009-10-14 17:51:48 +0200 (Mi, 14 Okt 2009) | 1 line #7125: fix typo. ........ r75459 | georg.brandl | 2009-10-17 10:57:43 +0200 (Sa, 17 Okt 2009) | 1 line Fix refleaks in _ctypes PyCSimpleType_New, which fixes the refleak seen in test___all__. ........ r75604 | georg.brandl | 2009-10-22 13:36:50 +0200 (Do, 22 Okt 2009) | 1 line Fix stylesheet for multi-paragraph impl-details. ........ r75696 | georg.brandl | 2009-10-25 21:25:43 +0100 (So, 25 Okt 2009) | 1 line Fix a demo. ........
2009-10-27 12:39:53 -03:00
var, num = match.groups()
return int(num), var
else:
# Bad input -- pretend it's a var with value 0
return 0, item
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r73206 | georg.brandl | 2009-06-04 11:15:12 +0200 (Do, 04 Jun 2009) | 1 line #3584: ignore trailing newlines when placing the caret for a SyntaxError location. ........ r73232 | georg.brandl | 2009-06-04 20:59:58 +0200 (Do, 04 Jun 2009) | 1 line Add test for #3684. ........ r73299 | georg.brandl | 2009-06-08 20:41:36 +0200 (Mo, 08 Jun 2009) | 1 line Typo fix. ........ r73683 | georg.brandl | 2009-06-29 16:44:49 +0200 (Mo, 29 Jun 2009) | 1 line Fix error handling in PyCode_Optimize, by Alexander Schremmer at EuroPython sprint. ........ r74020 | georg.brandl | 2009-07-16 09:18:07 +0200 (Do, 16 Jul 2009) | 1 line #5910: fix kqueue for calls with more than one event. ........ r74185 | georg.brandl | 2009-07-23 11:17:09 +0200 (Do, 23 Jul 2009) | 1 line Fix the "pylocals" gdb command. ........ r74544 | georg.brandl | 2009-08-24 19:12:30 +0200 (Mo, 24 Aug 2009) | 1 line #6775: fix python.org URLs in README. ........ r74643 | georg.brandl | 2009-09-04 08:59:20 +0200 (Fr, 04 Sep 2009) | 2 lines Issue #2666: Handle BROWSER environment variable properly for unknown browser names in the webbrowser module. ........ r74647 | georg.brandl | 2009-09-04 10:17:04 +0200 (Fr, 04 Sep 2009) | 2 lines Issue #5275: In Cookie's Cookie.load(), properly handle non-string arguments as documented. ........ r74817 | georg.brandl | 2009-09-16 11:05:11 +0200 (Mi, 16 Sep 2009) | 1 line Make deprecation notices as visible as warnings are right now. ........ r74838 | georg.brandl | 2009-09-16 18:22:12 +0200 (Mi, 16 Sep 2009) | 1 line Remove some more boilerplate from the actual tests in test_pdb. ........ r74839 | georg.brandl | 2009-09-16 18:36:39 +0200 (Mi, 16 Sep 2009) | 1 line Make the pdb displayhook compatible with the standard displayhook: do not print Nones. Add a test for that. ........ r74865 | georg.brandl | 2009-09-17 09:49:37 +0200 (Do, 17 Sep 2009) | 1 line #6912: add "with" block support to pindent. ........ r74946 | georg.brandl | 2009-09-19 10:43:16 +0200 (Sa, 19 Sep 2009) | 1 line Update bug tracker reference. ........ r75402 | georg.brandl | 2009-10-14 17:51:48 +0200 (Mi, 14 Okt 2009) | 1 line #7125: fix typo. ........ r75459 | georg.brandl | 2009-10-17 10:57:43 +0200 (Sa, 17 Okt 2009) | 1 line Fix refleaks in _ctypes PyCSimpleType_New, which fixes the refleak seen in test___all__. ........ r75604 | georg.brandl | 2009-10-22 13:36:50 +0200 (Do, 22 Okt 2009) | 1 line Fix stylesheet for multi-paragraph impl-details. ........ r75696 | georg.brandl | 2009-10-25 21:25:43 +0100 (So, 25 Okt 2009) | 1 line Fix a demo. ........
2009-10-27 12:39:53 -03:00
for line in sys.stdin:
items = sorted(makekey(item) for item in line.split())
for num, var in items:
print "%s=%s" % (var, num),
print
1995-04-10 08:40:26 -03:00
main()