[Tutor] another better way to do this ?

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Jan 12 20:30:31 CET 2014


On 12/01/2014 19:22, Keith Winston wrote:
> 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 ;)
>

A better idea would be to find out why the above dosn't work correctly, 
I'll leave that in your capable hands :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list