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

Expert's answer

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!

LATEST TUTORIALS
APPROVED BY CLIENTS