0

I'm new to programing, so I might miss the obvious. But I'm trying to solve a python exercise where I get a list ("cells") and an integer ("generation") and I must return the answer without changing the input list. I tried creating a new list ("nc") that equals "cells" list and manipulate it instead of "cells". And yet for some reason, "cells" still gets changed. Could you help me find the problem?

An example of the input I might get, could be [[0, 1, 0], [1, 1, 0], [1, 0, 1]], 3

def get_generation(cells, generations):

    nc = cells
    rownum = len(nc)
    cellnum = len(nc[0])

    for gen in range(generations):

        tc = []
        shouldadd = []

        for line in range(1, len(nc[0]) - 1):
           if nc[0][line - 1] + nc[0][line] + nc[0][line + 1] == 3:
               shouldadd.append(line)
        if len(shouldadd) > 0:
           rownum += 1
           nc.append([0 for x in nc[0]])
           for ll in range(len(nc[0])):
               if ll in shouldadd:
                   tc.append(1)
               else:
                   tc.append(0)


        for row in range(rownum):
            canrows = [row]
            if row == 0:
                canrows.append(1)
            elif row == rownum - 1:
                canrows.append(row - 1)
            else:
                canrows.append(row - 1)
                canrows.append(row + 1)
            for cell in range(cellnum):
                cancells = [cell]
                if cell == 0:
                    cancells.append(1)
                elif cell == cellnum - 1:
                    cancells.append(cell - 1)
                else:
                    cancells.append(cell - 1)
                    cancells.append(cell + 1)


                surcells = 0

                for cr in canrows:
                    for cc in cancells:
                        surcells += nc[cr][cc]

                surcells -= nc[row][cell] 

              
                if surcells < 2 or surcells > 3:
                    tc.append(0)
                elif surcells == 3:
                    tc.append(1)
                else:
                    tc.append(0) if nc[row][cell] == 0 else tc.append(1)

        shouldadd = []

        for line in range(1, len(nc[0]) - 1):
           if nc[-1][line - 1] + nc[-1][line] + nc[-1][line + 1] == 3:
               shouldadd.append(line)
        if len(shouldadd) > 0:
            rownum += 1
            nc.append([0 for x in nc[0]])
            for ll in range(len(nc[0])):
                if ll in shouldadd:
                    tc.append(1)
                else:
                    tc.append(0)

        for r in range(rownum):
            for c in range(cellnum):
                nc[r][c] = tc.pop(0)

        if sum(nc[0]) == 0:
            nc.pop(0)
            rownum -= 1

        if sum(nc[-1]) == 0:
            nc.pop()
            rownum -= 1

    return nc
Mugitaba
  • 3
  • 2

1 Answers1

0

Ah, I've been here before!

def get_generation(cells, generations):
    import copy
    nc = copy.deepcopy(cells)  #Use deepcopy as copy won't break link to source

    ... rest of your code
Amiga500
  • 1,258
  • 1
  • 6
  • 11