gpt4 book ai didi

python - 查找奇偶校验异常值 Python

转载 作者:行者123 更新时间:2023-11-30 21:53:46 25 4
gpt4 key购买 nike

def find_outlier(lstOfints):
for integer in lstOfints:
#print(integer)
if integer % 2 ==1:
integer
elif integer % 2 == 0:
integer
return integer

我不断得到错误的答案,但我不知道发生了什么事。这就是问题所在:

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

最佳答案

这似乎对我有用:

def find_outlier(lstOfints):
if lstOfints[0] % 2 == 1 and lstOfints[1] % 2 == 1:
for integer in lstOfints[2::]:
if integer % 2 == 0:
return integer
elif lstOfints[0] % 2 == 0 and lstOfints[1] % 2 == 0:
for integer in lstOfints[2::]:
if integer % 2 == 1:
return integer
else:
if lstOfints[0] % 2 == lstOfints[2] % 2:
return lstOfints[1]
else:
return lstOfints[0]

首先,我看看这个列表是全是赔率还是全是偶数(除了异常值)。然后,如果它是偶数,它会遍历列表寻找奇数,如果列表都是奇数,则相同。但是,如果离群值位于前两个数字中,则它会通过将模数与列表中第三个数字的模数进行比较来检查离群值是第一个数字还是第二个数字。

关于python - 查找奇偶校验异常值 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59538186/

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