can you change this code from using if statements to using switc state: def move(table,…
- User
can you change this code from using if statements to using switc state: def move(table, tile, move): table = table.copy() temp = table[tile] if move == "nomove": return table elif move == "switch1": table[tile] = table[tile+1] table[tile+1] = temp elif move == "switch2": table[tile] = table[tile-1] table[tile-1] = temp elif move == "leap1": table[tile] = table[tile+2] table[tile+2] = temp elif move == "leap2": table[tile] = table[tile-2] table[tile-2] = temp
codereturn table - Assistant
def move(table, tile, move): table = table.copy() temp = table[tile] switch = { "nomove": lambda: table, "switch1": lambda: (table.setitem(tile, table[tile+1]), table.setitem(tile+1, temp)), "switch2": lambda: (table.setitem(tile, table[tile-1]), table.setitem(tile-1, temp)), "leap1": lambda: (table.setitem(tile, table[tile+2]), table.setitem(tile+2, temp)), "leap2": lambda: (table.setitem(tile, table[tile-2]), table.setitem(tile-2, temp)) } switch.get(move, lambda: table)() return table
- User
i get this error : C:\Users\Joan Shameti\Desktop\Ai Homework>py frog.py Please insert the size of the board: 2 Traceback (most recent call last): File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 107, in dfs(table, n) File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 96, in dfs table_copy = do_move(table, i) ^^^^^^^^^^^^^^^^^ File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 73, in do_move table_copy = move(table_copy, tile, move_type) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 35, in move return table() ^^^^^^^ TypeError: 'list' object is not callable
- Assistant
It looks like the error is due to the
return table()statement at the end of themovefunction. This should be changed toreturn table. The corrected code should be:codedef move(table, tile, move): table = table.copy() temp = table[tile] switch = { "nomove": lambda: table, "switch1": lambda: (table.__setitem__(tile, table[tile+1]), table.__setitem__(tile+1, temp)), "switch2": lambda: (table.__setitem__(tile, table[tile-1]), table.__setitem__(tile-1, temp)), "leap1": lambda: (table.__setitem__(tile, table[tile+2]), table.__setitem__(tile+2, temp)), "leap2": lambda: (table.__setitem__(tile, table[tile-2]), table.__setitem__(tile-2, temp)) } switch.get(move, lambda: table)() return table - User
now i get tthis other one File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 107, in dfs(table, n) File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 97, in dfs if dfs(table_copy, size, moves_list): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 97, in dfs if dfs(table_copy, size, moves_list): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 97, in dfs if dfs(table_copy, size, moves_list): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [Previous line repeated 993 more times] File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 88, in dfs if win_state(table, size): ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Joan Shameti\Desktop\Ai Homework\frog.py", line 66, in win_state if table == gridWin: ^^^^^^^^^^^^^^^^ RecursionError: maximum recursion depth exceeded in comparison
- Assistant
This error is caused by exceeding the maximum recursion limit of Python. The DFS (depth-first search) algorithm you are using might be too inefficient due to the large search space of the problem.
You can try increasing the recursion limit using the
sysmodule:codeimport sys sys.setrecursionlimit(10**6)However, it is not recommended to use this approach for large problems because it might lead to a stack overflow or cause the program to hang.
A better solution is to use an iterative algorithm or an efficient search algorithm such as BFS (breadth-first search) or A* search.
- User
this is my dfs implementation can you fix it for me : def dfs(table, size, moves_list=None): if moves_list is None: moves_list = [] moves_list.append(table)
codeif win_state(table, size): print("Final moves to solve:") print(*moves_list, sep="\n") return True for i in range(len(table)): move_type = possible_move(table, i) if move_type != "nomove": table_copy = do_move(table, i) if dfs(table_copy, size, moves_list): return True del moves_list[-1] return False - Assistant
Here's your fixed
dfsfunction using iterative deepening:codedef dfs(table, size): for depth_limit in range(size ** 2): visited = [] stack = [(table, [])] while stack: node, moves_list = stack.pop() if node in visited: continue visited.append(node) if win_state(node, size): print("Final moves to solve:") print(*moves_list, sep="\n") return True if len(moves_list) < depth_limit: for i in range(len(node)): move_type = possible_move(node, i) if move_type != "nomove": table_copy = do_move(node, i) stack.append((table_copy, moves_list + [table_copy])) return False