[Python] CodeSignal - 'Mutate Array'

Discussão em 'Python' iniciado por Stack, Agosto 9, 2025.

  1. Stack

    Stack Membro Participativo

    The coding question asks:


    Given an integer n and an array a of length n, your task is to apply the following mutation to a:

    Array a mutates into a new array b of length n. For each i from 0 to n - 1, b = a[i - 1] + a + a[i + 1]. If some element in the sum a[i - 1] + a + a[i + 1] does not exist, it should be set to 0. For example, b[0] should be equal to 0 + a[0] + a1.
    ​


    My Code:

    def solution(n, a):
    b = [None] * n
    for i in range(0, n-1):
    print('i = ', i)
    if i <= 0:
    b = 0 + a + a[i+1]
    print('IF 1')
    elif i >= n-1:
    b = a[i-1] + a + 0
    print('IF 2')
    else:
    b = a[i-1] + a + a[i+1]
    print('IF 3')
    return b


    The issue is that the for loop does not seem to run a sufficient amount of times, and I cannot change the range according to the question. Any ideas? See results below.

    [​IMG]

    Continue reading...

Compartilhe esta Página