Question: A broken implementation of a recursive search for the optimal path through a grid of weights. Richard Lobb, January 2 0 1
A broken implementation of a recursive search for the optimal path through
a grid of weights.
Richard Lobb, January
INFINITY floatinf # Same as math.inf
def readgridfilename:
Read from the given file an n x m grid of integer weights.
The file must consist of n lines of m spaceseparated integers.
n and m are inferred from the file contents.
Returns the grid as an n element list of m element lists.
THIS FUNCTION DOES NOT HAVE BUGS.
with openfilename as infile:
lines infile.readsplitlines
grid intbit for bit in line.split for line in lines
return grid
def gridcostgrid:
The cheapest cost from row to row n origin in the given
grid of integer weights.
nrows lengrid
ncols lengrid
def cellcostrow col:
The cost of getting to a given cell in the current grid."""
if row or row nrows or col or col ncols:
return INFINITY # Offgrid cells are treated as infinities
else:
cost gridrowcol
if row :
cost mincellcostrow col deltacol for deltacol in range
return cost
best mincellcostnrows col for col in rangencols
return best
def filecostfilename:
The cheapest cost from row to row n origin in the grid of integer
weights read from the given file
infile openfilenamer
grid
for line in infile:
row intx for x in line.split
grid.appendrow
infile.close
return mincostpathgrid lengrid
The answer box has been preloaded with code that reads a file of integer weights and computes the minimum cost path between the bottom row and the top row. The code uses a direct recursive implementation of the recurrence relation given in the lecture notes.
But
there are two problems with the code.
Firstly, it has a simple logic error and fails even on a simple test case. For example, the output from the first test is
but the input file is
for which the answer should clearly be
The other problem is that it times out on larger grids, such as a
x
grid
We'll deal with the time
out problem in the next question. Your task in this question is just to fix that bug so that code passes the trivial test above and the simple
x
example from the lecture notes:
The bug is very simple, involving just one tiny bit of text
e
g
a word or a number
but it is not all that obvious. You will probably have to use some simple debugging techniques to locate it
Please keep in mind that the purpose of this exercise is to help you to understand the algorithm, so asking someone what the bug is will likely destroy the learning outcome.
For this question, pylint compliance is required although docstrings aren't necessary and name checking is turned off.
Resubmission penalties are turned off for this question.
For example:
Test Result
print
file
cost
checkerboard
trivial.in
print
file
cost
checkerboard
small.in
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
