📜  Python程序在列表中查找负,正偶数和正奇数的总和

📅  最后修改于: 2022-05-13 01:55:40.504000             🧑  作者: Mango

Python程序在列表中查找负,正偶数和正奇数的总和

给出一个列表。任务是找到列表中存在的负数、正偶数和正奇数的总和。

例子:

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

负数是小于 0 的数,而正偶数是大于 0 并且也可以被 2 整除的数。在这种情况下,0 被假定为正偶数。

方法:

  • 第一种方法输入来自用户的数字列表。
  • 运行两个循环,一个用于正数,另一个用于负数,计算数字的总和。
  • 如果 n 是数字列表的大小,
  • 它需要 O(2n) 时间复杂度,用于对数字列表进行两次迭代。
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 + = 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 + = num
                else:
  
                    # add to positive 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)


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 += num
            # if positive number
            else:
                
                # if even positive
                if(num % 2 == 0):
                    
                    # add to positive even sum
                    pos_even_sum + = num
                else:
                      
                    # add to positive 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)


输出:

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

第二种方法计算单个循环中各个数字的总和。它为三个条件中的每一个维护三个计数器,检查条件并相应地将数字的值添加到当前 sum 中。如果 n 是数字列表的大小,则需要 O(n) 时间复杂度来迭代数字列表一次。

蟒蛇3

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 += num
            # if positive number
            else:
                
                # if even positive
                if(num % 2 == 0):
                    
                    # add to positive even sum
                    pos_even_sum + = num
                else:
                      
                    # add to positive 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)

输出:

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