Generation

generate functionThu, 22 Dec 2022

Return a new array consisting of elements which are multiple of their own index in input array (length > 1). Some cases: [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] [22] should equal [-6, 32, 25] [68] should equal [-1, 10] [11] should equal [-11] [-56] should equal [-85, 72, 0, 68] [28] should equal [38, -44, -99] [-1] should equal [-49, 8, -60, 35]

def multiple_of_index(arr): new_arr = [] for i in range(1, len(arr)): if arr[i] % i == 0: new_arr.append(arr[i]) return new_arr

Questions about programming?Chat with your personal AI assistant