Alan Gauld wrote: > On 13/01/14 18:22, Peter Otten wrote: >> Peter Otten wrote: > >> In the mean time here is my candidate: >> >> def test(a, b): >> a = iter(a) >> return all(c in a for c in b) > > That's pretty close to my original thoughts. But one question. > Why explicitly convert string a to an iter? The 'in' test > would work with the original string. What extra magic does > iter confer? Or are you extending reuse beyond strings?
No, I wanted to give a solution for the problem as originally stated and as attacked by Emile, where all characters have to occur in a in the same order as in b, but with arbitrary garbage allowed in between. Compare: >>> for debris, product in [("alph", "alpha"), ("alpha", "alpha"), ("axlypzha", "alpha"), ("alpha", "alpha"[::-1])]: ... print("debris: {}, product: {} --> test(): {}, (...): {}".format( ... debris, product, test(debris, product), all(c in debris for c in product))) ... debris: alph, product: alpha --> test(): False, (...): True debris: alpha, product: alpha --> test(): True, (...): True debris: axlypzha, product: alpha --> test(): True, (...): True debris: alpha, product: ahpla --> test(): False, (...): True The all(...) expression alone gets neither the count nor the order right. The iter() call causes the `c in a` expression to search for the current c only after the occurence of the previous c. > And of course the original challenge was not for a > boolean result but a specific string result so I'd > go with: > > def test(a,b): > return {True: b, > False: 'Give me something that's not useless next time.' > }[all(c in a for c in b)] > > or even > > def test(a,b) > return b if all(c in a for c in b) else "Give me something that's > not useless next time." > > Are we there yet? :-) Where's there? When's yet? ;) _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor