Loops assignment

Am I crazy, or is looping not needed for this assignment? I’ve been struggling a little with loops, but when I saw this I had to ask myself if doing it without loops wasn’t the easier option. Now I’m wondering if I got the correct answer by accident or if my code would actually work correctly for the requested outcome.

Any input is greatly appreciated.

Instructions

1.

Create a function named larger_sum() that takes two lists of numbers as parameters named lst1 and lst2 .

The function should return the list whose elements sum to the greater number. If the sum of the elements of each list are equal, return lst1 .

https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-loops-cc/exercises/larger-sum

1 Like

There are lots of methods to avoid loops in Python and they’re well worth knowing but it’s a good idea to try completing those lessons using loops so as to get the most out of them.
As an aside there’s neat way to deal with that flow if you wanted.

1 Like

Slightly off topic…

Rather than have two loops to compute the sums, we can add a helper function to our function (yes, we can write functions inside functions).

def larger_sum(first, second):
    def sum(u):
        s = 0
        for x in u:
            s += x
        return s
    return second if sum(second) > sum(first) else first

Ok, point definitely noted with me needing to have a solid understanding of loops. My main thing was trying to figure out if my method actually was usable, or if it was just accidentally putting out the correct answer.
I’m still new to all of this, so when I tried a code that didn’t match the assignment but still worked, I honestly thought “That can’t be right, you don’t know what you’re doing well enough to find an alternate solution.” :joy:
Self doubt can be a powerful thing.
I’ll definitely make sure to get better at loops though. Thanks for helping me realize that the code I wrote actually does give the correct output.

1 Like

So @mtf bare with me as I’m still very new to coding. Can you explain to me what that inner function is doing in simplest terms?

We have overridden the built in sum() function by defining our own which takes a list and adds up all the elements, the same way the built-in does. Our function is calling this one.

That would be because you expected it to, based on your understanding of the built in sum() function. Perhaps how it works might still be a mystery, but our above helper function shows in a brief sense that operation.

You may not have seen that syntax before now. It is similar to but not exactly the same as,

s = s + x

I won’t get into all the details here but it is related to creating a new object, in this instance, as opposed to mutating an existing object, in the example above. Quite different operations internally. Enough on that, for now.

Bottom line, work with loops until it hurts. When you reach for a built-in, be able to envision in your mind how that iterator works. The only way that happens is if you’ve entrenched yourself in looping constructs and the logic involved.

3 Likes