Home > Net >  Python prison break problem. Not sure now to write the for loop
Python prison break problem. Not sure now to write the for loop

Time:01-31

Hi I am new to Python and trying to solve a prison break problem- Question: 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. Example: [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. Each time you free a prisoner, the locked cells become unlocked, and the unlocked cells become locked again. You can only move from left to right and not go back.

Create a program that, given this unique prison arrangement, returns the number of freed prisoners.

You must free a prisoner in order for the locks to switch. So in the second example where the input is [1, 1, 1] after you release the first prisoner, the locks change to [0, 0, 0]. Since all cells are locked, you can release no more prisoners. H You always start within the leftmost element in the list (the first prison cell). If all the prison cells to your right are zeroes, you cannot free any more prisoners.

Below is my code

prisoners = list(map(int(input("Please enter prisoner arrangement. e.g.0101010").strip()))
freed = 0  
for i in range(len(prisoners)):
  if prisoners[i] == 1: 
    freed  = 1  
    for j in range(len(prisoners)):
      prisoners[j] = 1 - prisoners[j]  # toggle the status of current and future cells
    if i==len(prisoners):
      break
print(freed)

————— I have looked online for help - while there are solutions to problems similar to this one, I cannot figure out why I cannot generate the number of freed prisoners.

Could you please let me know what I did wrong?

Thank you so much!

CodePudding user response:

You have used the map function to cast the input string to int, but you did not pass the argument to the function in the call to map. Change the line to prisoners = list(map(int, input("Please enter prisoner arrangement. e.g.0101010").strip())) to correctly cast the input string to a list of integers. Also, your code doesn't handle the lock toggle correctly

enter code here prisoners = list(map(int, input("Please enter prisoner arrangement. e.g.0101010").strip()))
freed = 0  
for i in range(len(prisoners)):
  if prisoners[i] == 1: 
    freed  = 1  
    if i 1 < len(prisoners):
      for j in range(i 1, len(prisoners)):
        prisoners[j] = 1 - prisoners[j]  # toggle the status of future cells
    break
print(freed)

CodePudding user response:

The other answer actually shows you your problem, but it'll be much more efficient (and simpler) instead of changing the value for each item in the array to have a boolean flag to let you know whether a value should be 0 or 1:

prisoners = list(map(int, input("Please enter prisoner arrangement. e.g.0101010: ").strip()))

freed = 0
toggle = True

for i in range(len(prisoners)):
  if (prisoners[i] and toggle) or (not prisoners[i] and not toggle):
    freed  = 1  
    toggle = not toggle
print(freed)
  • Related