programming question, thx Write the Python function find_interval(a_list) to fin
ID: 3679011 • Letter: P
Question
programming question, thx
Write the Python function find_interval(a_list) to find the Interval of the a_list: you want to find indices i_min and i_max such that is as large as possible. The function has to loop over all possible values of i_min and i_max working out the sum for each, and keeping track of the biggest you've seen so far. (Note that the a_list items are allowed to be negative, so it's not always best to just take the whole a_list.) When you have found i_min and i_max, you should return a list containing these two indices.Explanation / Answer
python Program:
def find_interval(a_list):
i_min=0;
i_max=1
for i in range(0,len(a_list)):
for j in range(2,len(a_list)):
if (a_list[i_min]+a_list[i_max]) < (a_list[i]+a_list[j]) and (i!=j):
i_min=i;
i_max=j;
print "i_min=",i_min," i_max=",i_max,"value1=",a_list[i_min],"value2",a_list[i_max],"sum=",a_list[i_min]+a_list[i_max]
find_interval([1,2,3,4,5,6,7,8,9,10])
output:
i_min= 8 i_max= 9 value1= 9 value2 10 sum= 19
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.