2

I have a string which contains a fixed character. Can I generate a permutation of strings by replacing it with another character or string?? Say, I have something like this:

designatedstring="u"
replacerstring="ough"
s="thruput"

I want an output like:

l=["throughput","thrupought","throughpought"]

Is there any way to do this?

1
  • what happen if designated = u and the replace is uu, do you want the output the output of AuuA to have one or two AuuuA? Commented Nov 29, 2011 at 9:23

3 Answers 3

4

More itertools sugar:

>>> parts = s.split(designatedstring)
>>> num = len(parts) - 1
>>> replacements = itertools.product([designatedstring, replacerstring], repeat=num)
>>> replacements = list(replacements)
>>> replacements.remove((designatedstring,) * num)
>>> for r in replacements:
...     print ''.join(itertools.chain(*zip(parts, r + ('',))))
...
thrupought
throughput
throughpought

If you can bear with original string in the result, you can omit ugly transformations on 4 and 5 lines.

Sign up to request clarification or add additional context in comments.

1 Comment

You have been faster than me for a few seconds! :o +1
0

Here's an interesting way to do it, though I'd still go with the itertools solution.

Notice that the list of strings you produce is morally a binary tree: at each instance of the replacement character in the string you either want to ignore it ("go left") or replace it ("go right"). So you can make a binary tree structure out of the replacement strings. Walking the leaves of a binary tree is easy.

class ReplacementTree(object):
    def __init__(self, s, src, target, prefix=""):
        self.leaf = src not in s
        if 1 == len(s.split(src, 1)):
            self.head, self.tail = s, ""
        else:
            self.head, self.tail = s.split(src, 1)
        self.prefix, self.src, self.target = prefix, src, target

    @property
    def value(self):
        if self.leaf:
            return self.prefix + self.head

    @property
    def left(self):
        if self.leaf:
            return None
        else:
            return ReplacementTree(self.tail, 
                                   self.src,
                                   self.target,
                                   prefix=self.prefix + self.head + self.src)

    @property
    def right(self):
        if self.leaf:
            return None
        else:
            return ReplacementTree(self.tail, 
                                   self.src,
                                   self.target,
                                   prefix=self.prefix + self.head + self.target)

def leaves(tree):
    if tree.leaf:
        yield tree.value
    else:
        for leaf in leaves(tree.left):
            yield leaf
        for leaf in leaves(tree.right):
            yield leaf

Example:

>>> x = repltree.ReplacementTree("thruput", "u", "ough")
>>> list(repltree.leaves(x))
['thruput', 'thrupought', 'throughput', 'throughpought']

Comments

-1

From the input given in your question, this will produce the list requested:

s="thruput"
designatedstring="u"
replacerstring="ough"

l = []
for i in range(len(s)):
    if s[i] == designatedstring:
        l += [s[:i] + replacerstring + s[i + 1:]]

l += [s.replace(designatedstring, replacerstring)]

1 Comment

@DrTyrsa It'll work, but it wont create the strings 'toughtoughtu', 'toughtutough' and 'tutoughtough'. Which technically wasn't specified in the question. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.