Spot the bug
This version gives wrong answers. Which line is wrong?
def others(m: list[int]) -> list[int]:out = [1] * len(m)run = 1for i in range(len(m)):run *= m[i]out[i] = runreturn outprint(others([2, 3, 4]))
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 5
run *= m[i]
Line 5 multiplies m[i] in before storing it, so slot i includes its own value. Store run first, then multiply.