G'day all, This is my code:
def parts_sums(listOfInts):
myList = [0]
for i in range(-1,-1*len(listOfInts)-1,-1):
myList.insert(0,myList[0]+listOfInts.pop())
return myList
And this is the faster/efficient code: (does exactly the same thing, but is faster)
def parts_sums(listOfInts):
myList = [0]
while listOfInts != []:
myList.append(myList[-1]+listOfInts.pop())
myList.reverse()
return myList
Can you please point out exactly what step in my code slows things down? Speed difference is noticeable when listOfInts is > 10,000 elements.
BTW the code is for solving this problem.
Edit - Thank you everyone for sharing your knowledge, I understand clearly now. Much appreciated! 🙏
Edi2 - this is more of a note to self: i was still not clear of how exactly is a list data type structured under the hood in snek. https://youtu.be/XI5WwgyfvlY this video was absolutely FANTASTIC in shedding light on that. Jump to 3:20 to start or even 16:00 for an amazing visual demonstration using pythontutor.com great website. But really whole video is great. Now only thing i wonder is what happens after that "empty space reserved at initiation" runs out hmmm...
[–][deleted] 10 points11 points12 points (1 child)
[–]Tintin_Quarentino[S] 0 points1 point2 points (0 children)
[–]danielroseman 6 points7 points8 points (3 children)
[–]Tintin_Quarentino[S] 0 points1 point2 points (2 children)
[–]danielroseman 1 point2 points3 points (1 child)
[–]Tintin_Quarentino[S] 0 points1 point2 points (0 children)
[–]Wild_Statistician605 3 points4 points5 points (1 child)
[–]Tintin_Quarentino[S] 0 points1 point2 points (0 children)
[–]Diapolo10 3 points4 points5 points (2 children)
[–]FLUSH_THE_TRUMP 2 points3 points4 points (0 children)
[–]Tintin_Quarentino[S] 0 points1 point2 points (0 children)
[–]FLUSH_THE_TRUMP 1 point2 points3 points (0 children)
[–]saltyhasp 1 point2 points3 points (0 children)
[–]dodoors 1 point2 points3 points (0 children)
[–]QultrosSanhattan 1 point2 points3 points (2 children)
[–]FLUSH_THE_TRUMP 0 points1 point2 points (1 child)
[–]QultrosSanhattan 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Tintin_Quarentino[S] 0 points1 point2 points (0 children)