如何在Python中递归地写这个和的总和? [英] How to write this sum of a sum recursively in python?

查看:222
本文介绍了如何在Python中递归地写这个和的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么写这个函数:我的问题(第一个答案),在Python中递归?这是我迄今为止:

  def f(n,p,k,t):
sum(在xrange(1,7)中为p:
sum(for x in(1,7)中的i:
if n == 3:return 1
if k == 1:return 0
return(1/36)* f(n-1,p,k-1,t-max(p,i))



在xrange(1,7)中打印总和([f(5,j,3,15)]]

任何帮助表示感谢,谢谢! :D



编辑:链接中的问题是这样的:

假设我有5(n),6(d)个正常的骰子,那么我怎样才能知道有多少可能的卷轴,前3(k)个数字滚动的位置等于15(t)?用
f(n,d,k,t)=Σi= 1jf(something,with,n,d,k,t ...)
这样的递归来做这个事情,我的答案是:



我得到的答案是:

<如果我们添加一个参数p,而不是当前最高的k(并且抛弃d,因为所有的骰子都是6边的),那么我相信我们可以得到以下结论:
f(n,p,k,t)=Σp'=16Σi=16136⋅f(n-1,p',k-1,t-(max(p',i)))
变量i表示下一次死亡的结果。



我不知道这是否正确。我只是对这个问题感兴趣,想要去尝试一下。这是我想出来的。

15的最终概率将是
Σp= 16f(5,p,3,15)$




$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'那么你可以看到所有可以直接到达A的情况,并将到达这些状态的概率乘以从前置状态到达A的概率。那么你总结了所有的前州。

我没有复制它的原因,是因为西格玛符号和LaTeX零星不显示

解决方案



For循环与生成器表达式



For循环:

 语句()

生成器表达式:

 表达式()在p范围内(1,7)

请注意,没有冒号,并且值之前的



如果语句与条件表达式



if语句:

 <$ ():
true_stmt()
else:
false_stmt()

如果表达式:

  true_expr()if predicate()else false_expr()



放在一起

  def f(n,p,k,t ):
return sum(sum(1 if if == 3 else
(0 if if == == else else
(1/36)* f(n-1,p,k- 1,t-max(p,i))))
在范围内(1,7))
在范围内(p,p)


How would I write this function: My Question (the first answer), in python recursively? This is what I have so far:

def f(n, p, k, t):
    sum(for p in xrange(1, 7):
        sum(for i in xrange(1,7):
                if n == 3: return 1
                if k == 1: return 0
                return (1/36) * f(n-1,p,k-1,t-max(p,i))
            )
        )

print sum([f(5,j,3,15) for j in xrange(1, 7)])

Any help appreciated, Thank you! :D

Edit: The question from the link is this:

"Let's say that I have 5 (n), 6-sided (d) normal dice. How would I figure out how many possible rolls there are, where the top 3 (k) numbers rolled, equal 15 (t)? How would I do this using recursion such as f(n,d,k,t)=∑i=1jf(something,with,n,d,k,t...) where the base cases are something else. How would I figure this out? Please help. Thank you."

The Answer I got was:

Going off of my comment, if we add a parameter p being the top die not in the current top k (and discard the d, because all the dice are 6-sided anyways), then I believe we get to the following: f(n,p,k,t)=∑p′=16∑i=16136⋅f(n−1,p′,k−1,t−(max(p′,i))) The variable i represents the result of next die being thrown.

I do not know if this is correct. I was just facinated with the question and wanted to have a go at it. This is what I came up with.

The final probability of sum 15 would then be ∑p=16f(5,p,3,15) with recursion base cases at n=3,k=1.

The general idea behind coming up with recursions like this is the following: You want to know the probability of reaching a state A. Then you look at all cases from which A is immedately reachable and multiply the probability of reaching those states with the probability of reaching A from that 'pre-state'. Then you sum this up over all pre-states.

The reason I did'nt copy it over, is because the sigma notations and LaTeX bits and pieces don't show up in stackoverflow.

解决方案

You just have some of the bits mixed around.

For loops versus generator expressions

For loop:

for p in range(1, 7):
    statement()

Generator expression:

expression() for p in range(1, 7)

Note that there is no colon and the value goes before the for.

If statements versus conditional expressions

If statement:

if predicate():
    true_stmt()
else:
    false_stmt()

If expression:

true_expr() if predicate() else false_expr()

Putting it together

def f(n, p, k, t):
    return sum(sum(1 if n == 3 else
                   (0 if k == 1 else
                    (1/36) * f(n-1, p, k-1, t-max(p,i))))
                   for i in range(1, 7))
               for p in range(1, 7))

这篇关于如何在Python中递归地写这个和的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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