A very big sum is a function that we are required to implement to solve one of the tasks in HackerRank. Now, there are many ways to solve this problem using a variety of programming languages. But for this article, we will see how to create a solution function using Python program.
What is a very big sum problem as given in Hacker Rank?
So, before we start working on the solution, let us go through the problem statement. Here is what it reads:
Calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.
So what this is asking us to do is to write a function that will take an array of elements as it’s input. Next, we will need to add all their values and give back the output.
It is as simple as that!
But here is the catch. When we are adding these values, we should be aware that some of these values can be quite big. So, we need to keep that in mind when creating our solution.
Alright. Sounds good? Great! Then let us start working on our solution next.
Writing A very big sum function in python
So how are we going to write our A very big sum function in Python?
A very big sum Python function implementation
Let us start by defining A very big sum function like below:
def aVeryBigSum(a): pass
So, as you can see, we have created a new Python function called aVeryBigSum. Now as you can see, this function takes in one parameter – An array of numbers. Now that is just as given in the problem right?
If you go through the entire problem question in Hackernews page, you will see that it says that the first input is a number n. So this number will tell us how many input array elements are going to be there. But why do we need this? We need it because we can then loop for so many times. But we can also simply use the length of array to loop, so we will just be using that instead.
So from these, we can update our A very big sum Python function as follows:
# Updated aVeryBigSum function def aVeryBigSum(a): x=0 for i in range(0,len(a)): x= x + ar[i] return x
So as you can see from the above, we ware simply looping through each element and adding them to x. That is all there is to this function!
So, with A very big sum function now implemented in python, we can write the remaining code. What this remaining code will do is just use the Python’s main function to read the n value and the input array elements from the command line. But once we have the input n and the array a, we will just call our big sum function.
What to do in the Python’s main function
So here is how the main function code will then look like:
if __name__ == '__main__': fptr = open('output.txt', 'w') a_count = int(input()) a = list(map(int, input().rstrip().split())) result = aVeryBigSum(a) fptr.write(str(result) + '\n') fptr.close()
So as you can see from the above code, we simply pass our input array to our Big sum function and then get the big sum value back. We then just write this value to an output file using Python’s file write function. So at the end, we will just close this output file pointer.
So there you have it. This is how we will calculate the sum of all the elements of the input array and get the value.
I hope this was a simple Python program for you to understand. But if you have any questions, do let me know in the comments below.
So until next time, ciao!