How to replace every element in a list by multiplication of next and previous elements with Python

1 Answer

0 votes
# lst[first] = lst[first] (itself) * lst[next];
# lst[last] = prev * lst[last] (itself);

def MultiplyNextPrevious(lst):
    size = len(lst);
 
    if size <= 1:
        return
 
    prev = lst[0]
    lst[0] = lst[0] * lst[1]
 
    for i in range(1, size - 1):
        curr = lst[i];
        lst[i] = prev * lst[i + 1]
        prev = curr
     
    lst[size - 1] = prev * lst[size - 1]
 
 
lst = [2, 3, 5, 6, 12, 8, 10, 7]

MultiplyNextPrevious(lst)

for i in range (0, len(lst)):
    print(lst[i]) #, end=' ')
    
    
    
'''
run:

6
10
18
60
48
120
56
70

'''

 



answered Oct 2, 2022 by avibootz
edited Oct 2, 2022 by avibootz
...