迭代器?产生所有可能组合的方式? [英] iterator? way of generating all possible combinations?

查看:82
本文介绍了迭代器?产生所有可能组合的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这真让我恼火。我确定有不同的方式来做这件事 - 我对算法感兴趣,而不是实际的解决方案,

我更想尝试玩迭代器和递归。我想创建一个

a程序,它可以生成一组

骰子的各种组合,并带有两个边。


so我开始使用迭代器


class die(object):

def __init __(self,sides):

self.sides =范围(1,边+ 1)

def __iter __(自我):

返回自我

def next(self):

self.sides = self.sides [1:] + [self.sides [0]]

返回self.sides [-1]
现在
我的想法是创建一个递归函数来遍历

所有骰子组合。不幸的是......我想要一个死的

结束。我已经尝试使用不同版本的模具,即

一个没有无限循环,但取而代之的是起始

的位置。各种各样的东西,但我找不到一个很好的递归

函数,它不属于骰子类。


有什么想法吗?或更好的谷歌条款?我试过了。


谢谢

解决方案


< a href =mailto:ak ********* @ gmail.com> ak ********* @ gmail.com 写道:

好的,这真让我烦恼。我确定有不同的方法可以做到这一点 - 我对算法感兴趣,而不是实际解决方案,
我更想尝试使用迭代器和递归。我想创建一个程序,它可以生成一组n /骰子的各种可能组合。
。 。 。有任何想法吗?或更好的谷歌条款?我试过了。




ASPN Cookbook中有几个有趣的花絮:
http://aspn.activestate.com/ASPN/sea...ype=Subsection

http://tinyurl.com/frxqz


雷蒙德




< ak ******** *@gmail.com>在消息中写道

news:11 ********************** @ i40g2000cwc.googlegr oups.com ...

好的,这真让我恼火。我确定有不同的方法可以做到这一点 - 我对算法感兴趣,而不是实际解决方案,
我更想尝试使用迭代器和递归。我想创建一个程序来生成一组
骰子的每个可能组合,以及两侧。




骰子是否完全相同或可区分(标记)。换句话说,用2

骰子,1,2与2,1相同或不同?请注意,在大多数骰子游戏中,如掷骰子这样的骰子,骰子都没有区别,但概率计算

必须对它们进行处理,就好像它们要获得基本事件一样。 br $>

Terry Jan Reedy




Terry Reedy写道:
< blockquote class =post_quotes>
骰子是相同的还是可区分的(标记的)。换句话说,用2个骰子,1,2是相同的2,1还是不同的?请注意,在大多数骰子游戏中,如掷骰子,掷骰子都没有区别,但概率计算必须像对待基本事件一样对它们进行深思熟虑。



他们是截然不同的。这不一定是模拟游戏。所以

是骰子被标记,我想跟踪那些不同的

possibilties。


Ok, this is really irritating me. I''m sure there are different ways of
doing this - I''m interested in the algo, not the practical solution,
I''m more trying to play with iterators and recursion. I want to create
a program that generates every possible combination of a set of a n
dice, with s sides.

so I started with an iterator

class die(object):
def __init__(self,sides):
self.sides = range(1,sides+1)
def __iter__(self):
return self
def next(self):
self.sides = self.sides[1:] + [self.sides[0]]
return self.sides[-1]
now my thought was to create a recursive function to iterate through
all the dice combinations. Unfortunately... I''m coming up with a dead
end. I''ve tried working it out with different version of the die, ie
one that doesn''t loop infinitely, but instead takes a starting
position. A vaiety of things, and yet I can''t find a nice recursive
function that falls out of the die class.

Any ideas? or better terms to google? cuz I''ve tried.

Thanks

解决方案


ak*********@gmail.com wrote:

Ok, this is really irritating me. I''m sure there are different ways of
doing this - I''m interested in the algo, not the practical solution,
I''m more trying to play with iterators and recursion. I want to create
a program that generates every possible combination of a set of a n
dice, with s sides. . . . Any ideas? or better terms to google? cuz I''ve tried.



There are several interesting tidbits in the ASPN Cookbook:
http://aspn.activestate.com/ASPN/sea...ype=Subsection

http://tinyurl.com/frxqz

Raymond



<ak*********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...

Ok, this is really irritating me. I''m sure there are different ways of
doing this - I''m interested in the algo, not the practical solution,
I''m more trying to play with iterators and recursion. I want to create
a program that generates every possible combination of a set of a n
dice, with s sides.



Are the dice identical or distinguishable (marked). In other words, with 2
dice, is 1,2 the same as 2,1 or different? Note that in most dice games,
such as craps, the dice are not distinguished, but probability calculations
must treast them as if they were to get the elementary events.

Terry Jan Reedy



Terry Reedy wrote:


Are the dice identical or distinguishable (marked). In other words, with 2
dice, is 1,2 the same as 2,1 or different? Note that in most dice games,
such as craps, the dice are not distinguished, but probability calculations
must treast them as if they were to get the elementary events.


they are distinct. This isn''t necessarily about simulating a game. So
yes the dice are marked and I want to track those different
possibilties.


这篇关于迭代器?产生所有可能组合的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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