长输入时抛出索引错误,但小输入时工作正常

时间:2019-07-17 18:00:24

标签: python-3.x list

我一直在练习Hackerrank。但是我无法调试我的代码。我在一些打印语句的帮助下发现,将部分数字从大列表复制到小列表时,不会复制完整的子列表。有人可以帮我吗?

这是我正在解决的问题的link

这是我为该问题编写的代码。

import math
import os
import random
import re
import sys
import bisect

# Complete the activityNotifications function below.
def activityNotifications(expenditure, d):
    temp = []
    temp.extend(expenditure[0:d])
    # Must be initially sorted once
    temp.sort()
    #print(len(temp), len(expenditure[:d]), d, len(expenditure))
    mid, mid1 = None, None
    mid = d // 2
    #print(d, temp, mid, len(temp))
    med = temp[mid]
    if d % 2 == 0:
        mid1 = mid-1
        med += temp[mid]
        med /= 2
    cnt = 0
    for i in range(d, len(expenditure)):
        #print(temp, d, expenditure[i])
        if expenditure[i] >= 2*med:
            cnt += 1
        idx = bisect.bisect(temp, expenditure[i-d])
        #print(idx)
        del temp[idx]
        bisect.insort(temp, expenditure[i])
        med = temp[mid]
        if mid1 != None:
            med += temp[mid1]
            med /= 2

    return cnt


if __name__ == '__main__':
    nd = input().split()

    n = int(nd[0])

    d = int(nd[1])

    expenditure = list(map(int, input().rstrip().split()))

    result = activityNotifications(expenditure, d)

    print(str(result) + '\n')

这为此test case引发了错误。

0 个答案:

没有答案