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
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.
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]]
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.
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]]
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.
OutputThe original string is : geeksforgeekscse
Grouped and Converted String : [[6, 4, 4, 10], [18, 5, 14, 17], [6, 4, 4, 10], [18, 2, 18, 4]]
OutputThe original string is : geeksforgeekscse
Grouped and Converted String : [[6, 4, 4, 10], [18, 5, 14, 17], [6, 4, 4, 10], [18, 2, 18, 4]]