python函数递归求和详解_在Python中递归求和子集

1586010002-jmsa.png

I will be happy to get some help.

I have the following problem:

I'm given a list of numbers seq and a target number and I need to write 2 things:

A recursive solution that returns True if there is a sum of a subsequence that equals the target number and False otherwise.

example:

subset_sum([-1,1,5,4],0) # True

subset_sum([-1,1,5,4],-3) # False

Secondly, I need to write a solution using what I wrote in the previous solution

but now with memoization that uses a dictionary in which the keys are tuples:

(len(seq),target)

For number 1 this is what I got to so far:

def subset_sum(seq, target):

if target == 0:

return True

if seq[0] == target:

return True

if len(seq) > 1:

return subset_sum(seq[1:],target-seq[0]) or subset_sum(seq[1:],target)

return False

Not sure I got it right so if I could get some input I will be grateful.

For number 2:

def subset_sum_mem(seq, target, mem=None ):

if not mem:

mem = {}

key=(len(seq),target)

if key not in mem:

if target == 0 or seq[0]==target:

mem[key] = True

if len(seq)>1:

mem[key] = subset_sum_mem(seq[1:],target-seq[0],mem) or subset_sum_mem(seq[1:],target,mem)

mem[key] = False

return mem[key]

I can't get the memoization to give me the correct answer so I'd be glad for some guidance here.

Thanks for anyone willing to help!

解决方案

Just for reference, here's a solution using dynamic programming:

def positive_negative_sums(seq):

P, N = 0, 0

for e in seq:

if e >= 0:

P += e

else:

N += e

return P, N

def subset_sum(seq, s=0):

P, N = positive_negative_sums(seq)

if not seq or s < N or s > P:

return False

n, m = len(seq), P - N + 1

table = [[False] * m for x in xrange(n)]

table[0][seq[0]] = True

for i in xrange(1, n):

for j in xrange(N, P+1):

table[i][j] = seq[i] == j or table[i-1][j] or table[i-1][j-seq[i]]

return table[n-1][s]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值