该函数未返回任何值。

huangapple go评论68阅读模式
英文:

the function is not returning any value

问题

问题是 - 给定一个整数列表,如果数组中有相邻的3,则返回True。

def has_33(list):
    for i in range(len(list) - 1):
        if list[i] == 3 and list[i + 1] == 3:
            return True
    return False

has_33([3, 3, 3])
英文:

the problem is -- Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.

Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.

def has_33(list):
    for i in list:
        if (i==3 and (i+1)==3):
            return True
        else:
            pass
has_33([3, 3, 3])

答案1

得分: 2

i+1 不是列表的下一个元素,它将1添加到列表的当前元素。

你需要循环遍历列表的索引,而不是值。

def has_33(list):
    for i in range(len(list)-1):
        if list[i] == 3 and list[i+1] == 3:
            return True
    return False

或者你可以使用 zip() 循环遍历元素对。

def has_33(list):
    for current, next in zip(list, list[1:]):
        if current == 3 == next:
            return True
    return False
英文:

i+1 is not the next element of the list, it adds 1 to the current element of the list.

You need to loop over the list indexes, not the values.

def has_33(list):
    for i in range(len(list)-1):
        if list[i] == 3 and list[i+1] == 3:
            return True
    return False

Or you can loop over pairs of elements using zip().

def has_33(list):
    for current, next in zip(list, list[1:]):
        if current == 3 == next:
            return True
    return False

答案2

得分: 0

def has_33(list):
    previous = None
    for i in list:
        if previous is None:
            previous = i
        elif previous == 3 and i == 3:
            return True
        else:
            previous = i
    return False
print(has_33([1, 3, 3]))

比Barmar的答案不够简洁(不够Pythonic),但你也可以存储前一个值以与下一个值进行比较。

英文:
def has_33(list):
    previous = None 
    for i in list:
        if previous is None:
            previous = i
        elif previous == 3 and i == 3:
            return True 
        else:
            previous = i
    return False 
print(has_33([1, 3, 3]))

Less clean than Barmar answer (not pythonic) but you could also store the previous value to compare it with the next one

答案3

得分: 0

你可以将当前数字存储在一个变量中,以便在下一次迭代中与下一个数字进行比较:

def has_33(lst):
    last = None
    for i in lst:
        if i == last == 3:
            return True
        last = i
    return False

你也可以使用 itertools.pairwise 来遍历列表中相邻的值对:

from itertools import pairwise

def has_33(lst):
    return any(a == b == 3 for a, b in pairwise(lst))
英文:

You can store the current number in a variable so it compared with the next number in the next iteration:

def has_33(lst):
    last = None
    for i in lst:
        if i == last == 3:
            return True
        last = i
    return False

You can also use itertools.pairwise to iterate through the list by pairs of adjacent values:

from itertools import pairwise

def has_33(lst):
    return any(a == b == 3 for a, b in pairwise(lst))

答案4

得分: 0

一个迭代器解决方案,重复查找下一个3并检查后续元素。

英文:

An iterator solution, repeatedly finding the next 3 and checking the following element.

def has_33(lst):
    it = iter(lst)
    while 3 in it:
        if next(it, None) == 3:
            return True
    return False

答案5

得分: -2

你应该使用 enumerate 来获取下一个值并比较当前的值。

代码:

def has_33(ls):
    for i, v in enumerate(ls[:-1]):
        if v == 3 and ls[i + 1] == 3:
            return True
    return False
英文:

You should use enumerate for next value and compare the current value

Code:

def has_33(ls):
    for i,v in enumerate(ls[:-1]):
        if v == 3 and ls[i+1] == 3:
            return True
    return False

答案6

得分: -2

The i goes from 1 to len(list)-1 so that we can compare the current and next values. After comparison, the list value is set to -1 just for reference.

def has_33(lst):
    for i in range(0, len(lst)-1):
        if (lst[i]==3 and lst[i+1]==3):
            return True
        else:
            pass
        lst[i]=-1
    return False 

l=[3,3,0]        
k=has_33(l)
print(k)

「i」的取值范围从1到「len(list)-1」,这样我们可以比较当前值和下一个值。比较后,列表的值被设置为-1,仅作参考。

def has_33(lst):
    for i in range(0, len(lst)-1):
        if (lst[i]==3 and lst[i+1]==3):
            return True
        else:
            pass
        lst[i]=-1
    return False 

l=[3,3,0]        
k=has_33(l)
print(k)
英文:

The i goes from 1 to len(list)-1 so that we can compare the current and next values. After comparison, the list value is set to -1 just for reference.

def has_33(lst):
    for i in range(0, len(lst)-1):
        if (lst[i]==3 and lst[i+1]==3):
            return True
        else:
            pass
        lst[i]=-1
    return False 

l=[3,3,0]        
k=has_33(l)
print(k)

huangapple
  • 本文由 发表于 2023年6月8日 15:18:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定