英文:
Re: checking if triplets of integers occur in a list - why does my code give the wrong answer for one specific test case?
问题
这是给定的任务:
编写一个名为 find_if_triplet() 的函数,它接收一个参数 my_list,这是一个升序排列的整数列表。
该函数返回一个布尔值 has_triplet,如果列表 my_list 中的某个元素出现至少三次,则设置为 True,否则返回 False。
例如,如果 my_list = [1,1,1],则函数应返回 has_triplet = True;如果 my_list = [1,2,3,3,3],则函数应返回 has_triplet = True;如果 my_list = [1,2,2,2,2,3],则函数应返回 has_triplet = True;如果 my_list = [1,2,2,3,4,4,5],则函数应返回 has_triplet = False。
我尝试使用 Python 的 count() 函数来检查列表中的整数是否至少出现三次。虽然它对大多数测试用例有效,例如当 my_list = [1,1,1] 或 [1,2,3,3,3] 或 [1,2,2,3,4,4,5] 时,但当 my_list =[1,2,2,2,2,3] 时,它不起作用。它不是返回 True(有三个相同的元素),而是错误地返回 False。这可能是为什么?
def find_if_triplet(my_list):
for i in my_list:
instances = my_list.count(i)
if instances >= 3:
has_triplet = True
else:
has_triplet = False
return has_triplet
英文:
Here is the task I am given:
Write a function find_if_triplet(), which receives a single parameter, my_list, a list of integers, with values in ascending order.
The function returns a single boolean, has_triplet, which is set to True if an element of the list my_list appears at least three times. It returns False otherwise.
For instance,
if my_list = [1,1,1], the function should return has_triplet = True, if my_list = [1,2,3,3,3], the function should return has_triplet = True, if my_list = [1,2,2,2,2,3], the function should return has_triplet = True, if my_list = [1,2,2,3,4,4,5], the function should return has_triplet = False.
I tried to use the Python count() function to check if an integer in the list occurs at least thrice. While it works for most test cases i.e. when my_list = [1,1,1] or [1,2,3,3,3] or [1,2,2,3,4,4,5], when my_list =[1,2,2,2,2,3], it doesn't. Instead of the boolean returning a True (has triplets) it incorrectly returns False. Why could this be?
def find_if_triplet(my_list):
for i in my_list:
instances = my_list.count(i)
if instances >= 3:
has_triplet = True
else:
has_triplet = False
return has_triplet
答案1
得分: 1
以下是翻译好的内容:
原问题中的缩进存在问题。可能需要的是这样的代码:
from collections import Counter
def find_if_triplet(my_list: list[int]) -> bool:
if len(my_list) > 2:
for x in set(my_list):
if my_list.count(x) > 2:
return True
return False
# 如果你更喜欢让 Counter 来完成所有工作,可以使用以下代码:
def find_if_triplet2(my_list: list[int]) -> bool:
if len(my_list) > 2:
_, n = Counter(my_list).most_common()[0]
return n > 2
return False
lst = [1, 2, 2, 2, 2, 3]
print(find_if_triplet(lst))
print(find_if_triplet2(lst))
输出结果:
True
True
注意:
输入列表按升序排序对这个实现没有影响。
英文:
The indentation in the original question is flawed. What's probably needed is this:
from collections import Counter
def find_if_triplet(my_list: list[int]) -> bool:
if len(my_list) > 2:
for x in set(my_list):
if my_list.count(x) > 2:
return True
return False
# if you prefer Counter to do all the work for you then:
def find_if_triplet2(my_list: list[int]) -> bool:
if len(my_list) > 2:
_, n = Counter(my_list).most_common()[0]
return n > 2
return False
lst = [1,2,2,2,2,3]
print(find_if_triplet(lst))
print(find_if_triplet2(lst))
Output:
True
True
Note:
The fact that the input list is sorted (ascending) is irrelevant in this implementation
答案2
得分: 0
由于您正在循环遍历列表,然后对每个元素使用 list.count
,因此对于每个元素,此函数计算整个数组中该数字的频率。
此外,在您的代码中,对于任何值,例如在 [1,2,2,2,2,4]
中,对于每个值,has_triplet
的值会在每个索引上更改为 [False, True, True, True, True, False]
,因为您获取的是 has_triplet
的最终值作为返回值,只关心最后一个数字是否为 has_triplet
True。
为了解决这个问题,您可以使用计数器函数或将您的计数器函数修改为:
def find_if_triplet(array):
has_triplet = False
freq = {}
for num in array:
if num not in freq:
freq[num] = 0
freq[num] += 1
if freq[num] >= 3:
has_triplet = True
break # 一旦找到三重子数组,停止搜索并结束循环
return has_triplet
英文:
since you are looping through the list and then using list.count
on each element, then for each element this function count the frequency of that number in entire array for each value.
also in your code, if for any value lets say in [1,2,2,2,2,4]
for each value has_triplet value changes as (for each index) [False, True, True, True, True, False]
since you are taking the final value of has_triplet as return, you are getting final value ie wheter if last number is has_triplet True or not only.
so to solve this issue, you can use counter fujnction or make your counter function as
def find_if_triplet(array):
has_triplet = False
freq = {}
for num in array:
if num not in freq:
freq[num] = 0
freq[num] += 1
if freq[num] >= 3:
has_triplet = True
break # once found triplet stop search further and end the looping
return has_triplet
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论