Software Engineering

# Solving for Multiple of Index in Python

## The challenge#

Return a new array consisting of elements which are multiple of their own index in input array (length > 1).

Examples:

[22, -6, 32, 82, 9, 25] => [-6, 32, 25]

[68, -1, 1, -7, 10, 10] => [-1, 10]

[-56,-85,72,-26,-14,76,-27,72,35,-21,-67,87,0,21,59,27,-92,68] => [-85, 72, 0, 68]

## The solution in Python code#

Option 1:

``````def multiple_of_index(l):
return [l[i] for i in range(1, len(l)) if l[i] % i == 0]
``````

Option 2:

``````def multiple_of_index(arr):
i = 1
rst = []
while i < len(arr):
if arr[i] % i == 0:
print(i)
rst.append(arr[i])
i += 1
return rst
``````

Option 3:

``````def multiple_of_index(arr):
return [n for i,n in enumerate(arr[1:], 1) if n%i==0]
``````

## Test cases to validate our solution#

``````import test
from solution import multiple_of_index

@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
test.assert_equals(multiple_of_index([22, -6, 32, 82, 9, 25]), [-6, 32, 25])
test.assert_equals(multiple_of_index([68, -1, 1, -7, 10, 10]), [-1, 10])
test.assert_equals(multiple_of_index([11, -11]), [-11])
test.assert_equals(multiple_of_index([-56,-85,72,-26,-14,76,-27,72,35,-21,-67,87,0,21,59,27,-92,68]), [-85, 72, 0, 68])
test.assert_equals(multiple_of_index([28,38,-44,-99,-13,-54,77,-51]), [38, -44, -99])
test.assert_equals(multiple_of_index([-1,-49,-1,67,8,-60,39,35]), [-49, 8, -60, 35])
``````