Open In App

Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List

Improve
Improve
Like Article
Like
Save
Share
Report

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:
 
    # find sum of negative numbers
    def negSum(self, list):
 
        # counter for sum of
        # negative numbers
        neg_sum = 0
 
        for num in list:
 
            # converting number
            # to integer explicitly
            num = int(num)
 
            # if negative number
            if(num < 0):
 
                # simply add to the
                # negative sum
                neg_sum = neg_sum + num
 
        print("Sum of negative numbers is ", neg_sum)
 
    # find sum of positive numbers
    # according to categories
    def posSum(self, list):
 
        # counter for sum of
        # positive even numbers
        pos_even_sum = 0
 
        # counter for sum of
        # positive odd numbers
        pos_odd_sum = 0
        for num in list:
 
            # converting number
            # to integer explicitly
            num = int(num)
 
            # if negative number
            if(num >= 0):
 
                # if even positive
                if(num % 2 == 0):
 
                    # add to positive
                    # even sum
                    pos_even_sum = pos_even_sum + num
                else:
 
                    # add to positive odd sum
                    pos_odd_sum = pos_odd_sum + num
 
        print("Sum of even positive numbers is ",
              pos_even_sum)
 
        print("Sum of odd positive numbers is ",
              pos_odd_sum)
 
 
# input a list of numbers
list_num = [-7, 5, 60, -34, 1]
 
# creating an object of class
obj = Sumofnumbers()
 
# calling method for  sum
# of all negative numbers
obj.negSum(list_num)
 
# calling method for
# sum of all positive numbers
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. 

Space complexity:
The space complexity of the Sumofnumbers class is O(1). This is because the class does not store any additional data and only uses the given list as an input.

Python3




class Sumofnumbers:
    # find sum of numbers
    # according to categories
    def Sum(self, list):
 
        # counter for sum
        # of negative numbers
        neg_sum = 0
 
        # counter for sum of
        # positive even numbers
        pos_even_sum = 0
 
        # counter for sum of
        # positive odd numbers
        pos_odd_sum = 0
 
        for num in list:
 
            # converting number
            # to integer explicitly
            num = int(num)
 
            # if negative number
            if(num < 0):
 
                # simply add
                # to the negative sum
                neg_sum = neg_sum + num
            # if positive number
            else:
 
                # if even positive
                if(num % 2 == 0):
 
                    # add to positive even sum
                    pos_even_sum = pos_even_sum + num
                else:
 
                    # add to positive odd sum
                    pos_odd_sum = 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)
 
 
# input a list of numbers
list_num = [1, -1, 50, -2, 0, -3]
 
# creating an object of class
obj = Sumofnumbers()
 
# calling method for
# largest sum of all numbers
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

Time Complexity: O(n) ,The time complexity of this algorithm is O(n) as it loops over all the numbers in the list and checks each of them to determine whether they are negative, even, or odd.

Space Complexity: O(1) ,The space complexity of this algorithm is O(1) as we are not creating any additional data structures or arrays. We are simply keeping track of the sums of all the numbers in the list and updating them as we loop through the list.

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 i<0]
print("even positive numbers sum",sum(x))
print("odd positive numbers sum",sum(y))
print("negative numbers sum",sum(z))


Output

even positive numbers sum 50
odd positive numbers sum 1
negative numbers sum -6

Time Complexity: O(n)
Auxiliary Space: O(n)

Method: Using the lambda function 

Python3




lst = [1, -1, 50, -2, 0, -3]
x=list(filter(lambda i: (i>0 and i%2==0),lst))
y=list(filter(lambda i: (i>0 and i%2!=0),lst))
z=list(filter(lambda i: (i<0),lst))
print("even positive numbers sum",sum(x))
print("odd positive numbers sum",sum(y))
print("negative numbers sum",sum(z))


Output

even positive numbers sum 50
odd positive numbers sum 1
negative numbers sum -6

The time complexity of the code is O(n), where n is the length of the input list lst. 

The auxiliary space complexity of the code is O(n), where n is the length of the input list lst.

Method: Using enumerate function

Python3




lst = ['1', '-1', '50', '-2',' 0', '-3']
x=[int(i) for a,i in enumerate(lst) if int(i)%2==0 and int(i)>0]
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]
print("even positive numbers sum",sum(x))
print("odd positive numbers sum",sum(y))
print("negative numbers sum",sum(z))


Output

even positive numbers sum 50
odd positive numbers sum 1
negative numbers sum -6

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.

Method: Using recursion

Python3




def recursive_sum(lst, i=0, x=[], y=[], z=[]):
 
    if i == len(lst):
 
        return (sum(x), sum(y), sum(z))
 
    elif lst[i] > 0 and lst[i] % 2 == 0:
 
        x.append(lst[i])
 
    elif lst[i] > 0 and lst[i] % 2 != 0:
 
        y.append(lst[i])
 
    elif lst[i] < 0:
 
        z.append(lst[i])
 
    return recursive_sum(lst, i+1, x, y, z)
 
lst = [1, -1, 50, -2, 0, -3]
 
x,y,z = recursive_sum(lst)
 
print("even positive numbers sum",x)
 
print("odd positive numbers sum",y)
 
print("negative numbers sum",z)
 
#This code is contributed by vinay pinjala.


Output

even positive numbers sum 50
odd positive numbers sum 1
negative numbers sum -6

Time Complexity: O(n), because n recursive calls are made. 
Auxiliary Space: O(n), because of n recursive calls are made and each recursive call is pushed into stack.

Method: Using reduce from the functools module

Python3




from functools import reduce
 
lst = [1, -1, 50, -2, 0, -3]
 
def filter_and_sum(lst, condition):
    filtered = list(filter(condition, lst))
    return reduce(lambda x, y: x + y, filtered)
 
x = filter_and_sum(lst, lambda num: num > 0 and num % 2 == 0)
y = filter_and_sum(lst, lambda num: num > 0 and num % 2 != 0)
z = filter_and_sum(lst, lambda num: num < 0)
 
print("even positive numbers sum", x)
print("odd positive numbers sum", y)
print("negative numbers sum", z)
 
#this code contributed by tvsk


Output

even positive numbers sum 50
odd positive numbers sum 1
negative numbers sum -6

Time Complexity: O(n)

Auxiliary Space: O(n)



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads