[Tutor] another better way to do this ?

Keith Winston keithwins at gmail.com
Sun Jan 12 20:22:09 CET 2014


On Sun, Jan 12, 2014 at 7:44 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> OK< So there is nothing here about the orders being the same.
> That makes it much easier.


There's another approach, I think, that's quite easy if order IS important.

Iterate through the letters of product, find() them initially from the
beginning of debris, and then from the index of the last letter found.
Accounts for multiples in product, & order.

def fix_machine(debris, product):
    index = 0
    success = False
    for letter in product:
        test = debris.find(letter, index)
        if test:
            index = test
        else:   # Failure
            return "Give me something that's not useless next time."
    return product   # Success

I suspect this could be done in one line, without regex, but it would
probably take me a week to figure out... maybe next week ;)

-- 
Keith


More information about the Tutor mailing list