Question: Have the function ArrayChallenge( arr ) take the array of numbers stored in arr and from the position in the array where a 1 is,

Have the function ArrayChallenge(arr) take the array of numbers stored in arr and from the position in the array where a 1 is, return the number of spaces either left or right you must move to reach an enemy which is represented by a 2. For example: if arr is [0, 0, 1, 0, 0, 2, 0, 2] then your program should return 3 because the closest enemy (2) is 3 spaces away from the 1. The array will contain any number of 0's and 2's, but only a single 1. It may not contain any 2's at all as well, where in that case your program should return a 0.

Have the function ArrayChallenge(arr) take the array of numbers stored in arr

1 def ArrayChallenge(arr): n= len(arr) x=1 y=2 min_dist= 100000 for i in range( n) : for k in range (i+1,n) : if (x== arr[i] and y== arr[k] or y== arr [i] and x==arr[k]) and min_dist> abs(i-k): min_dist2=abs(i-k) return min_dist2 \# code goes here return arr \# keep this function cal1 here print(Arraychallenge(input()))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!