有没有办法编写一个递归函数来查看列表中的所有整数并查看是否有任何两个整数等于负数? [英] Is there a way to write a recursive function that looks through all the integers in a list and sees if any two are equal to a negative sum?

查看:58
本文介绍了有没有办法编写一个递归函数来查看列表中的所有整数并查看是否有任何两个整数等于负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以问题是,你有一个整数列表,你必须找出列表中的任何两个和是否为负数.

现在我有这个

def negsum(L):如果 len(L) <= 2:if L[0] + L[1] >= -1: #这是我的基本情况返回错误别的:返回真别的:如果 L[0] + L[1] <= -1:返回真别的:return negsum(L[1:]) #递归部分

我的代码的问题是它只检查列表中的前 2 个.所以在一个列表中[-10, 15, 30, -5] 当它应该是真的时你会得到假,因为 -5 + -10 是一个负和.我的函数只检查:

-10 + 15

15 + 30

30 - 5

我怎样才能得到它以便它使用递归检查 -10 + 30、-10 -5 和 15-5?

编辑,我忘了提及,只允许使用 len() [] 和 : 运算符.没有循环.这甚至可能没有循环吗?

解决方案

这是一个没有循环(隐式或显式)的解决方案:

def negsum(lst, sub=True):返回 len(lst) >1 \和 ((lst[0] + lst[1]) <0或 negsum([lst[0]]+lst[2:], False)或 (sub 和 negsum(lst[1:])))

或者,以下版本更清晰地将流程分为 2 个子功能,并且不需要附加参数sub":

def negsum(lst):def first_with_others(lst): # 将 lst[0] 与所有后面的值进行比较如果 len(lst) >1:#print("summing", lst[0], "and", lst[1])返回 ((lst[0] + lst[1]) <0) 或 first_with_others([lst[0]]+lst[2:])def drop_first(lst): # 连续删除第一个元素如果 lst:返回 first_with_others(lst) 或 drop_first(lst[1:])return drop_first(lst) 或 False # 将 None 转换为 False

取消对打印函数调用的注释显示计算了哪些总和:

<预><代码>>>>负数([1,2,3,4,5])求和 1 和 2求和 1 和 3求和 1 和 4求和 1 和 5求和 2 和 3求和 2 和 42 和 5 相加3 和 4 相加3 和 5 相加4 和 5 相加错误的>>>负数([-1,2,3,4,5])求和 -1 和 2求和 -1 和 3求和 -1 和 4求和 -1 和 5求和 2 和 3求和 2 和 42 和 5 相加3 和 4 相加3 和 5 相加4 和 5 相加错误的>>>负数([-1,2,3,-4,5])求和 -1 和 2求和 -1 和 3求和 -1 和 -4真的>>>负数([-2,1])求和 -2 和 1真的

So the problem is, you have a list of integers and you have to find if any two in the list sum to a negative.

right now I have this

def negsum(L):
    if len(L) <= 2:
        if L[0] + L[1] >= -1: #Here is my base case
            return False
        else:
            return True
    else: 
        if L[0] + L[1] <= -1:
            return True
        else:
            return negsum(L[1:]) #Recursive portion

The issue with my code is it only checks the first 2 in the list. So in a list [-10, 15, 30, -5] you get False when it should be true, because -5 + -10 is a negative sum. My function only checks:

-10 + 15

15 + 30

30 - 5

How can I get it so that it checks -10 + 30, -10 -5 and 15-5 using recursion?

Edit, I forgot to mention, only len() [] and : operators are allowed. No loops. Is this even possible without loops?

解决方案

Here's a solution without loops (implicit or explicit):

def negsum(lst, sub=True):
    return len(lst) > 1 \
        and ((lst[0] + lst[1]) < 0
                or negsum([lst[0]]+lst[2:], False)
                or (sub and negsum(lst[1:])))

or, alternatively, the following version separates the process more cleanly into 2 sub-functions and doesn't need the additional parameter 'sub':

def negsum(lst):
    def first_with_others(lst): # compare lst[0] with all later values
        if len(lst) > 1:
            #print("summing", lst[0], "and", lst[1])
            return ((lst[0] + lst[1]) < 0) or first_with_others([lst[0]]+lst[2:])
    def drop_first(lst): # successively drop first element
        if lst:
            return first_with_others(lst) or drop_first(lst[1:])
    return drop_first(lst) or False # converts None to False

Uncommenting the call to the print function shows which sums are calculated:

>>> negsum([1,2,3,4,5])
summing 1 and 2
summing 1 and 3
summing 1 and 4
summing 1 and 5
summing 2 and 3
summing 2 and 4
summing 2 and 5
summing 3 and 4
summing 3 and 5
summing 4 and 5
False
>>> negsum([-1,2,3,4,5])
summing -1 and 2
summing -1 and 3
summing -1 and 4
summing -1 and 5
summing 2 and 3
summing 2 and 4
summing 2 and 5
summing 3 and 4
summing 3 and 5
summing 4 and 5
False
>>> negsum([-1,2,3,-4,5])
summing -1 and 2
summing -1 and 3
summing -1 and -4
True
>>> negsum([-2,1])
summing -2 and 1
True

这篇关于有没有办法编写一个递归函数来查看列表中的所有整数并查看是否有任何两个整数等于负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆