The below code is for taking a set of rows of students' marks in array, and need find the row with maximum marks.
Below is incomplete code, as need to search for the maximum sum row still; but stuck at the incomplete code due to error.
It gives below error in py3.codeskulptor.org/, while in Pythontutor, the program terminates at the same line number.
Line #17: IndexError: list assignment index out of range
# Input number of tests
t = int(input("Input number of tests"))
# Input size of rows (of students)
n = int(input("Input size of rows"))#.strip().split())
print (t,n)
arr = [[[] for i in range(t)] for i in range(n)]
total = [[] for i in range (n)]
for i in range(0,t):
# Input score of each student in row
sum =0
for j in range(n):
arr[i][j] = map(int, input("Input score").strip().split())#[:n]
#the above line causes compilation error
# Find sum of all scores row-wise
for m in arr[i][j]:
sum += m
total[i] = sum1
# Find the max. of total
for j in range(t):
y = max(total[j])
Please also suggest a crisper approach to program the above problem.
P.S. I found the above code to be nearly wrong, but kicked me in correct direction.
Have the modified code below with a question that arose during debugging that concerns the :
max() giving precedence to [] over an integer value
This behaviour of max() was detected when incorrectly stated line #13 as :
total = [[[] for i in range (l)] for i in range (n)]
The correct working code is below, with output :
# Input number of tests
t = int(input("Input number of tests"))
# Input number of rows (of students)
n = int(input("Input number of rows"))#.strip().split())
# Input limit on number of students in a row (of students)
l = int(input("Input max. number of studentsin any row"))#.strip().split())
print ('t :',t,'n :',n, 'l :', l)
arr = [[[[] for i in range(l)] for i in range(n)] for i in range(t)]
total = [[[] for i in range (n)] for i in range (t)]
# run input of tests
for i in range(t):
# Input score of each student in the i-th row, out of n
sum =0
for j in range(n):
#for k in range(l):
print("jkl:","i:",i,"j:",j)
arr[i][j] = map(int, input("Input score").strip().split())[:l]
for i in range(t):
for j in range(n):
# Find sum of all scores row-wise
sum = 0
for m in arr[i][j]:
#sum[i][j][] += m
print ('m :', m)
sum += m
#total[i][j] = sum[i][j][]
total[i][j] = sum
for i in range(t):
print("Test no. ", i)
for j in range(n):
#for m in arr[i][j]:
print (arr[i][j])
#print (m)
print("=========")
for i in range(t):
for j in range(n):
print(i,"-",j, total[i][j])
print("::::::")
print("=========")
# Find the max. of total
for i in range(t):
print([m for m in total[i]])
y = max([m for m in total[i]])
print ('i:',i,',', 'max total:',y)
Request reason for the precedence shown by max(), and if possible link the answer with some reference to underlying implementation in CPython.
arr = [[[] for i in range(t)] for i in range(n)]
Considering the above construction of arr
, you have swapped t
and n
in the nested loops:
for i in range(t): # … for j in range(n): arr[i][j] = …
The first index into arr
(corresponding to the outer loop) must be a number between 0 and n–1.
The second index into arr
(corresponding to the inner loop) must be a number between 0 and t–1.