Here’s a puzzler posed to me by Chris Laffra, one of the Bank of America Python gurus: Given a finite sequence of objects which contain some repetition find the most common object.
Here’s an example followed by my own naive solution:
input = 'aaabaccabdaabacccabdagatacaccacba' counts = [(input.count(x), x) for x in set(input)] sortedCounts = sorted(counts, key=lambda x:-x[0]) print sortedCounts[0][1]
This is pretty basic stuff, and the only thing I can say in defence of this first solution is that I bashed it out quickly. But after a few minutes of pondering I managed to find the optimal solution:
print max(input, key=input.count)
That’s only 18 bytes, not including whitespace and the print statement. This is a lovely example of what makes Python truly expressive – the ability to say a great deal in very few keystrokes.
