繁体   English   中英

删除朋友hackerearth:运行时错误 - NZEC :IndexError: deque index out of range

[英]Remove Friends hackerearth: Runtime Error - NZEC :IndexError: deque index out of range

问题陈述 ( https://www.hackerearth.com/problem/algorithm/remove-friends-5 )

我被困在这个问题上。 请指出什么是错误的以及可以做什么以获得正确的输出。

谢谢你的帮助

获得博士学位后,克里斯蒂成为了她大学的名人,她的 facebook 个人资料中满是好友请求。 作为一个好女孩,克里斯蒂接受了所有的要求。

现在 Kuldeep 嫉妒她从其他男人那里得到的所有关注,所以他要求她从她的朋友列表中删除一些男人。 为了避免“场景”,Christie 决定从她的朋友列表中删除一些朋友,因为她知道她拥有的每个朋友的受欢迎程度,她使用以下算法删除一个朋友。

算法删除(朋友):

         DeleteFriend=false

 for i = 1 to Friend.length-1

 if (Friend[i].popularity < Friend[i+1].popularity)

    delete i th friend

    DeleteFriend=true

    break
if(DeleteFriend == false)

delete the last friend

输入:第一行包含 T 个测试用例。 每个测试用例的第一行包含 N,Christie 当前拥有的朋友数量和 K,Christie 决定删除的朋友数量。 下一行包含她的朋友的人气,以空格分隔。

输出:对于每个测试用例,在删除 K 个朋友后打印代表 Christie 朋友受欢迎程度的 NK 个数字。

注意 删除恰好 K 个朋友后的朋友顺序应保持在输入中给出。

样本输入:

3

3 1

3 100 1

5 2

19 12 3 4 17

5 3

23 45 11 77 18

样品输出

100 1

19 12 17

77 18

我的解决方案:

import collections as col

def delete(n, k, frnd):

    temp = []

    while k!=0:   

        for f in frnd:

            if f<f+1:

                del frnd[f]

                k -= 1

    temp.append(frnd)

print(" ".join(map(str, temp))) 

for i in range(int(input())):

  n, k = (map(int, input().split()))

  frnd = col.deque(list(map(int, input().split())))

  delete(n, k, frnd)

要修复错误,请使用将您的for循环替换for

for idx, f in enumerate(frnd):
    if f<f+1:
        del frnd[idx]
        break # <------

您收到错误是因为您使用的是值而不是索引来删除deque的条目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM