-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (57 loc) · 1.82 KB
/
main.py
File metadata and controls
71 lines (57 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
The main function. Assume all characters are in ASCII.
"""
import sys, random
import algorithms
from url import URL
def get_strings(f):
strings = []
line = f.readline()
while len(line) > 0:
# do not add empty line
if len(line) > 1:
strings.append(line[:len(line) - 1])
line = f.readline()
return strings
if __name__ == "__main__":
filename = None
if len(sys.argv) not in (3,4):
print 'Usage: python main.py [--valid|--invalid] input-file output-file'
exit(1)
inputfile = None
if len(sys.argv) == 3:
inputfile = open(sys.argv[1])
else:
# If optional flag is set
inputfile = open(sys.argv[2])
strings = get_strings(inputfile)
urls = [URL(x) for x in strings]
if len(sys.argv) == 4:
if sys.argv[1] == '--valid':
urls = [x for x in urls if x.isValid()]
else:
urls = [x for x in urls if not x.isValid()]
algorithm_tuples = algorithms.GetAlgorithms()
print 'Which sorting algorithm would you like?'
for (i, tup) in enumerate(algorithm_tuples):
print str(i) + ") " + tup[0] + ". Runtime: " + tup[1]
option = None
while option is None or (option >= len(algorithm_tuples) or option < 0):
sorting_method = raw_input('Enter an option number: ')
try:
option = int(sorting_method)
if option < 0 or option >= len(algorithm_tuples):
raise ValueError('Option out of range')
except ValueError:
print 'Invalid input %s. Please enter the integer before each option.' % sorting_method
outputfile = None
if len(sys.argv) == 3:
outputfile = open(sys.argv[2], 'w+')
else:
outputfile = open(sys.argv[3], 'w+')
results = algorithms.RunAlgorithm(option, urls)
for item in results:
outputfile.write('%s\n' % item.url)
print 'Sorting with \'%s\' successfully.\n' %option
inputfile.close()
outputfile.close()