列出另一个满足条件的值的列表

时间:2019-12-23 04:18:08

标签: python list indexing

尝试建立对a problem on HackerRank的响应,我已经差不多了。我只需要为边缘情况构建一个if语句。边缘情况是2个以上的值彼此相等,您需要根据条件决定使用哪个值。我这样做的策略是获取2个以上元素的索引,并检查决胜局的补充列表。我的问题是如何获取与您的元素相同的列表中元素的索引列表?强文本

  

示例输入:

     

array, element_of_interest = [2,9,6,3,9], 9

     

示例输出:

     

indices = [1,4]

我包括了我的代码,但目前有点集群。提供了问题的说明以及如果对您的回答有所帮助的我的策略。

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

0 个答案:

没有答案
相关问题