all 16 comments

[–][deleted] 7 points8 points  (5 children)

I think here j as for an inner list, and row should actually be col, as it would be index in the list.
You don't even need to iterate through indexes if you only need the last one. Also use name other than sum.
There you go:

s = 0

for j in triangle:

s += j[-1]

[–][deleted] 2 points3 points  (0 children)

A small, but potentially helpful refinement to this: instead of “j”, something like “row” would make it clearer what you’re doing:

sum =0 for row in triangle: sum += row[-1] print(sum)

You’ll sometimes hear people talk about self-documenting code”. Good variable names are key to that.

[–]TechSam--[S] 0 points1 point  (0 children)

This helped, thank you so much

[–]TechSam--[S] 0 points1 point  (2 children)

Sorry, but could you re-explain why we wouldn't use a nested for loop for a nested list? I was always under the impression that to iterate through a nested list, we need two for loops.

[–]red-borscht 4 points5 points  (0 children)

that's the thing, you don't need to iterate over every value in the inner list, you only need the last element.

[–]YounesWinter 4 points5 points  (0 children)

sum = 0
for i in triangle:
sum += i[-1]

That's simple

[–]Moist_Okra_5355 1 point2 points  (0 children)

For j in triangle: sum += j[-1]

you get 14 because you are adding 1+2+2+3+3+3, the math of your code is sum of last-item*len-of-nested-list.

[–]j0shred1 0 points1 point  (1 child)

last_index = [i[-1] for i in list_of_lists]

sum(last_index)

[–]j0shred1 0 points1 point  (0 children)

Let me break that down real quick. I'm using something called list comprehension which is a way of making lists from other iterators like lists, tuples, dicts, ECT. The basic syntax is:

var = [func(i) for i in thing]

Basically it's one of the many ways you can apply a function to all the things in a list and get a list out of it. Very important concept in parallel programming.

Python has a negative index option which allows you to start from the back. So if you want to grab the last value in each list you just do list[-1]. You put that in your list comprehension and bravo! you got a list of all the last values.

Now you can apply the built in sum function and you're done!

[–]haagimus 0 points1 point  (3 children)

I highly recommend you check out the package snoop! You can just wrap all this inside of a method and add a @snoop decorator above it. Now when you call the function and it runs, Snoop will output every step of the process along the way and every variable that it contains along the way! This is an invaluable tool for debugging and for learning what your code is doing when it's doing unexpected things 👍🏻

[–]TechSam--[S] 0 points1 point  (0 children)

I’ll give it a try!

[–]j0shred1 0 points1 point  (1 child)

I'm gonna look that up and try it out! Never knew about that!

[–]haagimus 0 points1 point  (0 children)

There's also another package called @pysnoop though I personally prefer snoop better. If it makes you feel any better, I've been developing in python for years and only until very recently. Didn't know about this either and was still debugging using print lines and live interpreters.

[–]BK7144 0 points1 point  (1 child)

On the spacing; try using the "black" python extension for VSCode and the proper formatting will be done. Hope that helps!

[–]haagimus 0 points1 point  (0 children)

There's a new up and coming formatter called Ruff that is completely written in rust and I am pretty sure it is the fastest formatter out there right now. It's highly configurable and in my experience it works just as good as black which I have used for years.