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

Skip to content

Commit e8eecb0

Browse files
committed
Fixed node.solution() to skip the root node.
1 parent 4138da5 commit e8eecb0

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

search.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def child_node(self, problem, action):
8787

8888
def solution(self):
8989
"Return the sequence of actions to go from the root to this node."
90-
return [node.action for node in self.path()]
90+
return [node.action for node in self.path()[1:]]
9191

9292
def path(self):
9393
"Return a list of nodes forming the path from the root to this node."
@@ -806,22 +806,20 @@ def compare_graph_searchers():
806806

807807
__doc__ += """
808808
>>> ab = GraphProblem('A', 'B', romania)
809-
>>> breadth_first_tree_search(ab).state
810-
'B'
811-
>>> breadth_first_graph_search(ab).state
812-
'B'
813-
>>> depth_first_graph_search(ab).state
814-
'B'
815-
>>> iterative_deepening_search(ab).state
816-
'B'
817-
>>> depth_limited_search(ab).state
818-
'B'
819-
>>> astar_search(ab).state
820-
'B'
821-
>>> recursive_best_first_search(ab).state
822-
'B'
823-
>>> [node.state for node in astar_search(ab).path()]
824-
['A', 'S', 'R', 'P', 'B']
809+
>>> breadth_first_tree_search(ab).solution()
810+
['S', 'F', 'B']
811+
>>> breadth_first_graph_search(ab).solution()
812+
['S', 'F', 'B']
813+
>>> depth_first_graph_search(ab).solution()
814+
['T', 'L', 'M', 'D', 'C', 'R', 'S', 'F', 'B']
815+
>>> iterative_deepening_search(ab).solution()
816+
['S', 'F', 'B']
817+
>>> len(depth_limited_search(ab).solution())
818+
50
819+
>>> astar_search(ab).solution()
820+
['S', 'R', 'P', 'B']
821+
>>> recursive_best_first_search(ab).solution()
822+
['S', 'R', 'P', 'B']
825823
826824
>>> board = list('SARTELNID')
827825
>>> print_boggle(board)

0 commit comments

Comments
 (0)