In python: Write a function named largest that will receive one list as an input
ID: 3828431 • Letter: I
Question
In python: Write a function named largest that will receive one list as an input argument. The elements of the list can be positive or negative numbers. The function will return the largest element of the list that is adjacent to an element with the value 0(zero). If the input argument list is empty, or if the largest element adjacent to a zero is itself a zero, or if there is no element with the value zero (hence, no element adjacent to a zero), or if all elements of the input argument list are zeros, then the function should return an empty list. Note: The function should return the desired output regardless of the input list size.
Explanation / Answer
#Function returns the largest adjacent number to 0 if it present
#otherwise in all other cases it will return -1
def find_large(list2):
largest=0
temp=0
for i in range(0,len(list2)):
if list2[i]==0 and (i!=0 or i!=len(list2)):
if list2[i-1] > list2[i+1]:
temp=list2[i-1]
else:
temp=list2[i+1]
elif list2[i]==0 and i==0 :
temp=list2[i+1]
elif list2[i]==0 and i!=len(list2):
temp=[i-1]
if(temp > largest):
largest=temp
if largest==0:
return -1
else:
return largest
list2 = [1, 2, 0, 4, 5, 0, 7 ];
print find_large(list2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.