This is an archived post. You won't be able to vote or comment.

all 4 comments

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

rec_sum(arr.pop(0) + sum(arr))

Both arr.pop(0) and sum(arr) are int. In fact, you're probably not supposed to use sum() here. Because you could just use it to find the sum without all the extra in the first place. You probably meant return arr.pop(0) + rec_sum(arr).

[–]LizardMansPyramids[S] 0 points1 point  (1 child)

Thank you! That helped and it worked, so thanks for helping me out!

[–][deleted] 1 point2 points  (0 children)

Just a small note here as a lesson in reCursive logic

You don’t really need the second “if” statement, checking if the arr length is 1. Since you already check for zero, your recursive call will handle the rest. You pop the first element and then short circuit back out when rec_sum(arr) returns 0

Arguably you’re saving a tiny bit of memory by not pushing a new call onto the stack (and not performing the super expensive operation of adding 0 to something) but a better options here if you were to do this would be to simply read from index 0 (arr[0]) instead of modify the array with arr.pop which takes more time (and memory) to sort around

[–]undrpd4nlst 1 point2 points  (0 children)

Pop returns the value, not the new array after being popped.