Sunday, November 17, 2024
Google search engine
HomeLanguagesPython program to find the sum of all even and odd digits...

Python program to find the sum of all even and odd digits of an integer list

The following article shows how given an integer list, we can produce the sum of all its odd and even digits.

Input : test_list = [345, 893, 1948, 34, 2346] 
Output : 
Odd digit sum : 36 
Even digit sum : 40 
Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.
Input : test_list = [345, 893] 
Output : 
Odd digit sum : 20 
Even digit sum : 12 
Explanation : 4 + 8 = 12, even summation. 

Method 1 : Using loop, str() and int()

In this, we first convert each element to string and then iterate for each of its element, and add to respective summation by conversion to integer.

Python3




# initializing list
test_list = [345, 893, 1948, 34, 2346]
 
# printing original list
print("The original list is : " + str(test_list))
 
odd_sum = 0
even_sum = 0
 
for sub in test_list:
    for ele in str(sub):
         
        # adding in particular summation according to value
        if int(ele) % 2 == 0:
            even_sum += int(ele)
        else:
            odd_sum += int(ele)
 
# printing result
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))


Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

Time Complexity: O(n*n) where n is the length of the input list test_list, and m is the maximum number of digits in any element of the list.
Auxiliary Space:The auxiliary space complexity of this program is O(1), because the program uses a constant amount of extra space to store the odd_sum and even_sum variables, regardless of the size of the input list.

Method 2: Using loop and sum()

In this, we perform task of getting summation using sum(), and loop is used to perform the task of iterating through each element.

Python3




# initializing list
test_list = [345, 893, 1948, 34, 2346]
 
# printing original list
print("The original list is : " + str(test_list))
 
odd_sum = 0
even_sum = 0
 
for sub in test_list:
 
    # sum() used to get summation of even and odd elements
    odd_sum += sum([int(ele) for ele in str(sub) if int(ele) % 2 == 1])
    even_sum += sum([int(ele) for ele in str(sub) if int(ele) % 2 == 0])
 
# printing result
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))


Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

Time complexity: O(N * M).
Auxiliary space: O(1).

Method 3: Using list comprehension 

Python3




test_list = [345, 893, 1948, 34, 2346]
odd_sum = 0
even_sum = 0
 
odd_sum += sum([int(ele)
                for sub in test_list for ele in str(sub) if int(ele) % 2 == 1])
even_sum += sum([int(ele)
                 for sub in test_list for ele in str(sub) if int(ele) % 2 == 0])
 
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))


Output

Odd digit sum : 36
Even digit sum : 40

Time complexity: O(nm).
Auxiliary space: O(1).

Method 4: Using the enumerate function

Python3




test_list = [345, 893, 1948, 34, 2346]
odd_sum = 0
even_sum = 0
 
odd_sum += sum([int(ele) for i, sub in enumerate(test_list)
                for ele in str(sub) if int(ele) % 2 == 1])
even_sum += sum([int(ele) for i, sub in enumerate(test_list)
                 for ele in str(sub) if int(ele) % 2 == 0])
 
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))


Output

Odd digit sum : 36
Even digit sum : 40

The time complexity of the given program is O(n*k), where n is the number of elements in the list and k is the maximum number of digits in any element of the list.

The space complexity of the given program is O(1) because it uses a constant amount of extra space regardless of the size of the input. 

Method 5: Using recursion

Python3




def digit_sum(sub, even_sum, odd_sum):
    if not sub:
        return (even_sum, odd_sum)
    else:
        ele = sub.pop()
        if int(ele) % 2 == 0:
            even_sum += int(ele)
        else:
            odd_sum += int(ele)
        return digit_sum(sub, even_sum, odd_sum)
 
 
test_list = [345, 893, 1948, 34, 2346]
 
# printing original list
print("The original list is : " + str(test_list))
 
even_sum = 0
odd_sum = 0
 
for sub in test_list:
    sub = list(str(sub))
    even_sum, odd_sum = digit_sum(sub, even_sum, odd_sum)
 
# printing result
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))
 
#This article is published by Vinay Pinjala.


Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

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

Approach: Iterative Digit Summing Approach

Steps:

  1. Initialize two variables, even_sum and odd_sum, to 0.
  2. Iterate over each number in the list using a while loop until the number becomes 0.
  3. Extract the last digit of the number using modulo operator and add it to even_sum if it is even, else add it to odd_sum.
  4. Remove the last digit from the number using integer division operator.
  5. Return both even_sum and odd_sum.

Python3




def sum_of_even_odd_digits(test_list):
    even_sum = 0
    odd_sum = 0
     
    for num in test_list:
        while num != 0:
            digit = num % 10
            if digit % 2 == 0:
                even_sum += digit
            else:
                odd_sum += digit
            num //= 10
             
    return even_sum, odd_sum
 
 
test_list = [345, 893, 1948, 34, 2346]
even_sum, odd_sum = sum_of_even_odd_digits(test_list)
print("Odd digit sum :", odd_sum)
print("Even digit sum :", even_sum)


Output

Odd digit sum : 36
Even digit sum : 40

Time Complexity: O(n * d), where n is the number of elements in the list and d is the maximum number of digits in any number in the list.

Auxiliary Space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments