in the example there are 6 numbers 1,-2,3,-4,-5,6
the negative numbers in the given list are -2,-4 and -5 where the -2 is replaced with corresponding last positive number encountered which is 1.similarly -4 and -5 are replaced with 3
so the output should be 1 1 3 3 3 6
sample input1
1 -2 3 -4 -5 6
sample output1
1 1 3 3 3 6
sample input2
1 11 -13 21 -19
sample output2
1 11 11 21 21
list1 = [1, -2, 3, -4, -5, 6]
l = len(list1)
total = 0;
size = l
for i in range(1, size):
count = 0;
if list1[i] < 0:
x = list1.index(list1[i])
if x ==0:
list1[1] = list1[0]
for row in range(x, -1, -1):
if list1[row] > 0:
list1[i] = list1[row]
break
else:
list1[i] = list1[i]
print(list1)
Comments
Leave a comment