I am attempting to make an Abelian Sandpile Model (see: Rosetta Code) in Python 3.8, and I have not looked at the provided solution because this is a practice exercise for me. I am getting started with a possible solution, but I am not sure why the following issue is occurring.
I have a 5x5 grid, composed of lists.
# a list composed of 5 child-lists, each containing five 0's
area = [[0]*5]*5
My first step is to place the sandpile (ie. change one (x, y) pair to a higher number), and I have written this function to accomplish that:
# changes one location to a greater height, takes a one-based (x, y) pair
def make_sandpile(area, loc, height):
# accommodate for zero-based counting
loc = list(n-1 for n in loc)
# translate to x and y
x, y = loc
# change the corresponding point in the area
area[y][x] = height
calling make_sandpile(area, (3, 3), 4)
should have the following outcome:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 -> 0 0 4 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
However, the outcome is as follows:
0 0 0 0 0 0 0 4 0 0
0 0 0 0 0 0 0 4 0 0
0 0 0 0 0 -> 0 0 4 0 0
0 0 0 0 0 0 0 4 0 0
0 0 0 0 0 0 0 4 0 0
What can I do to correct this?