Hacker News new | past | comments | ask | show | jobs | submit login

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.




Be careful using sets -- what about "banana" and "ban"


The 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.


Yes and I appreciate it.

https://gist.github.com/rilindo/6576017

New toy to play with. :)


Python'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])`.


Using 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#collection...


At that point, might as well just do `sorted(list(x)) == sorted(list(y))`.


Counters 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: