In [5]:
import copy
In [1]:
def answer(x):
totalrabbits = 0
for famsize in x:
totalrabbits += famsize
return totalrabbits
testarray = [5, 10, 8]
print(answer(testarray))
In [6]:
def answer(population, x, y, strength):
# flip axes for matrix indexing
if population[y][x] > strength:
return population
else: # do urrything
#newpopulation = list(population)
#newpopulation = population[:]
#newpopulation = []
#newpopulation.extend(population)
newpopulation = copy.deepcopy(population)
xmax = len(population[0])
ymax = len(population)
newlyinfected = [(x,y)]
while len(newlyinfected) > 0:
tempinfected = []
for infected in newlyinfected:
xi, yi = infected
if xi - 1 >= 0: # x-1 > 0
if newpopulation[yi][xi-1] <= strength and newpopulation[yi][xi-1]>-1:
newpopulation[yi][xi-1] = -1
tempinfected.append((xi-1,yi))
if xi + 1 < xmax:
if newpopulation[yi][xi+1] <= strength and newpopulation[yi][xi+1]>-1:
newpopulation[yi][xi+1] = -1
tempinfected.append((xi+1,yi))
if yi - 1 >= 0: # x-1 > 0
if newpopulation[yi-1][xi] <= strength and newpopulation[yi-1][xi]>-1:
newpopulation[yi-1][xi] = -1
tempinfected.append((xi,yi-1))
if yi + 1 < xmax:
if newpopulation[yi+1][xi] <= strength and newpopulation[yi+1][xi]>-1:
newpopulation[yi+1][xi] = -1
tempinfected.append((xi,yi+1))
newlyinfected = tempinfected
return newpopulation
In [7]:
population = [[1, 2, 3], [2, 3, 4], [3, 2, 1]]
x = 0
y = 0
strength = 2
print(answer(population, x, y, strength))
print(population)