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
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)
Comments
Leave a comment