Unyeilding一个排列发生器 [英] Unyeilding a permutation generator

查看:80
本文介绍了Unyeilding一个排列发生器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,有人可以帮忙。


我在 http://code.activestate.com/recipes/252178/

def all_perms(str):

if len(str)< = 1:

yield str

else:

for perm in all_perms(str [1: ]):

for i in range(len(perm)+1):

#nb str [0:1]在字符串和列表上下文中都有效

收益烫发[:i] + str [0:1] +烫发[i:]


允许我做一些事情,比如

for x in all_permx(" ABCD"):

print x


我相信它正在利用简单的变化/约翰逊Trotter

算法。

有人可以确认一下吗?


无论如何我想做的是试验类似的代码(即

相同算法并保持递归)在其他语言中,

特别是vbscript并且想知道什么它看起来好像是因为它被重写为不使用yield语句 - 或者至少如果它被修改为
,以便它可以很容易地翻译成其他语言

没有python'的yield语句。我认为声明for perm in

all_perms(str [1:]):例如,在递归的

vbscript程序中最难复制。


感谢您给予的任何建设性帮助。


Hal

Hello, can someone please help.

I found the following code at http://code.activestate.com/recipes/252178/

def all_perms(str):
if len(str) <=1:
yield str
else:
for perm in all_perms(str[1:]):
for i in range(len(perm)+1):
#nb str[0:1] works in both string and list contexts
yield perm[:i] + str[0:1] + perm[i:]

which allows me to do things like

for x in all_permx("ABCD"):
print x

I believe it is making use of the plain changes / Johnson Trotter
algorithm.
Can someone please confirm?

Anyway what I want to do is experiment with code similar to this (i.e.
same algorithm and keep the recursion) in other languages,
particularly vbscript and wondered what it would look like if it was
rewritten to NOT use the yield statement - or at least if it was
amended so that it can be easily translated to other languages that
dont have python''s yield statement. I think the statement "for perm in
all_perms(str[1:]):" will be hardest to replicate in a recursive
vbscript program for example.

Thanks for any constructive help given.

Hal

推荐答案

si ***** *@yahoo.com 写道:

无论如何,我想做的是尝试与此类似的代码(即

相同的算法,并保持递归)在其他语言,

特别是vbscript,并想知道它是什么样子,如果它是/ b $ b重写为不使用yield语句 -
Anyway what I want to do is experiment with code similar to this (i.e.
same algorithm and keep the recursion) in other languages,
particularly vbscript and wondered what it would look like if it was
rewritten to NOT use the yield statement -



如果没有yield语句并保持相同的空间复杂度,那么你需要将b * b包装在一个数据对象中的枚举状态

可以明确枚举。这反过来可能意味着你必须将
的递归解除为老式堆栈操作。

Without the yield statement and keeping the same space complexity, you
basically have to wrap the enumeration state in a data object that you
can enumerate over explicitly. That in turn may mean you have to
unwind the recursion into old fashioned stack operations.


si ****** @ yahoo.com 写道:

无论如何,我想做的是尝试使用与此类似的代码(即

相同的算法并保持递归)在其他语言中,

特别是vbscript,并想知道它是什么样子如果它是

重写为不使用yield语句
Anyway what I want to do is experiment with code similar to this (i.e.
same algorithm and keep the recursion) in other languages,
particularly vbscript and wondered what it would look like if it was
rewritten to NOT use the yield statement



明显(尽管内存效率低)替换是累积

列表并返回列表。将结果变量初始化为空列表,

而不是产生元素,将它们附加到结果变量。

然后在函数末尾返回结果变量。


HTH,

-

Carsten Haese
http://informixdb.sourceforge.net


11月2日, 3:34 * pm,傻... @ yahoo.com写道:
On Nov 2, 3:34*pm, silly...@yahoo.com wrote:

您好,有人可以帮忙。


我发现以下代码:http://code.activestate.com/recipes/252178/

def all_perms(str):

* * if len (str)< = 1:

* * * *收益率

* *其他:

* * * * for perm in all_perms (str [1:]):

* * * * * *我在范围内(len(perm)+1):

* * * * * * * * #nb str [0:1]适用于字符串和列表上下文

* * * * * * * * yield perm [:i] + str [0:1] + per m [i:]


允许我在* all_permx(" ABCD")中执行类似


的事情:

*打印x


我相信它正在利用普通的变化/ Johnson Trotter

算法。

有人可以确认一下吗?


无论如何,我想做的是试验类似于此的代码(即

相同算法并保持递归)其他语言,

特别是vbscript,并想知道它是什么样子,如果它是/ b $ b重写为不使用yield语句 - 或者至少如果它是
$修改了b $ b,以便它可以很容易地翻译成其他语言,即
没有python'的yield语句。我认为声明for perm in

all_perms(str [1:]): *例如,在递归的

vbscript程序中最难复制。


感谢您给予的任何建设性帮助。


Hal
Hello, can someone please help.

I found the following code athttp://code.activestate.com/recipes/252178/

def all_perms(str):
* * if len(str) <=1:
* * * * yield str
* * else:
* * * * for perm in all_perms(str[1:]):
* * * * * * for i in range(len(perm)+1):
* * * * * * * * #nb str[0:1] works in both string and list contexts
* * * * * * * * yield perm[:i] + str[0:1] + perm[i:]

which allows me to do things like

for x in *all_permx("ABCD"):
* print x

I believe it is making use of the plain changes / Johnson Trotter
algorithm.
Can someone please confirm?

Anyway what I want to do is experiment with code similar to this (i.e.
same algorithm and keep the recursion) in other languages,
particularly vbscript and wondered what it would look like if it was
rewritten to NOT use the yield statement - or at least if it was
amended so that it can be easily translated to other languages that
dont have python''s yield statement. I think the statement "for perm in
all_perms(str[1:]):" *will be hardest to replicate in a recursive
vbscript program for example.

Thanks for any constructive help given.

Hal



我认为多线程是最真实的原来的。您可能

开发一个框架来设置特定生成器时的事件

转一圈,将它们的收益率本身放在特定的地方,设置

返回事件等,并重复使用它。当然,在
VBS中启动线程可能是另一回事。

I think multi-threading is the "truest" to the original. You might
develop a framework to set events when particular generators are to
take a turn, place their "yields", per se, in a particular place, set
the return event, etc., and reuse it. Of course, starting threads in
VBS might be another matter.


这篇关于Unyeilding一个排列发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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