Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 给定2d数组,长度为K且总和为S的子序列数_Python 3.x_Recursion_Substring - Fatal编程技术网

Python 3.x 给定2d数组,长度为K且总和为S的子序列数

Python 3.x 给定2d数组,长度为K且总和为S的子序列数,python-3.x,recursion,substring,Python 3.x,Recursion,Substring,我希望在给定一个数组的情况下,找出长度为K且总和为S的子序列的数目 样本输入: a=[1,1,1,2,2] & K=2 & S=2 3 {because a[0],a[1]; a[1]a[2]; a[0]a[2] are only three possible for the case} 样本输出: a=[1,1,1,2,2] & K=2 & S=2 3 {because a[0],a[1]; a[1]a[2]; a[0]a[2] are only thre

我希望在给定一个数组的情况下,找出长度为K且总和为S的子序列的数目

样本输入:

a=[1,1,1,2,2] & K=2 & S=2
3 {because a[0],a[1]; a[1]a[2]; a[0]a[2] are only three possible for the case}
样本输出:

a=[1,1,1,2,2] & K=2 & S=2
3 {because a[0],a[1]; a[1]a[2]; a[0]a[2] are only three possible for the case}
我曾尝试在Python中为starter编写一个递归循环,但它没有给出预期的输出。请您帮助我找到可能缺少的漏洞

def rec(k, sum1, arr, i=0):                                 
    #print('k: '+str(k)+' '+'sum1: '+str(sum1))     #(1) BaseCase: 
    if(sum1==0 and k!=0):                           #    Both sum(sum1) required and 
        return 0                                    #    numbers from which sum is required(k)
    if(k==0 and sum1 !=0):                          #    should be simultaneously zero
        return 0                                    #    Then required subsequences are 1
    if(k==0 and sum1==0 ):                          # 
        return 1                                    #
    base_check = sum1!=0 or k!=0                    #(2) if iterator i reaches final element 
    if(i==len(arr) and base_check):                 #    in array we should return 0 if both k 
        return 0                                    #    and sum1 aren't zero
                                                    #    func rec for getting sum1 from k elements 
    if(sum1<arr[0]):                                #    takes either first element or rejects it
        ans=rec(k-1,sum1,arr[i+1:len(arr)],i+1)     #    so 2 cases in else loop 
        print(ans)                                  #    i is taken in as iterator to provide array 
    else:                                           #    input to rec func from 2nd element of array 
        ans=rec(k-1, sum1-arr[0], arr[i+1:len(arr)],i+1)+rec(k, sum1, arr[i+1:len(arr)],i+1)
        #print('i: '+str(i)+' ans: '+str(ans))      
    return(ans)

a=[1,1,1,2,2]
print(rec(2,2,a))
def rec(k,sum1,arr,i=0):
#打印('k:'+str(k)+'+'sum1:'+str(sum1))#(1)基本情况:
如果(sum1==0和k!=0):#需要和(sum1)
返回0个需要求和的数字(k)
如果(k==0和sum1!=0):#应该同时为零
返回0#则所需的子序列为1
如果(k==0和sum1==0):#
返回1#
基本检查=sum1=0或k=0#(2)如果迭代器i到达最终元素
如果(i==len(arr)和base_check):#在数组中,如果k和
返回0,sum1不是零
#用于从k元素获取sum1的func rec
如果(使用
itertools.compositions
函数返回给定长度的所有子序列。然后我们过滤以只保留求和到所需值的子序列

导入itertools
def countsubsum(a、k、s):
返回和(1表示itertools中的c。如果和(c)=s,则组合(a,k)
修复代码 您的代码看起来很好,但有两件事情似乎是错误的

这是什么
如果
用于什么?


一开始我对
if(sum1)有点困惑。这真的很有帮助。非常感谢你的回答!!