Find N largest lines from a file: how would you make this better?
Question: I was recently rejected from a potential employer after submitting this code.
They suggested I wasn't technically capable enough. I'm wondering if someone
could shed light on to how to make this better/more efficient.

The question was to find the N longest lines from a file of multiple lines.
This ultimately boiled down to a sorting problem, so I built an algorithm to
find the N largest numbers from a list of numbers as so:

    
    
    def selection(numbers, n):
    
        maximum = []
    
        for x in range (0, n):
    
            maximum.append(numbers[x])
            ind = x
    
            for y in range ( x, len(numbers) ):
                if numbers[y] > maximum[len(maximum)-1]:
                    maximum[len(maximum)-1] = numbers[y]
                    numbers[ind], numbers[y] = numbers[y], numbers[ind]
    
        return maximum
    

This runs in _O(n)_ , unless N = n, in which case it runs in _O(n^2)_. I was
surprised to hear them doubt my technical abilities, so I thought I'd bring it
to you SO. How do I make this better?

**EDIT: Thanks for the feedback. To clarify: I populated a list with the line-
by-line word-counts from the file, and ran it through this function.**

**EDIT2: Some people mentioned syntax. I've only been doing Python for about a
day or two. My employer suggested I write it in Python (and I mentioned that I
didn't know Python), so I assumed small syntax errors and methods wouldn't be
such an issue.**

**EDIT3: Turns out my initial reasoning was flawed with the selection sort. I
had it in my head that a min-heap would be nlogn, but I forgot that the
average complexity for my code is n^2. Thanks for the help everyone.**


Answer: 
    from heapq import nlargest
    
    def longest_lines(n, filename):
        with open(filename) as input:
            return nlargest(n, input, key=len)
    

Alright, addressing the comments below:

    
    
    def longest_lines(n, filename):
        heap = []
        with open(filename) as input:
            for ln in filename:
                push(heap, ln)
                if len(heap) > n:
                    pop(heap)
        return heap
    

where `push` and `pop` are the good old min-heap insert and delete-min
algorithms that can be found in any textbook (and that I never get right in
one go, so I'm not posting them now), comparing lines by their length. This
runs in O(_N_ ×lg(_n_)) time where _N_ is the number of lines in the file,
consuming O(_n_) temporary space.

Note that the resulting list is not sorted by length, but adding that can be
done by popping the elements until the heap is empty and reversing the result
of that.

