diff options
| author | Georg Brandl <georg@python.org> | 2010-12-30 17:22:33 +0000 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2010-12-30 17:22:33 +0000 |
| commit | 4cf83f4d128bd40ebe3b6e59ced4895f554d18de (patch) | |
| tree | ccc6e4c3e03a711c45f4badf811314231d646d95 /Demo/comparisons/sortingtest.py | |
| parent | d1fc34d563a9fd06a78226b1bb4e56286c70e035 (diff) | |
| download | cpython-git-4cf83f4d128bd40ebe3b6e59ced4895f554d18de.tar.gz | |
Remove some of the old demos. (Put a few somewhere else.)
Diffstat (limited to 'Demo/comparisons/sortingtest.py')
| -rwxr-xr-x | Demo/comparisons/sortingtest.py | 45 |
1 files changed, 0 insertions, 45 deletions
diff --git a/Demo/comparisons/sortingtest.py b/Demo/comparisons/sortingtest.py deleted file mode 100755 index e826e81d65..0000000000 --- a/Demo/comparisons/sortingtest.py +++ /dev/null @@ -1,45 +0,0 @@ -#! /usr/bin/env python3 - -# 2) Sorting Test -# -# Sort an input file that consists of lines like this -# -# var1=23 other=14 ditto=23 fred=2 -# -# 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 -# -# 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 -import sys - -def main(): - prog = re.compile('^(.*)=([-+]?[0-9]+)') - def makekey(item, prog=prog): - match = prog.match(item) - if match: - var, num = match.groups() - return int(num), var - else: - # Bad input -- pretend it's a var with value 0 - return 0, item - for line in sys.stdin: - items = sorted(makekey(item) for item in line.split()) - for num, var in items: - print("%s=%s" % (var, num), end=' ') - print() - -main() |
