Pages

CodeEval Longest Lines

You get as input the number of lines you want in output and a bunch of lines having different lengths. You should return the longest ones. This is the CodeEval problem #2.

I am about to write a Python 3 solution, but first thing I write a test case for my function, based on the provided sample:
def test_provided_1(self):
    self.assertEqual('San Francisco\nHello World', solution(2, ['Hello World', 'CodeEval', 'Quick Fox', 'A', 'San Francisco']))

This problem uses a different structure from the CodeEval standard, I change accordingly the main script to extract the output size from the first line and to put all the other lines, stripped of their terminating newline character, in a list of strings.
data = open(sys.argv[1], 'r')
top = int(data.readline())
lines = [line.rstrip('\n') for line in data]
Having prepared the input data in this way, my solution is pretty compact:
def solution(size, lines):
    lines.sort(key=len, reverse=True) # 1
    del lines[size:] # 2
    return '\n'.join(lines) # 3
1. I use the specific list sort() method instead of the built-in sorted() function because I'm happy to modify the existing list. Using sorted() would have created a new list. In a real world application, usually sorted() is the preferred approach, because we don't want to mess with the data owned by the caller. I sort the strings by their length, this is done by passing in the key parameter the function len, that has to be used to compare the strings. And I want to get the longest on top, so I want to get the reversed natural (shorter first) order.
2. I want to output just the top 'size' lines. The easiest way to do that is removing all the other elements from the collection. Here I do it using the handy del operator.
3. Finally, I join the surviving elements in the list on the newline, since I have been asked to present each element on a different line.

After the solution was accepted with full marks, I pushed test case and actual python script to GitHub.

No comments:

Post a Comment