[Tutor] another better way to do this ?

Alan Gauld alan.gauld at btinternet.com
Mon Jan 13 20:27:41 CET 2014


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?

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? :-)
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list