Question #283339

ayush is a given a list of N integers.He does not like non positive numbers appearing in the list.He wishes to replace each of the given non positive numbers with the last positive number encountered in the list.Help him by providing the modified list as the output

Expert's answer

def replace_non_positive(L):
    prev_positive = 0
    for i in range(len(L)):
        if L[i] > 0:
            prev_positive = L[i]
        else:
            L[i] = prev_positive


L = [ 1, 3, -2, 7, 0, 4, -2, -7]
print("Initil list:  ", L)
replace_non_positive(L)
print("Replaced list:", L)


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS