Kaydet (Commit) b7878d09 authored tarafından Andrew M. Kuchling's avatar Andrew M. Kuchling

Modernize the code a bit:

   use re module
   use .split() string method

Doesn't use 'for line in sys.stdin'; that ends up changing its interactive
behaviour.
üst 946c53ed
...@@ -23,15 +23,16 @@ ...@@ -23,15 +23,16 @@
# - Outputs the sorted fields with exactly one space between them # - Outputs the sorted fields with exactly one space between them
# - Handles blank input lines correctly # - Handles blank input lines correctly
import regex import re
import string import string
import sys import sys
def main(): def main():
prog = regex.compile('^\(.*\)=\([-+]?[0-9]+\)') prog = re.compile('^(.*)=([-+]?[0-9]+)')
def makekey(item, prog=prog): def makekey(item, prog=prog):
if prog.match(item) >= 0: match = prog.match(item)
var, num = prog.group(1, 2) if match:
var, num = match.group(1, 2)
return string.atoi(num), var return string.atoi(num), var
else: else:
# Bad input -- pretend it's a var with value 0 # Bad input -- pretend it's a var with value 0
...@@ -40,7 +41,7 @@ def main(): ...@@ -40,7 +41,7 @@ def main():
line = sys.stdin.readline() line = sys.stdin.readline()
if not line: if not line:
break break
items = string.split(line) items = line.split()
items = map(makekey, items) items = map(makekey, items)
items.sort() items.sort()
for num, var in items: for num, var in items:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment