When I was thinking about when the enumerate function could be used effectively, the following tasks were asked in competitive programming, so I thought it was a good place to use it.
When data on student tests and absenteeism is given in order of student ID number, create a program that outputs the student ID number of students whose grades are above the passing score. Student ID numbers are given in order from 1. Student grades shall be the test scores minus the number of absences x 5 points.
N M a_1 b_1 a_2 b_2 a_3 b_3 …
N: Number of students M: Minimum passing score a_n: Test score b_n: Number of absences
There may be other good ways, but let's use the enumerate function to output the answer. You can use the enumerate function to get the list element and index at the same time in a for loop.
# man:Number of students, score:Minimum passing score
#First, get each numerical value from the input value by map.
man , score = map(int , input().split())
#Prepare an empty list
li = []
#Elements obtained by map to list(A pair with elements of score and absenteeism)In a loop
for i in range(man):
li.append(list(map(int , input().split())))
#Index of the obtained list using the enumerate function(Student number)Get list elements at the same time
#Output index by branching by calculating the numerical value in the element
for num , scr in enumerate(li):
if (scr[0] - scr[1]*5) >= score:
print(num + 1)
elif ((scr[0] - scr[1]*5) < score) and score == 0:
print(num + 1)
Recommended Posts