Diff
checker
Text
Text
Images
Documents
Excel
Folders
Legal
Enterprise
Desktop
Pricing
Sign in
Download Diffchecker Desktop
Compare text
Find the difference between two text files
Tools
History
Real-time editor
Hide unchanged lines
Disable line wrap
Layout
Split
Unified
Diff precision
Smart
Word
Char
Syntax highlighting
Choose syntax
Ignore
Transform text
Go to first change
Edit input
Diffchecker Desktop
The most secure way to run Diffchecker. Get the Diffchecker Desktop app: your diffs never leave your computer!
Get Desktop
MCS 275 2022 worksheet 7 question 3
Created
4 years ago
Diff never expires
Clear
Export
Share
Explain
9 removals
Lines
Total
Removed
Characters
Total
Removed
To continue using this feature, upgrade to
Diff
checker
Pro
View Pricing
34 lines
Copy
25 additions
Lines
Total
Added
Characters
Total
Added
To continue using this feature, upgrade to
Diff
checker
Pro
View Pricing
39 lines
Copy
Copy
Copied
Copy
Copied
def solvemaze
(M,path
=None
):
def solvemaze
_history
(M,path
_list
=None
, solved=False
):
"""
"""
Copy
Copied
Copy
Copied
Find a solution
to
maze `M`
Functions similarly
to
`solvemaze`, but returns a list of all paths
using a depth-first recursive
considered throughout solving process. Uses additional arg `solved`
search
.
to track whether solution has been found
.
"""
"""
Copy
Copied
Copy
Copied
print("Called with path={}".format(path))
if path
_list
==None:
if path
==None:
# no path specified, so start
# no path specified, so start
# at M.start
# at M.start
Copy
Copied
Copy
Copied
path
=
[M.start]
path
_list
=
[
[M.start]
] # Use a list of lists for path_list - one list for each path.
else:
print("Called with path={}".format(path_list[-1]))
path = path_list[-1]
if path[-1]==M.goal:
if path[-1]==M.goal:
# We've found a solution
# We've found a solution
print("SOLVED!")
print("SOLVED!")
Copy
Copied
Copy
Copied
return path
return path
_list, True # Set the "solved" variable to True
# next steps to consider
# next steps to consider
# (may include retracing our steps)
# (may include retracing our steps)
steps = M.free_neighbors(*path[-1])
steps = M.free_neighbors(*path[-1])
for s in steps:
for s in steps:
if len(path)>=2 and s == path[-2]:
if len(path)>=2 and s == path[-2]:
print("Not considering {}, as it would retrace our steps".format(s))
print("Not considering {}, as it would retrace our steps".format(s))
continue
continue
# Consider whether next step `s` leads to a solution
# Consider whether next step `s` leads to a solution
print("Considering next step {}".format(s))
print("Considering next step {}".format(s))
Copy
Copied
Copy
Copied
soln
= solvemaze
(M,path
+ [s]
)
next_path = path + [s]
if
soln != None
:
path_list.append(next_path)
soln
, solved
= solvemaze
_history
(M,path
_list
)
if
solved
:
# Some recursive call yielded a solution!
# Some recursive call yielded a solution!
print("Hooray, considering {} worked!".format(s))
print("Hooray, considering {} worked!".format(s))
Copy
Copied
Copy
Copied
return soln
return soln
, True # # Set the "solved" variable to True
# if we reach this line, step `s` only led to dead ends
# if we reach this line, step `s` only led to dead ends
# if we reach this line, then no continuation of `path`
# if we reach this line, then no continuation of `path`
# leads to a solution, only dead ends.
# leads to a solution, only dead ends.
Copy
Copied
Copy
Copied
# Still need to return path_list so we can visualize it later
print("Path {} leads only to dead ends".format(path))
print("Path {} leads only to dead ends".format(path))
Copy
Copied
Copy
Copied
return
None
return
path_list, False # Keep the "solved" variable as False.
Saved diffs
Original text
Open file
def solvemaze(M,path=None): """ Find a solution to maze `M` using a depth-first recursive search. """ print("Called with path={}".format(path)) if path==None: # no path specified, so start # at M.start path = [M.start] if path[-1]==M.goal: # We've found a solution print("SOLVED!") return path # next steps to consider # (may include retracing our steps) steps = M.free_neighbors(*path[-1]) for s in steps: if len(path)>=2 and s == path[-2]: print("Not considering {}, as it would retrace our steps".format(s)) continue # Consider whether next step `s` leads to a solution print("Considering next step {}".format(s)) soln = solvemaze(M,path + [s]) if soln != None: # Some recursive call yielded a solution! print("Hooray, considering {} worked!".format(s)) return soln # if we reach this line, step `s` only led to dead ends # if we reach this line, then no continuation of `path` # leads to a solution, only dead ends. print("Path {} leads only to dead ends".format(path)) return None
Changed text
Open file
def solvemaze_history(M,path_list=None, solved=False): """ Functions similarly to `solvemaze`, but returns a list of all paths considered throughout solving process. Uses additional arg `solved` to track whether solution has been found. """ if path_list==None: # no path specified, so start # at M.start path_list = [[M.start]] # Use a list of lists for path_list - one list for each path. else: print("Called with path={}".format(path_list[-1])) path = path_list[-1] if path[-1]==M.goal: # We've found a solution print("SOLVED!") return path_list, True # Set the "solved" variable to True # next steps to consider # (may include retracing our steps) steps = M.free_neighbors(*path[-1]) for s in steps: if len(path)>=2 and s == path[-2]: print("Not considering {}, as it would retrace our steps".format(s)) continue # Consider whether next step `s` leads to a solution print("Considering next step {}".format(s)) next_path = path + [s] path_list.append(next_path) soln, solved = solvemaze_history(M,path_list) if solved: # Some recursive call yielded a solution! print("Hooray, considering {} worked!".format(s)) return soln, True # # Set the "solved" variable to True # if we reach this line, step `s` only led to dead ends # if we reach this line, then no continuation of `path` # leads to a solution, only dead ends. # Still need to return path_list so we can visualize it later print("Path {} leads only to dead ends".format(path)) return path_list, False # Keep the "solved" variable as False.
Find difference