Alan Gauld wrote:
> 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
The test cases are not explicit about what to do with multiple occurences of
the same letter. I'd expect that debris must contain two 't's for 't-shirt'
to match. So:
print "Test case 5: ", fix_machine('wsx0-=mtrhix', 't-shirt') == "Give me
something that's not useless next time."
If my assumption is correct a containment test is not sufficient; you need
to count the characters:
def fix_machine(debris, product):
return (product
if all(debris.count(c) >= product.count(c) for c in
set(product))
else "Give me something that's not useless next time.")
OP: You'll get bonus points (from me, so they're pointless points, but
still) if you can solve this (including the fifth apocryphal test case)
using the collections.Counter class.
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor