Monday, April 15, 2024
HomeSoftware EngineeringFixing for A number of of Index in Python

Fixing for A number of of Index in Python

The problem#

Return a brand new array consisting of parts that are a number of of their very own index in enter array (size > 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 answer in Python code#

Possibility 1:

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

Possibility 2:

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

Possibility 3:

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

Check circumstances to validate our resolution#

``````import check
from resolution import multiple_of_index

@check.describe("Fastened Checks")
def fixed_tests():
@check.it('Fundamental Check Circumstances')
def basic_test_cases():
check.assert_equals(multiple_of_index([22, -6, 32, 82, 9, 25]), [-6, 32, 25])
check.assert_equals(multiple_of_index([68, -1, 1, -7, 10, 10]), [-1, 10])
check.assert_equals(multiple_of_index([11, -11]), [-11])
check.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])
check.assert_equals(multiple_of_index([28,38,-44,-99,-13,-54,77,-51]), [38, -44, -99])
check.assert_equals(multiple_of_index([-1,-49,-1,67,8,-60,39,35]), [-49, 8, -60, 35])
``````
RELATED ARTICLES