简单的递归帮助 [英] simple recursion help

查看:73
本文介绍了简单的递归帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用元素a,b和c找到长度为x的所有列表。

为此,我创建了下面的类,但它不是很有效,

我无法弄清楚要改变什么。有没有人有任何

的见解?


c级:

def __init __(个体经营):

self.traits = 4#列表元素的数量

self.types = 3#元素可以是0,1或2

def a(self ):

l = []

我在范围内(self.traits):

l.append(self.b(str(i) )))

返回l


def b(self,s):

如果len(s)== self。类型:

返回s

我在范围内(self.traits):

返回self.b(s + str(i))

i = c()

lst = ia()

提前致谢,


- d

Hi, I am trying to find all lists of length x with elements a, b, and c.
To this end, I have created the class below, but it is not quite working and
I am having trouble figuring out what to change. Does anyone have any
insight?

class c:
def __init__(self):
self.traits = 4 # number of list elements
self.types = 3 # elements can be 0, 1, or 2

def a(self):
l = []
for i in range(self.traits):
l.append(self.b(str(i)))
return l

def b(self, s):
if len(s) == self.types:
return s
for i in range(self.traits):
return self.b(s + str(i))
i = c()
lst = i.a()
Thanks in advance,

-d

推荐答案

" drs" < dr*@remove-to-send-mail-ecpsoftware.com>在消息中写道

新闻:Gp ******************* @ twister.nyroc.rr.com ...

我没有说明我做得非常清楚(或正确),所以我会再次尝试

。我正在寻找一个长度为x的所有唯一字符串的列表,其中

元素来自集合a,b和c。


所以,例如,在这种情况下,它将是


aaa,aab,aac,aba,abb,abc,aca,acb,acc,... cca,ccb,ccc


但是我需要长度和元素的数量可变。


我明白这对于大数字来说会很快失控,但是我

只需要很小的长度/数字。


谢谢,


-d
"drs" <dr*@remove-to-send-mail-ecpsoftware.com> wrote in message
news:Gp*******************@twister.nyroc.rr.com...
I didn''t state what I am doing quite so clearly (or correctly), so I''ll try
again. I am looking for a list of all unique strings of length x whose
elements are from the set a, b, and c.

So, for example, in this case it would be

aaa, aab, aac, aba, abb, abc, aca, acb, acc, ... cca, ccb, ccc

but I need for the length and the number of elements to be variable.

I understand this will get out of hand very quickly for large numbers, but I
only need it for small length/numbers.

Thanks,

-d


drs< drs< at> remove-to-send-mail-ecpsoftware.com>写道:
drs <drs <at> remove-to-send-mail-ecpsoftware.com> writes:

您好,我正在尝试使用元素a,b和c查找长度为x的所有列表。

Hi, I am trying to find all lists of length x with elements a, b, and c.




我不确定你到底在找什么。如果您给出了一些输入和输出的示例,那将会有所帮助。


似乎您可能正在寻找列表的排列。如果是这样,请查看

ASPN Cookbook:

http://aspn.activestate.com/ASPN/Coo.../Recipe/190465


如果不是你在寻找什么,请给我们更多关于

问题的信息。


史蒂夫



I''m not sure exactly what you''re looking for here. It would help if you gave an
example of some input and the output you want to produce.

Seems you might be looking for the permutations of a list. If so, check the
ASPN Cookbook:

http://aspn.activestate.com/ASPN/Coo.../Recipe/190465

If this is not what you''re looking for, please give us a little more info on the
problem.

Steve


drs< drs< at> remove-to-send-mail-ecpsoftware.com>写道:
drs <drs <at> remove-to-send-mail-ecpsoftware.com> writes:
我正在寻找一个长度为x的所有唯一字符串的列表,其
元素来自集合a,b和c。

但我需要长度和元素数量可变。
I am looking for a list of all unique strings of length x whose
elements are from the set a, b, and c.

So, for example, in this case it would be

aaa, aab, aac, aba, abb, abc, aca, acb, acc, ... cca, ccb, ccc

but I need for the length and the number of elements to be variable.



更清楚,谢谢。这是你在找什么:


Much clearer, thanks. Is this what you''re looking for:

def f(chars,n):
....对于字符中的字符:

....如果n == 1:

....产生字符

。 ......否则:

....对于f中的字符串(字符,n-1):

....产生字符串+字符串

.... list(f(''abc'',1))
['''',''b'',''c'']列表(f('''abc '',2))
['aa'',''ab'','ac'',''ba'',''bb'',''bc'',''ca '',''cb'',''cc'']列表(f('''abc'',3))
def f(chars, n): .... for char in chars:
.... if n == 1:
.... yield char
.... else:
.... for string in f(chars, n-1):
.... yield char + string
.... list(f(''abc'', 1)) [''a'', ''b'', ''c''] list(f(''abc'', 2)) [''aa'', ''ab'', ''ac'', ''ba'', ''bb'', ''bc'', ''ca'', ''cb'', ''cc''] list(f(''abc'', 3))



[' 'aaa'',''aab'',''aac'',''aba'',''abb'',''abc'','aca'',''acb'',''acc '','''baa'',''bab'',

''bac'',''bba'',''bbb'',''bbc'',''bca '','' bcb'',''bcc'',''caa'',''cab'',''cac'',''cba'',

''cbb'','' cbc'',''cca'',''ccb'',''ccc'']


Steve


[''aaa'', ''aab'', ''aac'', ''aba'', ''abb'', ''abc'', ''aca'', ''acb'', ''acc'', ''baa'', ''bab'',
''bac'', ''bba'', ''bbb'', ''bbc'', ''bca'', ''bcb'', ''bcc'', ''caa'', ''cab'', ''cac'', ''cba'',
''cbb'', ''cbc'', ''cca'', ''ccb'', ''ccc'']

Steve

这篇关于简单的递归帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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