Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 204d161

Browse files
committed
Fixed path() to follow its doc comment (first-to-last order).
1 parent 81acbd4 commit 204d161

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

search.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ def __repr__(self):
7474

7575
def path(self):
7676
"""Create a list of nodes from the root to this node."""
77-
# Isn't this backwards???
78-
x, result = self, [self]
79-
while x.parent:
80-
result.append(x.parent)
81-
x = x.parent
82-
return result
77+
node, path_back = self, []
78+
while node:
79+
path_back.append(node)
80+
node = node.parent
81+
return list(reversed(path_back))
8382

8483
def expand(self, problem):
8584
"Return a list of nodes reachable from this node. [Fig. 3.8]"
@@ -778,7 +777,7 @@ def compare_graph_searchers():
778777
>>> recursive_best_first_search(ab).state
779778
'B'
780779
>>> [node.state for node in astar_search(ab).path()]
781-
['B', 'P', 'R', 'S', 'A']
780+
['A', 'S', 'R', 'P', 'B']
782781
783782
>>> board = list('SARTELNID')
784783
>>> print_boggle(board)

0 commit comments

Comments
 (0)