Python recursion permutations

前端 未结 9 1041
自闭症患者
自闭症患者 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:44
    def permute(s):
        ch = list(s)
        if len(ch) == 2:
            per = ch[1] + ch[0] 
            return [''.join(ch)] + [per]
        if len(ch) < 2:
            return ch
        else:
            return [ init+per for init in ch for per in permute(''.join(ch).replace(init,""))] 
    
    0 讨论(0)
  • 2020-11-27 19:53

    Recursively, think about the base case and build from that intuition.

    1) What happens when there's only one character 'c'? There's only one permutation of that element, and so we return a list containing only that element.

    2) How can we generate the next permutation given the last one? Adding an additional letter 'a' at all possible positions in the previous permutation 'c' gives us 'ca', 'ac'.

    3) We can continue building larger and larger permutations by adding an additional character at all possible positions in each earlier permutation.

    The following code returns a list of one character if the string has one character or less. Otherwise, for all permutations not including the last character in the string s[-1], we generate a new string for each position where we could include that character and append the new string to our current list of permutations.

    def permutations(s):
        if len(s) <= 1:
            return [s]
        else:
            perms = []
            for e in permutations(s[:-1]):
                for i in xrange(len(e)+1):
                    perms.append(e[:i] + s[-1] + e[i:])
            return perms
    
    0 讨论(0)
  • 2020-11-27 19:53

    You can use a function that iterates an index through the list, and yield a list consisting of the value at the index followed by the permutations of the rest of the list values. Below is an example using features from Python 3.5+:

    def permutations(s):
        if not s:
            yield []
        yield from ([s[i], *p] for i in range(len(s)) for p in permutations(s[:i] + s[i + 1:]))
    

    so that list(permutations('abc')) returns:

    [['a', 'b', 'c'],
     ['a', 'c', 'b'],
     ['b', 'a', 'c'],
     ['b', 'c', 'a'],
     ['c', 'a', 'b'],
     ['c', 'b', 'a']]
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-27 19:54

    You want to do recursion, so you first have to find out how the recursion would work. In this case it is the following:

    permutation [a,b,c,...] = [a + permutation[b,c,...], b + permutation[a,c,..], ...]
    

    And as a final condition:

    permutation [a] = [a]
    

    So the recursion splits up the list in sublists with one element extracted each time. Then this element is added to the front of each of the permutations of the sublist.

    So in pseudo-code:

    def permutation(s):
       if len(s) == 1:
         return [s]
    
       perm_list = [] # resulting list
       for a in s:
         remaining_elements = [x for x in s if x != a]
         z = permutation(remaining_elements) # permutations of sublist
    
         for t in z:
           perm_list.append([a] + t)
    
       return perm_list
    

    Does this help?

    0 讨论(0)
  • 2020-11-27 19:55

    When you're lost in recursive function, you should draw the call tree. The following version (inspired @Ben answer) keep the input order (if the input is in lexicographic order, the list of permutations will be, '012' -> ['012', '021', '102', '120', '201', '210'].

    def permut2(mystr):
        if len(mystr) <= 1:
            return [mystr]
        res = []
        for elt in mystr:
            permutations = permut2(mystr.replace(elt, ""))
            for permutation in permutations:
                res.append(elt + permutation)
        return res
    

    The following version works for strings and lists, notice the reconstruction step is not the same:

    def permut(array):
        if len(array) == 1:
            return [array]
        res = []
        for permutation in permut(array[1:]):
            for i in range(len(array)):
                res.append(permutation[:i] + array[0:1] + permutation[i:])
        return res
    

    As an exercice, you should draw call tree of the these functions, do you notice something ?

    0 讨论(0)
提交回复
热议问题