We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Don't let this frustrate you. Over time, your coding skills will improve, to the point that, this will become trivial to you :)
In an attempt to help new coders doing this challenge, I will add a few ways this problem could have been resolved; in addition, some comparisons among them.
*****SpoilerAlert
Different approaches
Note that I removed the n parameter. Dont see any use for it here, and len(arr) will provide the same value in constant time, O(1).
# Python comes with a Built-in function, sum, which# takes an iterable and returns the sum of its values.defarray_sum_v1(arr):returnsum(arr)defarray_sum_v2(arr):total=0fornumberinarr:total+=numberreturntotaldefarray_sum_v3(arr):total=0idx=len(arr)-1whileidx>=0:total+=arr[idx]idx-=1returntotal# If you need to preserve `arr`, create an auxiliar# variable ``arr_cp`` and pop directly from that.defarray_sum_v4a(arr):total=0# This is called slicing (can be used to copy the array)arr_cp=arr[:]# It will run until there are no more elements on arr_cpwhilearr_cp:# `pop` removes (and returns) the last element# We could use it directly, without the# variable assignmentnumber=arr_cp.pop()total+=numberreturntotal# We will remove `number = arr_cp.pop()`# and use it directly on the next linedefarray_sum_v4b(arr):total=0whilearr:total+=arr.pop()# Avoids one linereturntotal# Overkill...# But just wanted to show that there are# much more ways to do this simple task; same as# for every non-trivial problem.# What is inside, its called a generator.defarray_sum_v5(arr):returnsum(xforxinarr)
Time comparison
Below I created an array of 10000 random integers in the range [0, 10^9]. This will be used to compare among the functions.
As expected, using the Built-in sum function is faster than looping over every item, loops in Python are kinda inefficient, but again, that is a topic for another day.
Hope this helps :)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Simple Array Sum
You are viewing a single comment's thread. Return to all comments →
Don't let this frustrate you. Over time, your coding skills will improve, to the point that, this will become trivial to you :)
In an attempt to help new coders doing this challenge, I will add a few ways this problem could have been resolved; in addition, some comparisons among them.
Different approaches
Note that I removed the n parameter. Dont see any use for it here, and len(arr) will provide the same value in constant time, O(1).
Time comparison
Below I created an array of 10000 random integers in the range [0, 10^9]. This will be used to compare among the functions.
As expected, using the Built-in sum function is faster than looping over every item, loops in Python are kinda inefficient, but again, that is a topic for another day.
Hope this helps :)