Tuesday, November 19, 2024
Google search engine
HomeLanguagesPython Program to Subtract K from each digit

Python Program to Subtract K from each digit

Given a list, the task is to write a Python Program to subtract K from each digit, if the element gets below 0, retain 0.

Examples:

Input : test_list = [2345, 8786, 2478, 8664, 3568, 28], K = 4
Output : [1, 4342, 34, 4220, 124, 4]
Explanation : In 2345, 4 subtracted from 2 is -2, hence ceiled to 0. Hence just 5-4 = 1, is retained. and thus output.

Input : test_list = [2345, 8786, 2478, 8664, 3568, 28], K = 3
Output : [12, 5453, 145, 5331, 235, 5]
Explanation : In 2345, 3 subtracted from 2 is -1, hence ceiled to 0. Hence just 5-3 = 2 and 4-3 = 1, are retained. and thus output.

Method 1: Using str() + – operator

In this, we convert the integer to string and perform subtraction by converting each digit to integer, the result is joined again and type cast back to integer.

Python3




# Python3 code to demonstrate working of
# Subtract K from each digit
# Using str() and - operator
 
# initializing list
test_list = [2345, 8786, 2478, 8664, 3568, 28]
              
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
res = []
for ele in test_list:
    str_ele = str(ele)
     
    # getting maximum of 0 or negative value using max()
    # conversion of each digit to int
    new_ele = int(''.join([ str(max(0, int(el) - K)) for el in str_ele]))
    res.append(new_ele)
 
# printing result
print("Elements after subtracting K from each digit : " + str(res))


Output

The original list is : [2345, 8786, 2478, 8664, 3568, 28]
Elements after subtracting K from each digit : [1, 4342, 34, 4220, 124, 4]

Time Complexity: O(n*m) where n is the number of elements in the list and m is the maximum number of digits in any element of the list.
Auxiliary Space: O(n*m)

Method 2: Using list comprehension

Similar to above method, just list comprehension is used for the task of providing a shorthand.

Python3




# Python3 code to demonstrate working of
# Subtract K from each digit
# Using list comprehension
 
# initializing list
test_list = [2345, 8786, 2478, 8664, 3568, 28]
              
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# list comprehension providing shorthand
res = [int(''.join([ str(max(0, int(el) - K)) for el in str(ele)]))
       for ele in test_list]
 
# printing result
print("Elements after subtracting K from each digit : " + str(res))


Output

The original list is : [2345, 8786, 2478, 8664, 3568, 28]
Elements after subtracting K from each digit : [1, 4342, 34, 4220, 124, 4]

Time Complexity: O(n*m) where n is the number of elements in the list and m is the maximum number of digits in any element of the list.
Auxiliary Space: O(n*m)

Method 3: Here is another approach to solve this problem using map and lambda functions:

Python3




# Python3 code to demonstrate working of
# Subtract K from each digit
# Using map and lambda function
  
# initializing list
test_list = [2345, 8786, 2478, 8664, 3568, 28]
               
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K
K = 4
  
# subtracting K from each digit using map and lambda
res = list(map(lambda ele: int(''.join([str(max(0, int(el) - K)) for el in str(ele)])), test_list))
  
# printing result
print("Elements after subtracting K from each digit : " + str(res))


Output

The original list is : [2345, 8786, 2478, 8664, 3568, 28]
Elements after subtracting K from each digit : [1, 4342, 34, 4220, 124, 4]

Time Complexity: O(nm) where n is the number of elements in the list and m is the maximum number of digits in any element of the list.
Auxiliary Space: O(nm)

Method 4 : Using a loop and modulo operator

Step-by-step approach:

Initialize the input list of numbers.
Initialize the value of K.
Create an empty list to store the results.
Loop through each number in the input list.
Convert the number to a string and create an empty string to store the new digits.
Loop through each digit in the string.
Subtract K from the digit using the modulo operator and append the result to the new digits string.
Convert the new digits string back to an integer and append it to the results list.
Return the results list.

Python3




# Python3 code to demonstrate working of
# Subtract K from each digit
# Using a loop and modulo operator
 
# initializing list
test_list = [2345, 8786, 2478, 8664, 3568, 28]
              
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# Using a loop and modulo operator
res = []
for ele in test_list:
    new_digits = ''
    for digit in str(ele):
        new_digit = int(digit) - K
        new_digits += str(max(new_digit, 0))
    res.append(int(new_digits))
 
# printing result
print("Elements after subtracting K from each digit : " + str(res))


Output

The original list is : [2345, 8786, 2478, 8664, 3568, 28]
Elements after subtracting K from each digit : [1, 4342, 34, 4220, 124, 4]

Time complexity: O(n * k), where n is the number of elements in the list and k is the number of digits in each element.

Auxiliary space: O(n), where n is the number of elements in the list, for storing the results list.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments