Answer to Question #265990 in Python for Sanysany

Question #265990

Yup are given a space separated list of integers as input,write a program to replace all non positive numbers in a list with the last positive number encountered

1
Expert's answer
2021-11-14T09:22:12-0500
lst_of_int = [int(i) for i in input().split()]
curr = -1
for i in range(len(lst_of_int)):
	if lst_of_int[i] >= 0:
		curr = lst_of_int[i]
	if lst_of_int[i] < 0:
		if curr >= 0:
			lst_of_int[i] = curr


print(*lst_of_int)

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