Getting a list of strings with a deleted character in permutation

I want to remove a character from a string in a permutation ....

Say I have a function

def (string,char):
    # remove char from string

Let's say that I have it aAabbAAas a string, and A as char, then I want the strings [aabb,aAabb,aabbA,aabbA, aabbAA,aAabbA ,aAabbA ]as output, which A is deleted 3 times, 2 times, 1 time.

What is the best way I can do this?

Many thanks....

+3
source share
3 answers

Here is one crazy idea using recursion:

def f(s, c, start):
    i = s.find(c, start)
    if i < 0:
        return [s]
    else:
        return f(s, c, i+1) + f(s[:i]+s[i+1:], c, i)

s = 'aAabbAA'
print f(s, 'A', 0)
# ['aAabbAA', 'aAabbA', 'aAabbA', 'aAabb', 'aabbAA', 'aabbA', 'aabbA', 'aabb']

Edit: using set:

def f(s, c, start):
    i = s.find(c, start)
    if i < 0:
        return set([s])
    else:
        return set.union(f(s, c, i+1), f(s[:i]+s[i+1:], c, i))

s = 'aAabbAA'
print f(s, 'A', 0)
# set(['aAabbA', 'aabbAA', 'aAabbAA', 'aabb', 'aAabb', 'aabbA'])

Edit 2: Using a ternary operator:

def f(s, c, start):
    i = s.find(c, start)
    return [s] if i < 0 else f(s, c, i+1) + f(s[:i]+s[i+1:], c, i)

s = 'aAabbAA'
print f(s, 'A', 0)
# ['aAabbAA', 'aAabbA', 'aAabbA', 'aAabb', 'aabbAA', 'aabbA', 'aabbA', 'aabb']

Edit 3 timeit::

In [32]: timeit.timeit('x = f("aAabbAA", "A", 0)', 
                       'from test3 import f', number=10000) 
Out[32]: 0.11674594879150391

In [33]: timeit.timeit('x = deperm("aAabbAA", "A")', 
                       'from test4 import deperm', number=10000) 
Out[33]: 0.35839986801147461

In [34]: timeit.timeit('x = f("aAabbAA"*6, "A", 0)', 
                       'from test3 import f', number=1) 
Out[34]: 0.45998811721801758

In [35]: timeit.timeit('x = deperm("aAabbAA"*6, "A")', 
                       'from test4 import deperm', number=1) 
Out[35]: 7.8437530994415283
+3
source

, . .

from itertools import product

def deperm(st, c):
    rsts = []
    indexes = [i for i, s in enumerate(st) if s == c]
    for i in product([c, ''], repeat=len(indexes)):
        newst = ''
        for j, ch in enumerate(st):
            if j in indexes:
                newst += i[indexes.index(j)]
            else:
                newst += ch
        rsts.append(newst)
    return rsts

for i in deperm('aAabbAA', 'A'):
    print i

:

aAabbAA
aAabbA
aAabbA
aAabb
aabbAA
aabbA
aabbA
aabb
+1

Such a recursive algorithm can help you here. Sorry, I'm not a python champion, so you may have to tweak the syntax yourself. Psuedo Code:

// returns a set of strings (permutations)
def permutation(string, char)
  if len(string) == 0
    return [] // return empty set

  // get the set of permutations of suffix string recursively
 set_of_perm_suffix = permutation(string[1:], char)

 // prepend char to every string in set_of_perm
 appended_set = prepend_char(set_of_perm_suffix , string[0])

 // if the first char matches the one we should remove, we could either
 // remove it or keep it.
 if (string[0] == char)
   return union_of_sets(set_of_perm_suffix , appended_set)
 else
   // the first char doesn't match the one we should remove,
   // we need to keep it in every string of the set
   return appended_set
0
source

Source: https://habr.com/ru/post/1781507/


All Articles