gpt4 book ai didi

python - CodeWars "Find The Parity Outlier"错误?

转载 作者:行者123 更新时间:2023-12-01 03:53:57 26 4
gpt4 key购买 nike

我正在完成this Codewars exercise 。以下是说明:

You are given an array (which will have a length of at least 3, but could be very large) containing integers. The integers in the array are either entirely odd or entirely even except for a single integer N. Write a method that takes the array as an argument and returns N.

For example:

[2, 4, 0, 100, 4, 11, 2602, 36]

Should return: 11

[160, 3, 1719, 19, 11, 13, -21]

Should return: 160

但是,当我提交以下代码时:

def ifeven(list):
#Determine if we are dealing with list of evens or odds
sum = 0
for ran in (0,1,2):
sum += abs(list[ran])%2
avg = sum/3
r_avg = round(avg)
return r_avg == 0

def find_outlier(integers):
even = ifeven(integers)
new = []
for num in integers:
new.append(num%2)
if even:
loc = new.index(1)
else:
loc = new.index(0)
return integers[loc]

使用此测试用例(请参阅练习链接):

test.assert_equals(find_outlier([1,2,3]), 2)

由于某些原因,我收到错误 1 should equal 2即使我在另一个编译器上运行代码,我也会得到 2作为(正确)答案。

是我的代码有问题还是 Codewars 的编译器有问题?

最佳答案

根据/u/Prune 的建议,答案如下:

检查正在使用的Python版本。 Python 2 和 3 对/运算符的处理方式不同。参见这里:Division in Python 2.7. and 3.3

关于python - CodeWars "Find The Parity Outlier"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37821709/

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