forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.py
50 lines (39 loc) · 1.43 KB
/
sol.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
class Map:
def __init__(self):
self.map = {}
@property
def opp_dir(self):
return {'N': 'S', 'S': 'N', 'E': 'W', 'W': 'E'}
def isValid(self) -> bool:
for v in self.map:
v_map = self.map[v]
if not set(v_map['N']).isdisjoint(v_map['S']):
return False
if not set(v_map['E']).isdisjoint(v_map['W']):
return False
return True
def validateRules(rules: "list(str)") -> bool:
map = Map()
for rule in rules:
node1, dir, node2 = rule.split()
print("%s is to the %s of %s" %(node1, dir, node2))
if node1 not in map.map:
map.map[node1] = {'N': [], 'S': [], 'W': [], 'E': []}
for d in dir:
map.map[node1][d].append(node2)
for node in map.map[node1][d]:
if node in map.map:
map.map[node][d].append(node2)
if node2 not in map.map:
map.map[node2] = {'N': [], 'S': [], 'W': [], 'E': []}
for d in dir:
map.map[node2][map.opp_dir[d]].append(node1)
for node in map.map[node2][map.opp_dir[d]]:
if node in map.map:
map.map[node][map.opp_dir[d]].append(node1)
if not map.isValid():
return False
return True
if __name__=='__main__':
print(validateRules(["A N B", "B NE C", "C N A"]))
print(validateRules(["A NW B", "A N B"]))