Skip to content Skip to sidebar Skip to footer

String Variations

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' an

Solution 1:

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:

defapply_rules(rules, input, start=0):
    # First yield the outcome of no applied rules.yieldinput[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 onecontinue# 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.

Post a Comment for "String Variations"