Skip to content
Advertisement

How can I change what a user already inputted and change a specific part of the input based on the parameters?

x=input()
x=list(x)
n=''.join(x[0:x.index(' ')])
m=''.join(x[x.index(' ')+1:])
for y in range (0,int(n)):
    seats=input()
events=int(input())
for a in range (0,events):
    changes=input()
    if changes[0]=='i' and changes[1]=='n':
        if int(changes[3:])>int(m):
            print("-1")

this is what my effort.

The goal is to take in two inputs (n,m), with n being the rows of seats and m being the number of columns. then I have to take in n more inputs with each one being whether a seat in that row is taken, for example, if 2 3 was inputted for n m then, the user would next input 2 more times with each input having 3 integers 0 or 1 (1 being seat is taken, 0 being seat is empty). so 101 111 would mean that the first row middle seat is empty but the rest of the seats are taken. then after the seats’ occupancy is inputted by the user, i will take one more int input determining the amount of events happening to the seats, then i will take x amount more inputs with x being the amount of events. there are two types of events: “in” and “out”. if the user inputs “in z”, it means z amount of students have entered the room and they are looking to find z amount of seats in the same row to sit in, if they cant find that amount of seats in a row, then they leave the room. what this means is lets say the class was originally 1110 0101 and then the user inputted ‘in 2’ then the 2 students that have entered the room would leave because there aren’t any 2 consecutive empty seats in the same row. when this happens, we should print ‘-1’ indicating they have left because they didn’t find seats. however, lets say it was 1001 1111 and the user inputted ‘in 2’, then the students would take the two seats in the front row and it would become 1111 1111, thus we must indicate they have taken those seats by printing ‘1 2’ indicating that the students have taken the 2 consecutive seats starting from row 1 seat 2. also, students that are choosing a seat are trying to minimize the row and column as much as possible, for example, 1010 1010 and the user inputs ‘in 1’ then the seats become 1110 1010 because the student chooses the lowest possible row and in that row, chooses the lowest possible consecutive column.

for the other type of event: ‘out’ the user would input ‘out x y’ with x being the row and y being the seat. out means that a student has left a seat (note: it is guaranteed that a student is seated there when the ‘out’ is inputted). so if the seats were 1110 0111 and the user inputted ‘out 2 3’ the seats would then become 1110 0101.

consider this sample output and input: Sample Input 1:// 3 4// 0110// 0001// 1010// 6// in 3// out 2 2// in 2// in 1// out 2 3// in 2// Sample Output 1:// 2 1// -1// 1 1// 2 2// 1110// 1111// 1010// notice that every line of output (// indicates next line) corresponds to the ‘in’ action, for example the ‘in 3’ action corresponds with the 2 1 seats being taken and the ‘in 2’ action is ‘-1’ because no two consecutive seats were free at the time. then print the final state of the seats after the events are done.

I’m extremely confused on how i could do this task without any import statements (its one of the guidelines). I would really appreciate any help thanks so much.

Advertisement

Answer

seats is the state of your classroom so you’ll be using it and updating it to determine what you need to know. An example seats is 0000 0000 0000. Note the spaces which keep the rows separated.

Given in z, to find consecutive empty seats, we can use seats.find('0' * z) which will either tell us the location or -1 if there isn’t any. location is always the lowest value so it will be the minimum row, column. If location is valid then we can update seats by slicing, seats = seats[:location] + '1' * z + seats[location + z:]

For out x y you need to convert the two inputs to the index in the seats string, location = (x - 1) * (m + 1) + (y - 1). We can slice again by seats = seats[:location] + '0' + seats[location + 1:]

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement