Answer to Question #274506 in Python for Holik

Question #274506

10. Find the arithmetic mean of the negative elements in the list and find the smallest


replace with an item.

1
Expert's answer
2021-12-04T20:48:20-0500
def fun(L):
    tot = 0
    n = 0
    i_min = None
    for i, x in enumerate(L):
        if x < 0:
            tot += x
            n += 1
        if i_min is None or x < L[i_min]:
            i_min = i
    if n > 0:
        mean = tot / n
        L[i_min] = mean
    
L = [1, -2, 3, 4 -5, 6, -7, -8]
print("Initial list  ", L)
fun(L)
print("Processed list", L)

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