I'm solving a problem with this concept: It is a classroom, and I have to replace an available seat with a name, but in a specific way: it is replaced in the last row, from right to left. If the classroom is full it will show a message that there are no places available.
I have no idea how to replace an available seat in the order that the problem says.
import numpy as np
classroom1 = np.array([["Martha", "Available"],
["Max", "Anahi"],
["Available", "Available"],
["Alexis", "Rigel"]])
So far I can replace a seat of my choice manually, but not in the way the problem states. for example:
classroom1[0,1] = "Alex"
print(classroom1)
[["Martha", "Alex"],
["Max", "Anahi"],
["Available", "Available"],
["Alexis", "Rigel"]]
But I still can't get an idea of how to solve it according to the problem. I don't know how to solve it.
CodePudding user response:
Would this be what you're looking for?
classroom1[-1,-2] = "Alex"
print(classroom1)
Output:
[['Martha' 'Available']
['Max' 'Anahi']
['Available' 'Available']
['Alex' 'Rigel']]
CodePudding user response:
We can use reverse indexing or negative indexing to solve this problem. With positive/normal indexing you traverse through an array start through end. But with reverse indexing you use "-" sign with indexes and it traverses in the reverse direction.
consider this example:
x = np.array(['a','b','c','d','e','f','g'])
print(x[2])
print(x[-5])
so both these instructions will pring out 'c'
one thing to remember is that in reverse indexing we start from -1 so if you want to print 'g' you would have to pass x[-1]
Now to your original problem: you have a 2 dimensional array that means you will have to pass two negative indexes to do the required modification
classroom[index of subarray][index of element within subarray]
consider another example:
classroom1[0,1] = "Alex"
print(classroom1)
[["Martha", "Alex"],
["Max", "Anahi"],
["Available", "Available"],
["Alexis", "Rigel"]]
say we want to update the seat at index 0 in array at index 2, so we will have to do reverse indexing as follows:
classroom[-2][-2] = "Peter"
[["Martha", "Alex"],
["Max", "Anahi"],
["Peter", "Available"],
["Alexis", "Rigel"]]
CodePudding user response:
Code:-
import numpy as np
classroom1 = np.array([["Martha", "Available"],
["Max", "Anahi"],
["Available", "Available"],
["Alexis", "Rigel"]])
list_of_names=['Yash','Sam','Itachi','Ace']
seats_available=0
for i in range(len(classroom1)):
for j in range(len(classroom1[0])):
if classroom1[i][j]=="Available":
seats_available =1
#Enter the students in order right to left and starting from the last
count=0
for i in range(len(classroom1)-1,-1,-1):
for j in range(len(classroom1[0])-1,-1,-1):
if classroom1[i][j]=="Available":
if seats_available and count<len(list_of_names):
classroom1[i][j]=list_of_names[count]
count =1
seats_available-=1
print(classroom1) #as you can see Ace is not present cause there is no available seats for them when he appeared.
Output:-
[['Martha' 'Itachi']
['Max' 'Anahi']
['Sam' 'Yash']
['Alexis' 'Rigel']]