-
Notifications
You must be signed in to change notification settings - Fork 3
/
Map.py
66 lines (50 loc) · 2.47 KB
/
Map.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
class Map():
"""Maps are undirected graphs whose nodes have locations and connections"""
def __init__(self, connections_file_string, locations_file_string):
"""Set up the map created by the passed in connections and locations files"""
self.connections_dictionary = {}
self.locations_dictionary = {}
# construct a dictionary mapping a node to its connections list
connections_file = open(connections_file_string, 'r')
for line in connections_file:
stripped_line = line.rstrip('\n')
split_line = stripped_line.split()
self.connections_dictionary[split_line[0]] = split_line[2:]
del self.connections_dictionary["END"]
# construct a dictionary mapping a node to its location
locations_file = open(locations_file_string, 'r')
for line in locations_file:
stripped_line = line.rstrip('\n')
split_line = stripped_line.split()
self.locations_dictionary[split_line[0]] = split_line[1:]
del self.locations_dictionary["END"]
def print_connections(self):
"""Prints all nodes and their connections"""
for key, value in self.connections_dictionary.items():
print("Node: " + key + "\tConnections: %s" % value)
def print_locations(self):
"""Prints all nodes and their locations"""
for key, value in self.locations_dictionary.items():
print("Node: " + key + "\tLocation: %s" % value)
def print_all(self):
"""Prints all nodes' locations and connections"""
for key, value in self.locations_dictionary.items():
connections = self.connections_dictionary[key]
print("Node:" + key + " \t\tLocation:" + str(value) +
"\t\tConnections:" + str(connections))
def exclude_node(self, excluded_node):
excluded_connections = self.connections_dictionary[excluded_node]
del self.connections_dictionary[excluded_node]
del self.locations_dictionary[excluded_node]
for node in excluded_connections:
target_list = self.connections_dictionary[node]
target_list.remove(excluded_node)
def main():
my_map = Map("connections.txt", "locations.txt")
my_map.print_all()
print("************************************************************")
my_map2 = Map("connections.txt", "locations.txt")
my_map2.exclude_node("A1")
my_map2.print_all()
if __name__ == "__main__":
main()