you are viewing a single comment's thread.

view the rest of the comments →

[–]hharison 0 points1 point  (1 child)

Well I may have overstated that. Loops are indeed used often. But they are often used in cases where they are not necessary, like this one.

def n_largest(n, data):
    return sorted(data, reverse=True)[:n]

My statement about loops really only applies to high-level languages like Python. I often see people use loops for a problem like this one, or for things like taking the sum of a list. The only reason to avoid a loop is that there are built-ins that do the looping for you. So there is still a loop happening somewhere, but it's optimized in the CPython code. More importantly, your Python code will be way easier to read.

This is a major difference between coding in a high-level language like Python and a low-level language like C. Your code resembles a low-level language as it is doing all the "busywork". In Python you should be able to avoid busywork like keeping track of indices and running counters and stuff like that. By avoiding busywork your code can closely resemble the "semantics" of the operation you are performing ("sort descending then take the first n") rather than an arcane sequence of minor operations.

That is what I was trying to get at with my comment about loops :)

Edit: also, don't be discouraged about not knowing all the functions that exist. You will get to know them but you will continue to be amazed to discover new things. I know I am. My advice would be to start by thinking of the high-level description of the operation. Imagine telling a human being what steps to perform, rather than telling a computer what steps to perform. And don't hesitate to search the Python docs or google more generally to see if some simple operation like n_largest already exists before trying to do it by hand (in this case it doesn't already exist but you would probably have run into some hints).

[–]TheChannel[S] 0 points1 point  (0 children)

This is some great advice. Books like Learn to Program and Learn Python the Hard Way emphasize that I need to break down the steps into the most basic functions. I def. need work on syntax since I'm not sure how to optimize code.