Open In App

Python program to Increment Numeric Strings by K

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given the Strings list, write a Python program to increment strings that are numeric by K.

Examples:

Input : test_list = [“gfg”, “234”, “is”, “98”, “123”, “best”, “4”], K = 6 
Output : [‘gfg’, ‘240’, ‘is’, ‘104’, ‘129’, ‘best’, ’10’] 
Explanation : 234, 98 and 4 are incremented by 6 in result.

Input : test_list = [“gfg”, “234”, “is”, “98”, “123”, “best”, “4”], K = 4 
Output : [‘gfg’, ‘238’, ‘is’, ‘102’, ‘129’, ‘best’, ‘8’] 
Explanation : 234, 98 and 4 are incremented by 4 in result. 

Method #1 : Using str() + int() + loop + isdigit()

In this, we perform task of interconversion of elements using str() + int(), and check for number using isdigit(), iteration is done using loop.

Python3




# Python3 code to demonstrate working of
# Increment Numeric Strings by K
# Using str() + int() + loop + isdigit()
 
# initializing Matrix
test_list = ["gfg", "234", "is", "98", "123", "best", "4"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 6
 
res = []
for ele in test_list:
 
    # incrementing on testing for digit.
    if ele.isdigit():
        res.append(str(int(ele) + K))
    else:
        res.append(ele)
 
# printing result
print("Incremented Numeric Strings : " + str(res))


Output:

The original list is : [‘gfg’, ‘234’, ‘is’, ’98’, ‘123’, ‘best’, ‘4’] Incremented Numeric Strings : [‘gfg’, ‘240’, ‘is’, ‘104’, ‘129’, ‘best’, ’10’]

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

Method #2 : Using list comprehension + isdigit()

This is one of the ways in which this task can be performed. Similar to above, just one line alternative to compact the solution using list comprehension.

Python3




# Python3 code to demonstrate working of
# Increment Numeric Strings by K
# Using list comprehension + isdigit()
 
# initializing Matrix
test_list = ["gfg", "234", "is", "98", "123", "best", "4"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 6
 
# increment Numeric digits.
res = [str(int(ele) + K) if ele.isdigit() else ele for ele in test_list]
 
# printing result
print("Incremented Numeric Strings : " + str(res))


Output:

The original list is : [‘gfg’, ‘234’, ‘is’, ’98’, ‘123’, ‘best’, ‘4’] Incremented Numeric Strings : [‘gfg’, ‘240’, ‘is’, ‘104’, ‘129’, ‘best’, ’10’]

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3 : Without using any built-in methods

Python3




# Python3 code to demonstrate working of
# Increment Numeric Strings by K
# checking whether string is numeric or not
def numeric(num):
    digits="0123456789"
    c=0
    for i in num:
        if i in digits:
            c+=1
    if(c==len(num)):
        return True
    return False
 
# initializing list
test_list = ["gfg", "234", "is", "98", "123", "best", "4"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 6
new_list=[]
# increment numeric strings.
for i in test_list:
    if(numeric(i)):
        new_list.append(str(int(i)+K))
    else:
        new_list.append(i)
# printing result
print("Incremented Numeric Strings : " + str(new_list))


Output

The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']

The time and space complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)

Method #4 : Using isnumeric() method

Python3




# Python3 code to demonstrate working of
# Increment Numeric Strings by K
 
# initializing Matrix
test_list = ["gfg", "234", "is", "98", "123", "best", "4"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 6
 
res = []
for i in test_list:
    if i.isnumeric():
        res.append(str(int(i)+K))
    else:
        res.append(i)
# printing result
print("Incremented Numeric Strings : " + str(res))


Output

The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']

Method#5:Using map and lambda

Python3




def increment_numeric_strings(lst, K):
    return list(map(lambda x: str(int(x) + K) if x.isdigit() else x, lst))
 
test_list = ["gfg", "234", "is", "98", "123", "best", "4"]
print("The original list is:", test_list)
print("Incremented Numeric Strings:", increment_numeric_strings(test_list, 6))
#This code is contributed by Vinay pinjala.


Output

The original list is: ['gfg', '234', 'is', '98', '123', 'best', '4']
Incremented Numeric Strings: ['gfg', '240', 'is', '104', '129', 'best', '10']

Time Complexity: O(n)

Auxiliary Space: O(1)

Method#6: Here’s a solution using try/except:

In this solution, we use a try/except block to check if the element can be converted to an integer. If it can, we increment it by K and add the result to the result list. If the conversion fails, we know that the element is not a number and we add it to the result list as is.

Python3




def increment_numeric_strings(test_list, K):
    result = []
    for ele in test_list:
        try:
            number = int(ele)
            result.append(str(number + K))
        except ValueError:
            result.append(ele)
    return result
 
test_list = ["gfg", "234", "is", "98", "123", "best", "4"]
K = 6
print(increment_numeric_strings(test_list, K))


Output

['gfg', '240', 'is', '104', '129', 'best', '10']

The time complexity of this solution is O(n), where n is the number of elements in test_list. The space complexity is O(n), as we are creating a new list result of the same size as test_list.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads