def freed_prisoners(prison):
prisoners_count = 0
if prison[0] == 1:
prisoners_count += 1
del prison[0]
if len(prison) != 0:
for i in range(len(prison)):
if prison[i] == 1:
prison[i] = 0
else:
prison[i] = 1
freed_prisoners(prison)
else:
return prisoners_count
else:
del prison[0]
if len(prison) != 0:
freed_prisoners(prison)
else:
return prisoners_count
print(freed_prisoners([1, 1, 0, 0, 0, 1, 0]))
It returns none and i don t know why That's the exercise: A prison can be represented as a list of cells. Each cell contains exactly one prisoner. A 1 represents an unlocked cell and a 0 represents a locked cell.
[1, 1, 0, 0, 0, 1, 0] Starting inside the leftmost cell, you are tasked with seeing how many prisoners you can set free, with a catch. You are the prisoner in the first cell. If the first cell is locked, you cannot free anyone. Each time you free a prisoner, the locked cells become unlocked, and the unlocked cells become locked again.