collections.Counter will not allow any account to go below zero if you use the - operator. However, if you use c.subtract (d), then this will allow you to do this. In addition, when using c.elements (), values ββwith a negative number of samples are ignored.
Here is a collection based implementation. Counter:
import collections def remove_common(x, y): sort = lambda x: "".join(c for c in sorted(x.lower()) if c.isalpha()) x, y = list(sort(x)), list(sort(y)) cx = collections.Counter(x) cy = collections.Counter(y) cx.subtract(cy) result = "" for letter, count in cx.iteritems(): for i in range(abs(count)): result += letter return result
I ran it on the following test suites:
print remove_common("Lets chat about code","that cave") print remove_common("basdf aa", "a basd") print remove_common("asdfq", "asdf") print remove_common("asdf", "asdfq") print remove_common("aa bb s", "a bbb")
Results:
cbedloosutv af q q asb
To detect letters that are in y but not in x, you must compare the result of cy.subtract(cx) with the value of cy . For instance:
cz = collections.Counter(cy)
Other solutions to this bit that I have seen also fail if the letter exists in y but is repeated more than in x (for example: "hello there" and "hii" will raise an AssertionError in the Josh Smeaton solution but not this one). Your requirement is a bit ambiguous in this regard IMO. The beauty of stackoverflow is that there are enough answers to choose your poison.
Hope this helps.
source share