Question 1 10 marksl Write a function find valuepos(Alist, base) that returns th
ID: 3701377 • Letter: Q
Question
Question 1 10 marksl Write a function find valuepos(Alist, base) that returns the position of the smallest value in the list Alist and this smallest value is larger than base. The function will return -1 if there is no value larger than base. Note, do not sort this list. You should be able to solve this problem going through the list only once Use this function in a main program as follows. First your main program should assign values to a list. For example L-14,-1, 52, 17, 81, -2, 75,24] The main program should then print the list. Then the main program should call the above function appropriately in order to print the smallest numbers from the list that are greater than 0, 25, 50, 75 and 100 respectively using the format below. If there is no value larger than one or more of these numbers, then the program will print an appropriate message as indicated below. Remember that the TAs will potentially change the list (and its size) in order to test your program so your program must be general for different list values and sizesExplanation / Answer
def find_valuepos(Alist, base): pos = -1 for i in range(len(Alist)): if Alist[i] > base: if pos == -1: pos = i else: if Alist[pos] > Alist[i]: pos = i return pos if __name__ == '__main__': L = [4, -1, 52, 17, 81, -2, 75, 24] #L = [1, -3, 50, 84, 39, 13, 5] for i in range(0, 101, 25): index = find_valuepos(L, i) if index != -1: print("The smallest number greater than ", i, " is ", L[index]) else: print("There are no numbers greater than ", i, " in this list")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.