-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySet.py
105 lines (82 loc) · 2.77 KB
/
MySet.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from Path import Path
import pickle
import json
class MySet(set):
def __init__(self):
self.paths = list()
self.visited = list()
self.min = None
super().__init__()
def add(self, path: Path):
found = False
# for each of the paths we have so far discovered(visited or not)
for p in self.paths:
# if the path's starting and ending nodes coincide with p's ones
if path == p:
# if path is cheapest than p -> add it
if path < p:
self.paths.remove(p)
self.paths.append(path)
return
else:
found = True
break
# if path's and p's starting and ending nodes do not coincide with p's -> add it
if not found:
self.paths.append(path)
# self.write_paths()
def pop(self):
# the last node of the path we are about to pop is now considered visited
self.visited.append(self.min.last)
# self.write_visited()
# return the next unvisited cheapest path
return self.min
def set_min(self):
# get the unvisited paths
not_visited_paths = list(filter(lambda x: not(x.last in self.visited), self.paths))
if not not_visited_paths:
return
# find the cheapest of them
self.min = min(not_visited_paths)
def has_next(self):
# get the last nodes of all paths in our path list
last_nodes = set(map(lambda x: x.last, self.paths))
# check if the last_nodes set is a subset of the visited set
diff = last_nodes.difference(set(self.visited))
# if it is then there are no more unvisited paths
return not(diff.__len__() == 0)
def is_visited(self, url):
visited_pages = list(map(lambda x: x.page, self.visited))
return url in visited_pages
def print(self):
print()
print("Min = {}".format(self.min))
print("Paths{")
for p in self.paths:
print(p)
print("}")
print("Visited{")
for p in self.visited:
print(p)
print("}")
def __str__(self):
s = "Paths{"
for p in self.paths:
s += str(p) + "\n"
s += "}\n"
s += "Visited{"
for p in self.visited:
s += str(p) + "\n"
s += "}"
return s
def to_json(self):
s = {"paths": list(map(lambda x: x.to_json(), self.paths))}
return json.dumps(s)
def write_visited(self):
f = open("col_visited", "bw")
pickle.dump(self.visited, f)
f.close()
def write_paths(self):
f = open("col_paths", "bw")
pickle.dump(self.paths, f)
f.close()