Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21bc15b

Browse files
committed
commit -- why not...
1 parent f4449de commit 21bc15b

5 files changed

Lines changed: 238 additions & 0 deletions

File tree

Demo/comparisons/README

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Subject: Re: What language would you use?
2+
From: Tom Christiansen <[email protected]>
3+
Date: 6 Nov 1994 15:14:51 GMT
4+
Newsgroups: comp.lang.python,comp.lang.tcl,comp.lang.scheme,comp.lang.misc,comp.lang.perl
5+
Message-Id: <[email protected]>
6+
7+
8+
[...]
9+
If you're really into benchmarks, I'd love it if someone were to code up
10+
the following problems in tcl, python, and scheme (and whatever else you'd
11+
like). Separate versions (one optimized for speed, one for beauty :-) are
12+
ok. Post your code so we can time it on our own systems.
13+
14+
0) Factorial Test (numerics and function calls)
15+
16+
(we did this already)
17+
18+
1) Regular Expressions Test
19+
20+
Read a file of (extended per egrep) regular expressions (one per line),
21+
and apply those to all files whose names are listed on the command line.
22+
Basically, an 'egrep -f' simulator. Test it with 20 "vt100" patterns
23+
against a five /etc/termcap files. Tests using more elaborate patters
24+
would also be interesting. Your code should not break if given hundreds
25+
of regular expressions or binary files to scan.
26+
27+
2) Sorting Test
28+
29+
Sort an input file that consists of lines like this
30+
31+
var1=23 other=14 ditto=23 fred=2
32+
33+
such that each output line is sorted WRT to the number. Order
34+
of output lines does not change. Resolve collisions using the
35+
variable name. e.g.
36+
37+
fred=2 other=14 ditto=23 var1=23
38+
39+
Lines may be up to several kilobytes in length and contain
40+
zillions of variables.
41+
42+
3) System Test
43+
44+
Given a list of directories, report any bogus symbolic links contained
45+
anywhere in those subtrees. A bogus symbolic link is one that cannot
46+
be resolved because it points to a nonexistent or otherwise
47+
unresolvable file. Do *not* use an external find executable.
48+
Directories may be very very deep. Print a warning immediately if the
49+
system you're running on doesn't support symbolic links.
50+
51+
52+
I'll post perl solutions if people post the others.
53+
54+
55+
--tom
56+
--
57+
Tom Christiansen Perl Consultant, Gamer, Hiker [email protected]
58+
59+
"But Billy! A *small* allowance prepares you for a lifetime of small
60+
salaries and for your Social Security payments." --Family Circus

Demo/comparisons/patterns

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
^def
2+
^class
3+
^import
4+
^from

Demo/comparisons/regextest.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#! /usr/local/bin/python
2+
3+
# 1) Regular Expressions Test
4+
#
5+
# Read a file of (extended per egrep) regular expressions (one per line),
6+
# and apply those to all files whose names are listed on the command line.
7+
# Basically, an 'egrep -f' simulator. Test it with 20 "vt100" patterns
8+
# against a five /etc/termcap files. Tests using more elaborate patters
9+
# would also be interesting. Your code should not break if given hundreds
10+
# of regular expressions or binary files to scan.
11+
12+
# This implementation:
13+
# - combines all patterns into a single one using ( ... | ... | ... )
14+
# - reads patterns from stdin, scans files given as command line arguments
15+
# - produces output in the format <file>:<lineno>:<line>
16+
# - is only about 2.5 times as slow as egrep (though I couldn't run
17+
# Tom's test -- this system, a vanilla SGI, only has /etc/terminfo)
18+
19+
import string
20+
import sys
21+
import regex
22+
from regex_syntax import *
23+
24+
regex.set_syntax(RE_SYNTAX_EGREP)
25+
26+
def main():
27+
pats = map(chomp, sys.stdin.readlines())
28+
bigpat = '(' + string.joinfields(pats, '|') + ')'
29+
prog = regex.compile(bigpat)
30+
31+
for file in sys.argv[1:]:
32+
try:
33+
fp = open(file, 'r')
34+
except IOError, msg:
35+
print "%s: %s" % (file, msg)
36+
continue
37+
lineno = 0
38+
while 1:
39+
line = fp.readline()
40+
if not line:
41+
break
42+
lineno = lineno + 1
43+
if prog.search(line) >= 0:
44+
print "%s:%s:%s" % (file, lineno, line),
45+
46+
def chomp(s):
47+
if s[-1:] == '\n': return s[:-1]
48+
else: return s
49+
50+
main()

Demo/comparisons/sortingtest.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#! /usr/local/bin/python
2+
3+
# 2) Sorting Test
4+
#
5+
# Sort an input file that consists of lines like this
6+
#
7+
# var1=23 other=14 ditto=23 fred=2
8+
#
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.
12+
#
13+
# fred=2 other=14 ditto=23 var1=23
14+
#
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+
26+
import regex
27+
import string
28+
import sys
29+
30+
def main():
31+
prog = regex.compile('^\(.*\)=\([-+]?[0-9]+\)')
32+
def makekey(item, prog=prog):
33+
if prog.match(item) >= 0:
34+
var, num = prog.group(1, 2)
35+
return string.atoi(num), var
36+
else:
37+
# Bad input -- pretend it's a var with value 0
38+
return 0, item
39+
while 1:
40+
line = sys.stdin.readline()
41+
if not line:
42+
break
43+
items = string.split(line)
44+
items = map(makekey, items)
45+
items.sort()
46+
for num, var in items:
47+
print "%s=%s" % (var, num),
48+
print
49+
50+
main()

Demo/comparisons/systemtest.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#! /usr/local/bin/python
2+
3+
# 3) System Test
4+
#
5+
# Given a list of directories, report any bogus symbolic links contained
6+
# anywhere in those subtrees. A bogus symbolic link is one that cannot
7+
# be resolved because it points to a nonexistent or otherwise
8+
# unresolvable file. Do *not* use an external find executable.
9+
# Directories may be very very deep. Print a warning immediately if the
10+
# system you're running on doesn't support symbolic links.
11+
12+
# This implementation:
13+
# - takes one optional argument, using the current directory as default
14+
# - uses chdir to increase performance
15+
# - sorts the names per directory
16+
# - prints output lines of the form "path1 -> path2" as it goes
17+
# - prints error messages about directories it can't list or chdir into
18+
19+
import os
20+
import sys
21+
from stat import *
22+
23+
def main():
24+
try:
25+
# Note: can't test for presence of lstat -- it's always there
26+
dummy = os.readlink
27+
except AttributeError:
28+
print "This system doesn't have symbolic links"
29+
sys.exit(0)
30+
if sys.argv[1:]:
31+
prefix = sys.argv[1]
32+
else:
33+
prefix = ''
34+
if prefix:
35+
os.chdir(prefix)
36+
if prefix[-1:] != '/': prefix = prefix + '/'
37+
reportboguslinks(prefix)
38+
else:
39+
reportboguslinks('')
40+
41+
def reportboguslinks(prefix):
42+
try:
43+
names = os.listdir('.')
44+
except os.error, msg:
45+
print "%s%s: can't list: %s" % (prefix, '.', msg)
46+
return
47+
names.sort()
48+
for name in names:
49+
if name == os.curdir or name == os.pardir:
50+
continue
51+
try:
52+
mode = os.lstat(name)[ST_MODE]
53+
except os.error:
54+
print "%s%s: can't stat: %s" % (prefix, name, msg)
55+
continue
56+
if S_ISLNK(mode):
57+
try:
58+
os.stat(name)
59+
except os.error:
60+
print "%s%s -> %s" % \
61+
(prefix, name, os.readlink(name))
62+
elif S_ISDIR(mode):
63+
try:
64+
os.chdir(name)
65+
except os.error, msg:
66+
print "%s%s: can't chdir: %s" % \
67+
(prefix, name, msg)
68+
continue
69+
try:
70+
reportboguslinks(prefix + name + '/')
71+
finally:
72+
os.chdir('..')
73+
74+
main()

0 commit comments

Comments
 (0)