Answer to Question #283339 in Python for Swarupa

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

1
Expert's answer
2021-12-28T10:11:48-0500
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS