将“a”替换为数字7s或8s递归(python)

2024-09-27 00:20:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,正在尝试学习递归。 我试图通过将“a”改为数字7或8来显示所有可能的结果

例如

user_type =  40aa

因此它将显示:

4077

4078

4087

4088

谢谢

它不一定是40aa,它可以是a4a0,aaa0,等等

这个代码只是替换7,我怎么能修复它

user_type = 40aa
def replace(string, a, b)
   if not string:
       return ""
   elif string[:len(b)] == b: 
       return a + replace(string[len(b):], a, b)
   else: 
       return string[0] + replace(string[1:], a, b)
print(replace(user_type, '7', 'a'))

Tags: 代码stringlenreturnifdeftypenot
3条回答

我对Python不是很了解,但是我可以帮助处理递归。 其基本思想是循环遍历字符串中的每个字符,每次碰到“a”时,都将其替换为7和8,并将这两个值传递给递归方法。你知道吗

举个例子: 假设你有字符串“Basttaa”。你知道吗

循环,直到你击中一个a,所以你在第二个字符。将其替换为“7”和“8”。现在您有了两个独立的字符串,可以将它们传递给递归方法。你知道吗

我们现在有“B7sttaa”和“B8sttaa”。我们将两者都传递给我们的职能部门。 在第一个字符串中,我们找到第6个字符,用“7”和“8”替换它,然后重复这个过程。更换后,我们有“B7stt7a”、“B7stt8a”和“B8sttaa”。你知道吗

现在,对于传递的第二个字符串,我们再次到达第6个字符并执行替换过程。现在我们有四个字符串:“B7stt7a”、“B7stt8a”、“B8stt7a”和“B8stt8a”。你知道吗

这四个字符串再次传递给递归方法,在每个字符串上的最后一个字符被“7”和“8”替换之后,我们得到最后8个字符串。你知道吗

我们的四个字符串:“B7stt7a”、“B7stt8a”、“B8stt7a”和“B8stt8a”再次传递给我们的递归方法。该方法获取每个的最后一个字符,并用“7”和“8”替换每个字符的a。然后,因为它位于每个字符串的末尾,所以它将每个字符串添加到列表中。你知道吗

“B7stt7a”变为“B7stt77”和“B7stt78”,两者都添加到列表中。你知道吗

“B7stt8a”变为“B7stt87”和“B7stt88”,两者都添加到列表中。你知道吗

“B8stt7a”变为“B8stt77”和“B8stt78”,两者都添加到列表中。你知道吗

“B8stt8a”变为“B8stt87”和“B8stt88”,两者都添加到列表中。你知道吗

现在列表有[“B7stt77”,“B7stt78”,“B7stt87”,“B7stt88”,“B8stt77”,“B8stt78”,“B8stt87”,“B8stt88”]

psuedo代码如下所示:

list[];
recusion(string str)
    for each char
        if char is 'a'
            return recursion(str replace char with 7)
            return recursion(str replace char with 8)
        if at end
            add str to list
    return;
pattern = "40aa"
options = [7, 8]

def replace(left, right):
   if len(right) > 0:
      if right[0] == "a":
         results = []
         for i in options:
             results.extend(replace(left + str(i), right[1:]))
         return results
      else:
         return replace(left + right[0], right[1:])
   else:
      return [left]

print replace("", pattern)

换言之,调用函数时使用模式中已处理的部分和剩余部分。如果下一个模式的字符是数字,它将从模式传递到结果。如果它是“a”,它将被所有选项一步一步地替换,剩下的模式将被递归处理。你知道吗

所以,我们可以用一种非常简单的方法来做。我们需要创建两个列表:

user_type.split('a') == ['40', '', '']
itertools.product('78', repeat=user_type.count('a')) == [('7', '7'), ('7', '8'), ('8', '7'), ('8', '8')]

现在,对于我们的每一对,我们需要交错它们。itertools文档提供了一个名为roundrobin()的好方法:

import itertools

# Round Robin recipe from the itertools documentation
def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF')  > A D E B F C"
    # Recipe credited to George Sakkis
    pending = len(iterables)
    nexts = itertools.cycle(iter(it).next for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = itertools.cycle(itertools.islice(nexts, pending))

让我们把这些放在一起:

user_type = "40aa"
user_count = user_type.count('a')

for replacement in itertools.product('78', repeat=user_count):
    print ''.join(roundrobin(user_type.split('a'), replacement))

输出:

4077
4078
4087
4088

相关问题 更多 >

    热门问题