【问题标题】:Permutation in scheme方案中的排列
【发布时间】:2023-03-29 09:42:01
【问题描述】:

问题:写一个函数全排列的归纳定义,该函数将一个数字列表作为输入,并且 返回该数字列表的所有排列的集合,作为输出,表示为列表的列表。

(apply append(map(lambda (i) (map (lambda (j)(cons i j))
                                                (permute (remove i 
lst))))lst)))))

我想出了问题的核心代码。但我需要用纯英文和数学符号表达解决方案,没有变量或数据结构突变。

【问题讨论】:

    标签: scheme permutation


    【解决方案1】:
    # Python program to print all permutations with
    # duplicates allowed
    
    def toString(List):
        return ''.join(List)
    
    # Function to print permutations of string
    # This function takes three parameters:
    # 1. String
    # 2. Starting index of the string
    # 3. Ending index of the string.
    def permute(a, l, r):
        if l==r:
            print toString(a)
        else:
            for i in xrange(l,r+1):
                a[l], a[i] = a[i], a[l]
                permute(a, l+1, r)
                a[l], a[i] = a[i], a[l] # backtrack
    
    # Driver program to test the above function
    string = "ABC"
    n = len(string)
    a = list(string)
    permute(a, 0, n-1)
    
    # This code is contributed by Bhavya Jain
    

    这是在 geekforgeeks 中发现的你的问题在 python 中完成的代码

    来源:Mathword(http://mathworld.wolfram.com/Permutation.html)

    【讨论】:

    • 感谢您的评论。但是,我需要 Scheme 语言的答案。
    • @NickSun 你为什么接受这个答案,那么,如果它不符合你的要求呢?
    猜你喜欢
    • 1970-01-01
    • 2013-12-17
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多