编写一个程序来显示" abcdef"的所有字谜。在Python中

时间:2015-02-26 03:06:16

标签: python anagram

我的朋友写了一个代码,在python中显示“abcdef”的所有字谜。但是在这段代码中,我无法理解递归过程是如何工作的anagrams = get_list_of_anagrams(''.join(tmp_list))函数如何调用自身?

def get_list_of_anagrams(s):
    if len(s)==0:
        return ['']
    all_chars = list(s)
    unique_chars = list(set(s))
    anagrams_list = []
    for char in unique_chars:
        tmp_list = list(all_chars)
        tmp_list.remove(char)
        anagrams = get_list_of_anagrams(''.join(tmp_list))
        for i in range(len(anagrams)):
            anagrams[i] = char+anagrams[i]
        anagrams_list += anagrams
    return anagrams_list

当我尝试将所有内容打印到anagrams = get_list_of_anagrams(''.join(tmp_list)

之前
def get_list_of_anagrams(s):
    if len(s)==0:
        return ['']
    all_chars = list(s)
    unique_chars = list(set(s))
    anagrams_list = []
    for char in unique_chars:
        tmp_list = list(all_chars)
        tmp_list.remove(char)
        anagrams = get_list_of_anagrams(''.join(tmp_list))



print get_list_of_anagrams('abc')

我得到以下输出。

enter image description here

以下代码:

def get_list_of_anagrams(s):
    if len(s)==0:
        return ['']
    all_chars = list(s)
    unique_chars = list(set(s))
    anagrams_list = []
    for char in unique_chars:
        tmp_list = list(all_chars)
        tmp_list.remove(char)
        anagrams = get_list_of_anagrams(''.join(tmp_list))
        print anagrams
        for i in range(len(anagrams)):
            anagrams[i] = char+anagrams[i]
        anagrams_list += anagrams
    return anagrams_list  

print get_list_of_anagrams('abc')

我得到以下输出:

enter image description here

有人可以解释一下为什么上面的输出是这种模式的吗?

4 个答案:

答案 0 :(得分:2)

生命是短暂的使用库:

import itertools
d = 'abc'
e = len(d)
j = list()

for p in itertools.permutations(d, e):
    print p

('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')

答案 1 :(得分:1)

追踪:

def get_list_of_anagrams(s):
    # base case - empty string, return empty
    if len(s)==0:
        return ['']

    # figure out which characters are available
    all_chars    = list(s)       # may have repeats
    unique_chars = list(set(s))  # no repeats

    # prepare to store all results
    anagrams_list = []

    # for each unique character
    for char in unique_chars:
        # remaining unused characters
        tmp_list = list(all_chars)
        tmp_list.remove(char)

        # recurse: get all anagrams of remaining characters
        anagrams = get_list_of_anagrams(''.join(tmp_list))

        # prefix each result with picked character
        for i in range(len(anagrams)):
            anagrams[i] = char+anagrams[i]

        # add to overall results
        anagrams_list += anagrams

    # return all results
    return anagrams_list

我会重写一下,比如

def get_list_of_anagrams(s):
    anagrams = []

    remaining = list(s)
    for char in sorted(set(s)):
        remaining.remove(char)
        if remaining:
            # recurse: get all anagrams of remaining characters
            for anagram in get_list_of_anagrams(remaining):
                # prefix each result with picked character
                anagrams.append(char + anagram)
        else:
            anagrams.append(char)
        remaining.append(char)

    # return all results
    return anagrams

所以要找到get_list_of_anagrams("abcdef"),你得到

"a" + each(get_list_of_anagrams("bcdef"))
"b" + each(get_list_of_anagrams("acdef"))
"c" + each(get_list_of_anagrams("abdef"))
"d" + each(get_list_of_anagrams("abcef"))
"e" + each(get_list_of_anagrams("abcdf"))
"f" + each(get_list_of_anagrams("abcde"))

答案 2 :(得分:0)

这是一个递归函数。 例如着名的斐波那契序列:

def fib(n):
    if n < 2:
        return n
    return fib(n-2) + fib(n-1)
print fib(10)

查看this question about recursive了解详情。

答案 3 :(得分:0)

想法是找到'abc'的字谜,首先找到'bc'的所有字谜,这是通过首先找到'c'的所有字谜完成的,这是首先完成的查找''的所有字谜。

那你如何找到&#34;外层&#34;的字谜?鉴于&#34;内层的所有字谜&#34;?鉴于'bc''bc''cb')的所有字谜,您可以通过将'abc'插入a的每个位置来找到'bc'的所有字谜{1}}和'cb'。详情:

# 'bc' will give these anagrams:
abc # insert 'a' at first position
bac # insert 'a' at second position
bca # insert 'a' at third position (also last)

# Similarly, 'cb' gives:
acb
cab
cba

所以'bc'的2个字谜会给你6个'abc'的字谜。

===

关于您的算法:它在每次递归时删除1个字符(使用tmp_list.remove(char))。在len(s)==0的基本情况下(即删除了所有字符时),它只返回''。然后继续使用上面的逻辑找到'f'的字谜,然后是'ef' ....的字谜,最后返回abcdef的字谜。