
Gavin R. answered 10/22/20
Experienced Coding Instructor and Tutor
Is this a trick question?
The function is finding the first value that, when combined with it's index, is greater than t. The most computationally simple way to do that is to check each value in turn, and stop when you find one that satisfies the condition. This function does just that and is O(1).
However, I would probably clean up the code a bit like this:
def large_prefix(lst, t):
for i in range(len(lst)):
if i + lst[i] > t:
return lst[:i+1]
(n and s are not needed, and Python functions automatically return None when not specified.)