Python search

From MDWiki
Jump to navigationJump to search

Example files for a pattern file

pattern0.txt


Stub of python code to time string search code

import sys, string, time


###
### insert naiveStringSearch routine
###


# check if enough arguments are supplied
if (len(sys.argv) < 3):
    sys.stderr.write("usage: python -i %s <text file> <pattern file>\n\n" % sys.argv[0])
    sys.exit()


# read the text from the file given as the first argument to the program

f = open(sys.argv[1])
text =f.read()
f.close()

# read the pattern from the file given as the second argument to the program

f = open(sys.argv[2])
pattern =f.read()
f.close()



t0 = time.clock()
pos1 = naiveStringSearch(text, pattern)
t1 = time.clock()
print "naive search:"
print "   position where pattern matches: %i" % pos1
print "   time for search               : %.3f s" % (t1-t0)


t2 = time.clock()
       #####
pos2 = ##### insert string search call to string.find routine
       #####
t3 = time.clock()
print "python search:"
print "   position where pattern matches: %i" % pos2
print "   time for search               : %.3f s" % (t3-t2)

sys.exit()