Deadline 2022-02-22 Thuesday 24:00
python3 1AdrianSmith.py text.text pattern
The program reads the file text.text and search for the first occurrence of the given pattern in it. The command arguments can be read out from the inbuilt array sys.argv:
import sys print(f'Filename: {sys.argv[1]}, pattern: {sys.argv[2]}') print(f"The name of this program has {len(sys.argv[0]):2} characters")
The sys.argv[0] contains the name of the program, the len() function gives the length of it.
with open(filename) as f: text = f.read().strip()
The strip() method cut down the unnecessary starting and ending white spaces.
Simple search: 2610 comparisons, the pattern starts at 2321 Quick search: 270 comparisons, the pattern starts at 2321
Specify the number of comparisons even if the pattern does not appear in the text:
Simple search: 53610 comparisons, no pattern Quick search: 5470 comparisons, no pattern
File | Pattern |
---|---|
1.text | overscrupulous |
1.text | zombies |
2.text | aaaaaaaaa |
2.text | bbbbbbbbb |
2.text | aaaaaaaab |
2.text | aaaaaaaba |