Python program to convert String to K sized Numerical Rows
Last Updated :
18 Apr, 2023
Given a string of alphabets, convert it to K sized numerical rows, which contain the number being the positional value of characters.
Input : test_str = ‘geeksforgeek’, K = 4
Output : [[6, 4, 4, 10], [18, 5, 14, 17], [6, 4, 4, 10]]
Explanation : g is at 6th position in alphabet hence g→ 6 and the string is split after every fourth character
Input : test_str = ‘geeksforgeek’, K = 3
Output : [[6, 4, 4], [10, 18, 5], [14, 17, 6], [4, 4, 10]]
Explanation : g is at 6th position in alphabet hence g→ 6 and the string is split after every third character
Method 1 : Using loop + index()
In this, we perform an iteration of each character using a loop and fetch the required position of the character in alphabets using index() on an ordered character list.
Python3
test_str = 'geeksforgeekscse'
print ( "The original string is : " + str (test_str))
K = 4
alphabs = "abcdefghijklmnopqrstuvwxyz"
res = []
temp = []
for ele in test_str:
temp.append(alphabs.index(ele))
if len (temp) = = K:
res.append(temp)
temp = []
if temp ! = []:
res.append(temp)
print ( "Grouped and Converted String : " + str (res))
|
Output:
The original string is : geeksforgeekscse Grouped and Converted String : [[6, 4, 4, 10], [18, 5, 14, 17], [6, 4, 4, 10], [18, 2, 18, 4]]
Method #2 : Using ljust() + ord() + list comprehension
In this, we perform the task of padding is required to have equal length rows using ljust(), then get numerical alphabetic positions using ord(), list comprehension with slicing helps to convert the list to K chunked Matrix.
Python3
from math import ceil
test_str = 'geeksforgeekscse'
print ( "The original string is : " + str (test_str))
K = 4
temp = test_str.ljust(ceil( len (test_str) / K) * K)
temp = [ 0 if char = = ' ' else ( ord (char) - 97 ) for char in temp]
res = [temp[idx: idx + K] for idx in range ( 0 , len (temp), K)]
print ( "Grouped and Converted String : " + str (res))
|
Output:
The original string is : geeksforgeekscse Grouped and Converted String : [[6, 4, 4, 10], [18, 5, 14, 17], [6, 4, 4, 10], [18, 2, 18, 4]]
Time Complexity: O(n)
Space Complexity: O(n)
Method 3: Using dictionary
In this method, we use a dictionary to store the alphabet and its corresponding numerical value. Then we loop through the string and convert each character to its corresponding numerical value using the dictionary.
Python3
test_str = 'geeksforgeekscse'
print ( "The original string is : " + str (test_str))
K = 4
alphabet_dict = { chr (i): i - 97 for i in range ( 97 , 123 )}
temp = [alphabet_dict[ele] for ele in test_str]
res = [temp[i:i + K] for i in range ( 0 , len (temp), K)]
print ( "Grouped and Converted String : " + str (res))
|
Output
The original string is : geeksforgeekscse
Grouped and Converted String : [[6, 4, 4, 10], [18, 5, 14, 17], [6, 4, 4, 10], [18, 2, 18, 4]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4 : using the built-in zip() function
Step-by-step approach:
- Initialize the string test_str.
- Print the original string.
- Initialize the value of K.
- Create a list comprehension that converts each character in the string to its corresponding numerical value using the dictionary alphabet_dict.
- Use the zip() function to group the list into K-sized sublists.
- Print the resulting list.
Below is the implementation of the above approach:
Python3
test_str = 'geeksforgeekscse'
print ( "The original string is : " + str (test_str))
K = 4
alphabet_dict = { chr (i): i - 97 for i in range ( 97 , 123 )}
temp = [alphabet_dict[ele] for ele in test_str]
res = [temp[i:i + K] for i in range ( 0 , len (temp), K)]
print ( "Grouped and Converted String : " + str (res))
|
Output
The original string is : geeksforgeekscse
Grouped and Converted String : [[6, 4, 4, 10], [18, 5, 14, 17], [6, 4, 4, 10], [18, 2, 18, 4]]
Time complexity: O(n), where n is the length of the input string test_str, as we need to iterate through the string to convert each character to its corresponding numerical value, and then group them into sublists.
Auxiliary space: O(n), as we need to store the converted numerical values in a temporary list temp, which has the same length as the input string test_str, and also the resulting list res.
Share your thoughts in the comments
Please Login to comment...