algorithm - Solving jumbled word puzzles with python? -
I have an interesting programming puzzle for you:
Two things will be given to you:
P>
subset = ['I', 'A', 'M', 'AMT', 'M', 'T', 'TI' 'Tier', 'Tired', 'I' 'R', 'R', 'Re', 'Lal', 'Redre', 'E', 'D', 'Da', 'Dar', 'Dear', 'A', 'R', 'are '' '' '', '' '' '' '' '' ' Level-1: Let me practice the members in practically Find a subset of
in an order with "iamtiredareyou"
or ['i', 'am', 'tired', 'are', 'you' ] Level 2: The original string may contain some additional characters in the sequence that are not present in the subset. Like "iamtired12aareyou"
. The given subset
is similar to the above, the solution automatically will automatically include the result in the nickname in the right place. I.e. ['i', 'am', 'tired', '12a', 'are', 'you']
How can I do this?
Generally, there would be a recursive algorithm. Start with checking all the subsets against the beginning of the given word - is found - to add found (add) for the values found and recurse with the remaining word and current found value. Or if it is the end of the string - print found value. Something like this:
all = [] def frec (word, value = []): gobal if all words == "": # result found all + = [Value] for subset: if word.startswith (s): frec (word [lane]:], value + [s]) frec (word)
Note that There are lots of potential solutions because the subset includes several letter strings you should get some least results. (13146 solution ...) "All sources (cmp = lambda x, y: cmp (len (x), lan (y))" to get the least ...
one For Level 2 - you need another loop if a subset matches that which adds more and more symbols for the next value (and it happens again) until the match is not found. Def frec (word, values = []): Global if all words == "": # result found all = = [value] back to real match = most Incorrect for surnames in surnames: if the word Starts (s): match = true frec (word [lane]:], value + [s]) if match does not: return to fricity (word [1:], value + [word [0]]) freesi )
This does not attempt to combine a non-subset value into a string, though.
Comments
Post a Comment