我正在研究用于获取基本细胞自动机代码的功能。我的所有输出都正确,但是我无法获得所需的输出。
def main():
N= int(input("Enter number of steps,N:"))
C= int(input("Enter number of cells,C:"))
i_list=list(map(int,input("Enter the indices of occupied cells (space-separated):").split()))
cell= [0]*C
for i in i_list:
cell[int(i)] = 1
displayCells(cell)
for step in range(N):
newCells = updateCells(cell)
displayCells(newCells)
cells = []
cells += newCells # concatenates new state to new list
def displayCells(Cells):
for cell in Cells:
if cell == 1:
print("#", end='')
def updateCells(cells):
nc = [] # makes a new empty list
nc += cells # copy current state into new list
for a in range(1, len(nc)-1):
if nc[a-1] == 1 and nc[a] == 1 and nc[a+1] == 1:
nc[a] = 0
elif nc[a-1] == 1 and nc[a] == 1 and nc[a+1] == 0:
nc[a] = 1
elif nc[a-1] == 1 and nc[a] == 0 and nc[a+1] == 1:
nc[a] = 1
elif nc[a-1] == 1 and nc[a] == 0 and nc[a+1] == 0:
nc[a] = 0
elif nc[a-1] == 0 and nc[a] == 1 and nc[a+1] == 1:
nc[a] = 1
elif nc[a-1] == 0 and nc[a] == 1 and nc[a+1] == 0:
nc[a] = 1
elif nc[a-1] == 0 and nc[a] == 0 and nc[a+1] == 1:
nc[a] = 1
else:
nc[a] = 0
return nc
main()
我希望输出是遵循updateCells函数规则的循环,并在程序看到1时绘制#。