Cafeteria Queue Lunchtime is the most awaited time of the day for students in your school. Not only because there are no classes, but because the lunch is really good! At 12, all students run to the cafeteria and queue up for the meal.
Professor Yilma noticed that and decided to use it to encourage students to do better in his course. He convinced the cafeteria people to serve the students not in the order they arrived, but in order of their grades, highest to lowest. This way, students with higher grades get their lunch first.
Your task is simple: given the arrival time of the students in the cafeteria, and their respective grades in the math class, reorder the queue according to the math grades, and check how many students do not need to change place in this reordering.
Implement the function cafeteriaQueue(Q) that takes as input a list of the students in the queue, identified by their grades. For example, if the first students that arrived has grade 100, the second has grade 80, and the third student’s grade is 90, Q would be [100,80,90]. There will be no students with the same grade due to professor Yilma’s fine grained scoring system. This function returns the number of students that do not need to change places in line.
• cafeteriaQueue([100,80,90]) == 1
• cafeteriaQueue([100,120,30,50]) == 0
• cafeteriaQueue([100,90,30,25]) == 4
Code for above program in python :- if u need code in any other language leave a comment and do give a thumbs up.
# function to calculate Number of students who do not need to change position def cafeteriaQueue(queue): # creating a copy of the queue sortedqueue = queue.copy() # sorting the copied queue in descending order sortedqueue.sort(reverse=True) # initialising variable to keep sum of students who do not change position student = 0 # iterating over both the lists for i in range(len(queue)): # checking if the position is same as earlier or not in the queue if queue[i] == sortedqueue[i]: # incrementing count student+=1 return student print(cafeteriaQueue([100,80,90])) print(cafeteriaQueue([100,120,30,50])) print(cafeteriaQueue([100,90,30,25]))
Screenshot for reference :-
Get Answers For Free
Most questions answered within 1 hours.