Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to Increment Numeric Strings by K

Improve Article
Save Article
  • Last Updated : 29 Jul, 2022
Improve Article
Save Article

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’]

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’]

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']

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!