-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
67 lines (48 loc) · 1.5 KB
/
util.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
from typing import List, Optional
class State:
def __init__(self, row: int, column: int):
self._items = (row, column)
def __getitem__(self, index: int) -> int:
return self._items[index]
def __len__(self) -> int:
return len(self._items)
def __repr__(self) -> str:
return repr(self._items)
def __iter__(self) -> iter:
return iter(self._items)
def __eq__(self, other) -> bool:
if isinstance(other, State):
return self._items == other._items
return False
def __hash__(self) -> int:
return hash(self._items)
class Action:
def __init__(self, name: str, state: State) -> None:
self.name = name
self.state = state
def __iter__(self) -> iter:
return iter((self.name, self.state))
class Node:
def __init__(
self,
state: State,
parent: Optional['Node'],
action: str
) -> None:
self.state = state
self.parent = parent
self.action = action
class StackFrontier:
def __init__(self):
self.frontier: List[Node] = []
def add(self, node: Node) -> None:
self.frontier.append(node)
def contains_state(self, state) -> None:
return any(node.state == state for node in self.frontier)
def empty(self) -> bool:
return len(self.frontier) == 0
def remove(self) -> Node:
if self.empty():
raise Exception("Empty frontier.")
else:
return self.frontier.pop()