gpt4 book ai didi

python - Codewars 中止 : process was terminated. 完成时间超过 12000 毫秒

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:56:08 25 4
gpt4 key购买 nike

我正在测试“查找唯一编号”的代码,我遇到了

STDERR:

Process was terminated. It took longer than 12000ms to complete

SIGKILL Process exited prematurely with a SIGKILL signal. Server Execution Error:

The server timed out waiting for the code to finish executing. It is possible that this is due to high server load. It may also be caused by inefficent code. Please try your request again.

这经常发生在我的代码中。这与代码质量有关,还是服务器问题?我希望它指的是我的代码。

find_uniq = lambda a: [x for x in a if a.count(x) == 1].pop()
# I have passed all of this tests
# btw I'm using python 3
test.assert_equals(find_uniq([ 1, 1, 1, 2, 1, 1 ]), 2)
test.assert_equals(find_uniq([ 0, 0, 0.55, 0, 0 ]), 0.55)
test.assert_equals(find_uniq([ 3, 10, 3, 3, 3 ]), 10)
Time: 92ms Passed: 3 Failed: 0

最佳答案

对于非常大的集合,这是低效的,因为您会多次检查冗余的集合元素。例如,在您的第一个测试集中,您将检查 1 五次。试试这个修改:

find_uniq = lambda a: [x for x in set(a) if a.count(x) == 1].pop()

如果你想节省几微秒,直接抓取第一个元素而不是popping:

find_uniq = lambda a: [x for x in set(a) if a.count(x) == 1] [0]

关于python - Codewars 中止 : process was terminated. 完成时间超过 12000 毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45601000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com