2017-08-09 113 views
1

我测试我的代码为“查找唯一编号”,我遇到Codewars中止:进程已终止。它需要较长的时间比12000ms完成

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 
+0

“这也可以通过inefficent代码造成的。”在真实的环境中运行你的代码。完成时间是否超过12秒? – Carcigenicate

+0

这看起来像你的代码被太慢的服务器杀死。您可以尝试将您的'find_uniq'函数提交给https://codereview.stackexchange.com/以获得提高速度的帮助。 –

+0

您发布的代码中没有任何内容可以解释长运行时间。即使92ms似乎也相当长。 –

回答

0

对于非常大的集合,这是效率低下的,因为您多次检查冗余集合元素。例如,在您的第一个测试集中,您将检查五次。如果你想节省几微秒

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

,只要抓住而不是弹出的第一个元素平:试试这个修改

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

确实,这是我的错误。非常感谢! 'set'解决了这个问题。 –