Skip to content
Advertisement

Tag: sum

Iterative summation

I’m trying to write a python code that allows me to iteratively sum up the average values of three elements of a list, starting with the third element and its two predecessors. Let me give you an example: I want to calculate the following: Since I’m quite new to programming I couldn’t find an effective way of putting this into

Python: Maximum Subarray Sum

I’ve just tried to start learning Python today, so I am not knowledgeable when it comes to it’s functions. However, I found the maximum subarray problem, and wanted to try and solve it with the few, simple logical commands I have at my disposal. I have gotten stuck, and am almost certain the problem is in my logic and not

Sum of the integers from 1 to n

I’m trying to write a program to add up the numbers from 1 to n. I’ve managed to get it to print the numbers several times but not to add them all. It keeps on just adding two of the numbers. My 1st attempt is: How can I fix this problem? For the recursive version of this question, see Recursive

Sum slices of consecutive values in a NumPy array

Let’s say I have a numpy array a containing 10 values. Just an example situation here, although I would like to repeat the same for an array with length 100. I would like to sum the first 5 values followed by the second 5 values and so on and store them in a new empty list say b. So b

Sum the digits of a number

If I want to find the sum of the digits of a number, i.e.: Input: 932 Output: 14, which is (9 + 3 + 2) What is the fastest way of doing this? I instinctively did: and I found this online: Which is best to use for speed, and are there any other methods which are even faster? Answer Both

Add SUM of values of two LISTS into new LIST

I have the following two lists: Now I want to add the items from both of these lists into a new list. output should be Answer The zip function is useful here, used with a list comprehension. If you have a list of lists (instead of just two lists):

Advertisement