Python recursion permutations

前端 未结 9 1071
自闭症患者
自闭症患者 2020-11-27 19:24

Im having trouble trying to make a permutation code with recursion. This is suppose to return a list back to the use with all the posible position for each letter. so for t

9条回答
  •  执念已碎
    2020-11-27 19:53

    def permutations(string_input, array, fixed_value=""):
        for ch in string_input:
            permutations(string_input.replace(ch, ""), array, fixed_value + ch)
        if not string_input:
            array.append(fixed_value)
    

    You can call it by

    array = []
    permutations("cat", array)
    print array
    

提交回复
热议问题