Answer to Question #265991 in Python for sai krishna

Question #265991

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


1
Expert's answer
2021-11-15T00:30:36-0500


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)             
        
    
    


    

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