HackerRank Algorithms Warmup Simple Array Sum | Python

Опубликовано: 29 Октябрь 2024
на канале: Over The Shoulder Coding
31,962
310

In this video, I will walk through solving HackerRank’s Algorithm Warmup Challenge: Simple Array Sum.

Try solving it yourself!
http://bit.ly/2Nmw1Dk

View my solution for Simple Array Sum at
http://bit.ly/2NTBQVJ

Join our LinkedIn Group to ask questions and learn from others.
  / 13664544  

Support me on Patreon!
  / overtheshouldercoding  

#OTSC #HackerRank #Python

Given an input array `ar`, print out the sum of its elements. If `ar = [1, 2, 3]` return `6` since `1 + 2 + 3 = 6`.
The first line contains an integer n denoting the size of the array.
The second line contains n space-separated integers representing the array's elements.
Print the sum of the array's elements as a single integer.
1. Iterate over the elements in the array `ar` as integers.
2. Add them to a sum variable that we initialize as 0.
3. Return the sum variable.
HackerRank is already parsing the input for from the 2 lines into an array of integers stored in _ar_.
The also call our method `simpleArraySum` with the parameter, retrieve the output, and print it to the output path. All we need to do is to implement `simpleArraySum`.

We will initialize the sum variable to start at 0.
Then we will make a for loop to iterate over the elements of the array _ar_.
We will add each element to the running total _sum_.
Then when we are done with the for loop, we will return the sum value.