Python – Every Kth Strings Uppercase
Last Updated :
21 Apr, 2023
Given a String list, change every Kth string to uppercase.
Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 3
Output : ['GFG', 'is', 'best', 'FOR', 'geeks']
Explanation : All Kth strings are uppercased.
Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 4
Output : ['GFG', 'is', 'best', 'for', 'GEEKS']
Explanation : All Kth strings are uppercased.
Method #1 : Using loop + upper()
In this, we iterate for all strings using loop and upper is used to perform uppercase, Kth index is detected using modulo operator.
Python3
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" , "and" , "CS" ]
print ( "The original list is : " + str (test_list))
K = 3
res = []
for idx in range ( len (test_list)):
if idx % K = = 0 :
res.append(test_list[idx].upper())
else :
res.append(test_list[idx])
print ( "The resultant String list : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']
Time Complexity: O(n), where n is length of test_list.
Auxiliary Space: O(n), where n is length of res list to store the result.
Method #2: Using list comprehension
This is yet another way in which this task can be performed. In this we use list comprehension as shorthand, performs tasks similar to the above method.
Python3
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" , "and" , "CS" ]
print ( "The original list is : " + str (test_list))
K = 3
res = [test_list[idx].upper() if idx % K = = 0 else test_list[idx]
for idx in range ( len (test_list))]
print ( "The resultant String list : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using reduce():
Algorithm:
- Initialize the list test_list with the given strings.
- Print the original list test_list.
- Initialize the value of K to 3.
- Define a lambda function to_upper that takes an index and an element as input and returns the uppercase
- version of the element if the index is a multiple of K, otherwise returns the original element.
- Use the reduce method from the functools module to apply the lambda function to each element of the
- test_list and accumulate the results in a list called res.
- Print the resulting uppercase string list res.
Python3
from functools import reduce
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" , "and" , "CS" ]
print ( "The original list is : " + str (test_list))
K = 3
to_upper = lambda idx, elem: elem.upper() if idx % K = = 0 else elem
res = reduce ( lambda acc, elem: acc + [to_upper( len (acc), elem)], test_list, [])
print ( "The resultant String list : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']
Time Complexity: O(n), where n is the length of the input list test_list. The reduce method applies the to_upper lambda function to each element of the test_list exactly once, so the time complexity of the reduce method is O(n).
Space Complexity: O(n). The test_list and res lists both have a size of n, where n is the length of the input list. The to_upper lambda function requires constant space, so its space complexity is O(1).
Share your thoughts in the comments
Please Login to comment...