String Variations

Question:

I have a string in python and a dictionary of ‘rules’, or possible alterations to the string. For example, one rule might have a key of 'he' and a value of 'e', or a key of 'll' and a value of 'l'.

These rules would mean that any occurrence of “’he’ in my string can be replaced with an 'e', and similarly for 'll' and 'l'.

What I want is to find all variations of my string, given this dictionary of rules. For instance, with the two rules from above and the string 'hello', I would like to return:

['hello', 'ello', 'helo', 'elo']

Any help is appreciated, thanks!

Asked By: Clayton

||

Answers:

Write a recursive function that takes a substring of the input. This function then examines all rules. For each rule that matches, one replacement is done, and the remainder of the string is processed by a recursive call:

def apply_rules(rules, input, start=0):
    # First yield the outcome of no applied rules.
    yield input[start:]

    for match, replace in rules:
        # Find the first match for this rule.
        index = input.find(match, start)
        if index < 0:
            # No match -- skip to next one
            continue
        # Prepare the result of the replacement.
        prefix = input[start:index] + replace
        # Apply further rules to the rest of the string
        # by a recursive call.
        for suffix in apply_rules(rules, input, index + len(match)):
            yield prefix + suffix

Use it like this:

>>> rules = [('he','e'), ('ll','l'), ('e','ee')]
>>> list(apply_rules(rules, 'hello'))
['hello', 'ello', 'elo', 'helo', 'heello', 'heelo']

Please note that I don’t allow rules to be applied on replaced strings, to prevent cases of infinite results as demonstrated in the comments to this question.

Answered By: Ferdinand Beyer
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.