Studying for a test...this may be a question but I am stuck and can't figure out what I am doing wrong.
def theSum(aList): s = 0 for x in aList: if x > 0: s+=x return theSum
alper
2,4635 gold badges42 silver badges75 bronze badges
asked Mar 1, 2015 at 20:42
7
Your logic seems correct but you have a couple of indentation and variable errors in your code.
Instead of returning function itself, you should return s:
def theSum(aList): s = 0 for x in aList: if x > 0: s = s + x return s >>> print theSum([-1, 1, -2, 2]) 3answered Mar 1, 2015 at 20:50
Ozgur VatanseverOzgur Vatansever
46.6k17 gold badges81 silver badges116 bronze badges
Not the answer you're looking for? Browse other questions tagged python sum or ask your own question.
View Discussion
Improve Article
Save Article
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 1Negative 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 6The 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 1Method: 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 -6Method: 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 -6Method: 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