[Tutor] another better way to do this ?
Alan Gauld
alan.gauld at btinternet.com
Sun Jan 12 13:44:20 CET 2014
On 12/01/14 08:12, Roelof Wobben wrote:
> # Write a Python procedure fix_machine to take 2 string inputs
> # and returns the 2nd input string as the output if all of its
> # characters can be found in the 1st input string and "Give me
> # something that's not useless next time." if it's impossible.
OK< So there is nothing here about the orders being the same.
That makes it much easier.
> # 5***** # If you've graduated from CS101,
> # Gold # try solving this in one line.
Its not too hard to do in one line.
I think a filtered list comprehension and the all() function
would be one way.
> print "Test case 1: ", fix_machine('UdaciousUdacitee', 'Udacity') ==
> "Give me something that's not useless next time."
> print "Test case 2: ", fix_machine('buy me dat Unicorn', 'Udacity') ==
> 'Udacity'
> print "Test case 3: ", fix_machine('AEIOU and sometimes y... c',
> 'Udacity') == 'Udacity'
> print "Test case 4: ", fix_machine('wsx0-=mttrhix', 't-shirt') == 't-shirt'
I'd not use the while loop personally, I'd go for a for loop over b
and use the in operation on a. So Something like
for letter in b:
if letter not in a:
return ....
return b
HTH
--
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