How do you sum all positive numbers in python?

i need to write a function that takes an array of numbers and finds the maximum sum of all the numbers. In other words, I need to find the sum of just the positive numbers. I wrote this, I'm getting "list is out of range"

thoughts?

    def maximum_sub[A]:
        x = 0
        i = 0
        for i in A:
            while A[i] > 0:
            x+=A[i]
            i+=1
        return x

asked Oct 27, 2016 at 23:21

3

Use super functions and list comprehension instead:

>>> a = [1, 2, 3, -4, 5, -3, 7, 8, 9, 6, 4, -7]
>>> sum[x for x in a if x > 0]
45

[x for x in a if x > 0] will create an array made of the positive values in a.

sum[...] will return the sum of the elements in that array.

answered Oct 27, 2016 at 23:24

UrielUriel

15k6 gold badges24 silver badges46 bronze badges

6

There are better approaches with sums and comprehensions, but I'll assume you're writing a function as an exercise in algorithms.

You don't need the while loop. Use an if to check if it is positive, and add it to the sum. Otherwise, don't do anything. The for loop will automatically iterate over the values for you.

def maximum_sum[A]:
    x = 0
    for i in A:
        if i > 0:
            x += i
    return x

A few words of advice: name things expressively. Here's some names I might use:

def maximum_sum[arr]:
    max_sum = 0
    for num in arr:
        if num > 0:
            max_sum += num
    return max_sum

answered Oct 27, 2016 at 23:31

1

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List.

    Examples:

    Input: -7 5 60 -34 1 
    Output: 
    Sum of negative numbers is  -41 
    Sum of even positive numbers is  60 
    Sum of odd positive numbers is  6
    
    Input: 1 -1 50 -2 0 -3
    Output:
    Sum of negative numbers is  -6
    Sum of even positive numbers is  50
    Sum of odd positive numbers is  1

    Negative numbers are the numbers less than 0 while positive even numbers are numbers greater than 0 and also divisible by 2. 0 is assumed to be a positive even number, in this case. 

    Approach:

    • The first approach input a list of numbers from the user.
    • Two loops are run, one for the positive numbers and the other for the negative numbers, computing the numbers’ summation.
    • If n is the size of the list of numbers,
    • it takes O[2n] time complexity, for iterating over the list of numbers twice.

    Python3

    class Sumofnumbers:

        def negSum[self, list]:

            neg_sum = 0

            for num in list:

                num = int[num]

                if[num < 0]:

                    neg_sum + = num

            print["Sum of negative numbers is ", neg_sum]

        def posSum[self, list]:

            pos_even_sum = 0

            pos_odd_sum = 0

            for num in list:

                num = int[num]

                if[num >= 0]:

                    if[num % 2 == 0]:

                        pos_even_sum + = num

                    else:

                        pos_odd_sum + = num

            print["Sum of even positive numbers is ",

                  pos_even_sum]

            print["Sum of odd positive numbers is ",

                  pos_odd_sum]

    list_num = [-7, 5, 60, -34, 1]

    obj = Sumofnumbers[]

    obj.negSum[list_num]

    obj.posSum[list_num]

    Output:

    Sum of negative numbers is  -41
    Sum of even positive numbers is  60
    Sum of odd positive numbers is  6

    The second approach computes the sum of respective numbers in a single loop. It maintains three counters for each of the three conditions, checks the condition and accordingly adds the value of the number in the current sum . If n is the size of the list of numbers, it takes O[n] time complexity, for iterating over the list of numbers once. 

    Python3

    class Sumofnumbers:

        def Sum[self, list]:

            neg_sum = 0

            pos_even_sum = 0

            pos_odd_sum = 0

            for num in list:

                num = int[num]

                if[num < 0]:

                    neg_sum += num

                else:

                    if[num % 2 == 0]:

                        pos_even_sum + = num

                    else:

                        pos_odd_sum + = num

            print["Sum of negative numbers is ",

                  neg_sum]

            print["Sum of even positive numbers is ",

                  pos_even_sum]

            print["Sum of odd positive numbers is ",

                  pos_odd_sum]

    list_num = [1, -1, 50, -2, 0, -3]

    obj = Sumofnumbers[]

    obj.Sum[list_num]

    Output: 

    Sum of negative numbers is  -6
    Sum of even positive numbers is  50
    Sum of odd positive numbers is  1

    Method: Using list comprehension 

    Python3

    lst = [1, -1, 50, -2, 0, -3];i=0

    x=[i for i in lst if i>0 and i%2==0]

    y=[i for i in lst if i>0 and i%2!=0]

    z=[i for i in lst if i0 and i%2==0],lst]]

    y=list[filter[lambda i: [i>0 and i%2!=0],lst]]

    z=list[filter[lambda i: [i0]

    y=[int[i] for a,i in enumerate[lst] if int[i]%2!=0 and int[i]>0]

    z=[int[i] for a,i in enumerate[lst] if int[i] 0]:.

    sum += num..

    How do you sum all numbers in Python?

    sum[] function in Python Python provides an inbuilt function sum[] which sums up the numbers in the list. Syntax: sum[iterable, start] iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers.

    Is there a summation function in Python?

    Python sum[] Function The sum[] function returns a number, the sum of all items in an iterable.

    Chủ Đề