回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<pre><code>def jump_left(markers, row, column):
"""
Returns the grid that results after the marker at (row, column) jumps left
@type markers: list[list[str]]
@type row: int
@type column: int
@rtype: list[GridPegSolitairePuzzle]
>>> grid = [["*", "*", "*", "*", "*"]]
>>> grid.append(["*", "*", "*", "*", "*"])
>>> grid.append(["*", "*", "*", "*", "*"])
>>> grid.append(["*", "*", ".", "*", "*"])
>>> grid.append(["*", "*", "*", "*", "*"])
>>> gpsp1 = GridPegSolitairePuzzle(grid, {"*", ".", "#"})
>>> L1 = jump_left(gpsp1._marker, 3, 4)
>>> grid[3][2] = "*"
>>> grid[3][3] = "."
>>> grid[3][4] = "."
>>> L2 = [GridPegSolitairePuzzle(grid, {"*", ".", "#"})]
>>> L1 == L2
True
"""
# Checking bounds and whether the right pieces are in the positions needed
if (column - 2) >= 0 and (markers[row][column - 2] == ".") and\
(markers[row][column - 1] == "*"):
# Each row must be copied individually (since they are all lists)
m_copy = []
for i in range(len(markers)):
m_copy.append(markers[i].copy())
new_grid = GridPegSolitairePuzzle(m_copy, {"*", ".", "#"})
# Performs the jump
new_grid._marker[row][column] = "."
new_grid._marker[row][column - 1] = "."
new_grid._marker[row][column - 2] = "*"
return [new_grid]
else:
return []
</code></pre>
<p>我的程序应该移动用“*”表示的peg,然后跳转到空位(“.”)并移除其间的peg。你知道吗</p>
<p>对于上面的docstring:r1将变成</p>
<pre><code> ["*", "*", ".", "."]
</code></pre>
<p>我的代码适用于显示的docstring,但是如果<code>r1 = ["*", "*", ".", "*"]</code>和<code>r3 = ["*", ".", "*", "*"]</code>。你知道吗</p>
<p>它应该交换r3元素,但它不起作用(我知道我没有遍历每个空位置,但我找不到方法)</p>
<p>还有一个更好的方法是在指数超出范围内做。我在做try和except block,因为如果空的位置在第3列,它会给我索引超出范围,因为我在寻找它旁边的销钉</p>