Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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.
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started