Open In App

Python – Kth digit Sum

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Numeric list, extract the sum of the Kth digit.

Input : test_list = [5467, 34232, 45456, 22222, 3455], K = 2 
Output : 19 
Explanation : 6 + 2 + 4 + 2 + 5 = 19.

Input : test_list = [5467, 34232, 45456, 22222, 3455], K = 0 
Output : 17 
Explanation : 5 + 3 + 4 + 2 + 3 = 17. 

Method #1 : Using str() + loop

In this, we convert the element to string and then compute the summation of only Kth digit by extracting it using loop.

Python3




# Python3 code to demonstrate working of
# Kth digit Sum
# Using loop + sum()
 
# initializing list
test_list = [5467, 34232, 45456, 22222, 3455]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
res = 0
for ele in test_list:
 
    # adding Kth digit
    res += int(str(ele)[K])
 
# printing result
print("Kth digit sum : " + str(res))


Output:

The original list is : [5467, 34232, 45456, 22222, 3455]
Kth digit sum : 19

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

Method #2 : Using sum() + list comprehension + str()

In this, we perform the task of getting sum using sum(), and list comprehension is used to get one-liner approach to the problem. 

Python3




# Python3 code to demonstrate working of
# Kth digit Sum
# Using sum() + list comprehension + str()
 
# initializing list
test_list = [5467, 34232, 45456, 22222, 3455]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# sum() getting summation
res = sum([int(str(ele)[K]) for ele in test_list])
 
# printing result
print("Kth digit sum : " + str(res))


Output:

The original list is : [5467, 34232, 45456, 22222, 3455]
Kth digit sum : 19

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

Use string slicing to extract the Kth digit from each number:

Approach:

Define a function named “kth_digit_sum” that takes two arguments, a list of integers named “test_list” and an integer named “K”.
Convert each integer in the list to a string using the “str” function and map it to a new list using the “map” function.
Slice the Kth character from each string using string slicing and map it to a new list using the “map” function.
Convert the list of characters to a list of integers using the “map” and “int” functions.
Add up the integers in the list using the “sum” function and return the result.
Use the function to solve the given input and print the output.

Python3




def kth_digit_sum(test_list, K):
    str_list = map(str, test_list)
    char_list = map(lambda x: x[K:K+1], str_list)
    int_list = map(int, char_list)
    return sum(int_list)
 
# Example usage:
test_list = [5467, 34232, 45456, 22222, 3455]
K = 2
result = kth_digit_sum(test_list, K)
print(result)  # Output: 19
 
K = 0
result = kth_digit_sum(test_list, K)
print(result)  # Output: 17


Output

19
17

Time complexity: O(n * k), where n is the length of the list and k is the value of K.
Auxiliary Space: O(n * k), because we create a new list for each step of the mapping operation.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads