Trying to build a response to a problem on HackerRank, and I've almost got it. I just need to build one if statement for an edge case. The edge case is where there are 2+ values equal to each other and you need to decide which to use based on a condition. My strategy for doing this is grabbing the indices of the 2+ elements and checking the complementary list for the tie breaker. My question is how to get a list of indices of elements in a list that are equal to your element?strong text
Example Input:
array, element_of_interest = [2,9,6,3,9], 9
Example Output:
indices = [1,4]
I'm including my code, but it's a bit of a clusterfuck at the moment. Provided a description of the problem and my strategy if it makes a difference for your answer.
def sherlock_array2(arr, p, q):
# Problem:
# For each value in range(p, q+1), find the absolute difference between it and each of the values in the array. Then
# take the smallest difference for each value and make a list of them. Now that we have a list of differences and
# each's associated (p,q+1) range value, find the largest difference. Report the value from range(p, q+1) that is
# associated with the largest difference.
# i.e. maximize the minimum difference
# My Strategy:
# The number in range(p, q+1) that has highest minimum difference is going to be the median of the largest gap in the
# given array. E.g. arr = [1,4,10,22], range(0, 24). At point 16, the difference is minimized against 10 and 24
# (difference equals 6). The next greatest, minimum difference is at point 7 where it is minimized against 4 and 10
# for a difference of 3.
# The edge case where this strategy doesn't work is when an array value exceeds the bounds of our range.
# Instead of iterating through every value in the range, I simply find the largest gap, then check if the median value
# is in the range. If it is, we send the value on its way. If it isn't, we have to check the two values that gave us
# that difference. If one is in the range, we need to compute the difference between it and the range's extreme value
# (the one the median crossed over). If that difference is still the greatest, we use the range extreme. If it isn't,
# we get rid of the extreme value and rerun the function.
arr = sorted(arr)
diffs, ceilings, floors = [], [], []
for i, x in enumerate(arr):
if i < len(arr) - 1:
diffs.append(abs(arr[i+1] - x))
ceilings.append(arr[i +1])
floors.append(x)
max_diff = max(diffs)
if diffs.count(max_diff) == 1:
i = diffs.index(max_diff)
med = ceilings[i] - round(max(diffs) / 2)
if med < p:
if ceilings[i] > p:
new_diff = ceilings[i] - p
diffs[i] = new_diff
if new_diff == max(diffs):
return p
else:
ii = arr.index(floors[i])
arr.pop(ii)
return sherlock_array2(arr, p, q)
else:
ii = arr.index(floors[i])
arr.pop(ii)
return sherlock_array2(arr, p, q)
elif med > q:
if floors[i] < q:
new_diff = q - floors[i]
diffs[i] = new_diff
if new_diff == max(diffs):
return p
else:
ii = arr.index(ceilings[i])
arr.pop(ii)
return sherlock_array2(arr, p, q)
else:
ii = arr.index(ceilings[i])
arr.pop(ii)
return sherlock_array2(arr, p, q)
else:
return med
else:
# This is where the tie-breaker will be