Conor
Conor

Reputation: 109

CodeSignal - 'Mutate Array'

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[i] = a[i - 1] + a[i] + a[i + 1]. If some element in the sum a[i - 1] + a[i] + 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[i] = 0 + a[i] + a[i+1]
            print('IF 1')
        elif i >= n-1:
            b[i] = a[i-1] + a[i] + 0
            print('IF 2')
        else:
            b[i] = a[i-1] + a[i] + 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.

enter image description here

Upvotes: 2

Views: 2794

Answers (2)

Abhishek Porwal
Abhishek Porwal

Reputation: 11

The other solution failed for single element array. This should work fine.

def solution(a):
    b = []
    #print(b)
    for i in range(len(a)):
        if len(a) == 1:
            b.append(a[0])
        else:

            if i == 0:
                # b[i] = 0 + a[i] + a[i+1]
                b.append(0 + a[i] + a[i+1])
                print(b[i])
            elif i >= len(a) - 1:
                b.append(a[i - 1] + a[i] + 0)
            else:
                # b[i] =  a[i - 1] + a[i] + a[i + 1]
                b.append(a[i - 1] + a[i] + a[i + 1])
    return b

Upvotes: 1

constantstranger
constantstranger

Reputation: 9379

You wrote "For each i from 0 to n - 1, b[i] = ...".

Since we're probably going to use range(), let's start by examining how it works with a handful of experiments.

Experiment #1: This code ...

n = 5
for i in range(0, n-1):
    print(i)

... produces this output:

0
1
2
3

In other words, range(0, n-1) only produces the values 0 through n-2.

Experiment #2: This code ...

n = 5
for i in range(0, n):
    print(i)

... produces this output:

0
1
2
3
4

From this second experiment, we see that range(0, n) is the way to correctly produce the values 0 through n-1, which is the task specified in your question.

Let's do one final experiment.

Experiment #3: The following code ...

n = 5
for i in range(n):
    print(i)

... produces this output:

0
1
2
3
4

The output for range(n) is exactly the same as for range(0, n). This experiment demonstrates that although range(0, n) matches the requirements of the task in your question, so does the slightly simpler expression range(n).

Now that we have clarified how range() works, let's try the following slight modification of the code sample in your question:

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

This code runs without any errors and its output is:

i =  0
IF 1
i =  1
IF 3
i =  2
IF 3
i =  3
IF 3
i =  4
IF 2
[4, 5, -1, 2, 1]

Problem solved!

Upvotes: 0

Related Questions