3 ms·
You might want to try something like 'return set(x)==set(y)'. Or, a better solution might be 'return set(collections.Counter(x).items())==set(collections.Counte
by MichaelSalib 13y ago
You might want to try something like 'return set(x)==set(y)'. Or, a better solution might be 'return set(collections.Counter(x).items())==set(collections.Counter(y).items())'.
The point of the question isn't to see if you've memorized every single stdlib function. If you can describe a stdlib function but can't remember it, a good interviewer will tell you about it.
- minikomi 13y agoBe careful using sets -- what about "banana" and "ban"
- MichaelSalib 13y agoThe OP's solution didn't deal with letter counts at all; my first expression was to show him a similar solution that was more succinct to illustrate the principle at work. My second solution deals with multiplicity properly.
- rilindo 13y agoYes and I appreciate it. https://gist.github.com/rilindo/6576017 https://gist.github.com/rilindo/6576017 New toy to play with. :)
- deleted 13y ago[deleted]
- matchu 13y agoPython's Counters can also be compared directly. No need to drop them in a set; `Counter([1, 1, 2, 3]) == Counter([1, 2, 3, 1])`.
- beambot 13y agoUsing Counters is actually pretty cool... it's basically a sparse histogram. Can't believe I've never used 'em before -- all I've ever used from collections is OrderedDicts. Actually... I prefer the parent's solution using set(counter.items()). This way, you know that the equality tests are ordering-agnostic. It's not clear if that's the case for counters... even the docs are unclear AFAICT: http://docs.python.org/2/library/collections.html#collections.Counter http://docs.python.org/2/library/collections.html#collection...
- meowface 13y agoAt that point, might as well just do `sorted(list(x)) == sorted(list(y))`.
- beambot 13y agoCounters are effectively building a sparse histogram by going through each element in x, and then each element in y. Ignoring the test for equality, the Big-O for histograms is O(x+y). For sorting in general, the Big-O is O(x·log(x)+y·log(y)). In other words: the sorting solution is correct but less efficient.