for x in all_permx(" ABCD"): print x 我相信它正在利用简单的变化/约翰逊Trotter 算法。 有人可以确认一下吗? 无论如何我想做的是试验类似的代码(即 相同算法并保持递归)在其他语言中, 特别是vbscript并且想知道什么它看起来好像是因为它被重写为不使用yield语句 - 或者至少如果它被修改为,以便它可以很容易地翻译成其他语言 没有python'的yield语句。我认为声明for perm in all_perms(str [1:]):例如,在递归的 vbscript程序中最难复制。 感谢您给予的任何建设性帮助。 HalHello, 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 strelse:for perm in all_perms(str[1:]):for i in range(len(perm)+1):#nb str[0:1] works in both string and list contextsyield perm[:i] + str[0:1] + perm[i:]which allows me to do things likefor x in all_permx("ABCD"):print xI believe it is making use of the plain changes / Johnson Trotteralgorithm.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 wasrewritten to NOT use the yield statement - or at least if it wasamended so that it can be easily translated to other languages thatdont have python''s yield statement. I think the statement "for perm inall_perms(str[1:]):" will be hardest to replicate in a recursivevbscript 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 wasrewritten to NOT use the yield statement - 如果没有yield语句并保持相同的空间复杂度,那么你需要将b * b包装在一个数据对象中的枚举状态 可以明确枚举。这反过来可能意味着你必须将的递归解除为老式堆栈操作。Without the yield statement and keeping the same space complexity, youbasically have to wrap the enumeration state in a data object that youcan enumerate over explicitly. That in turn may mean you have tounwind 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 wasrewritten 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, [email protected] 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程序中最难复制。 感谢您给予的任何建设性帮助。 HalHello, 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 likefor x in *all_permx("ABCD"):* print xI believe it is making use of the plain changes / Johnson Trotteralgorithm.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 wasrewritten to NOT use the yield statement - or at least if it wasamended so that it can be easily translated to other languages thatdont have python''s yield statement. I think the statement "for perm inall_perms(str[1:]):" *will be hardest to replicate in a recursivevbscript program for example.Thanks for any constructive help given.Hal 我认为多线程是最真实的原来的。您可能 开发一个框架来设置特定生成器时的事件 转一圈,将它们的收益率本身放在特定的地方,设置 返回事件等,并重复使用它。当然,在 VBS中启动线程可能是另一回事。I think multi-threading is the "truest" to the original. You mightdevelop a framework to set events when particular generators are totake a turn, place their "yields", per se, in a particular place, setthe return event, etc., and reuse it. Of course, starting threads inVBS might be another matter. 这篇关于Unyeilding一个排列发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 06:40