Python - Incremental Size Chunks from Strings
Last Updated :
15 May, 2023
Given a String, split it into incremental sizes consecutive list.
Input : test_str = 'geekforgeeks is best'
Output : ['g', 'ee', 'kfo', 'rgee', 'ks is', ' best']
Explanation : Characters size increasing in list.
Input : test_str = 'geekforgeeks'
Output : ['g', 'ee', 'kfo', 'rgee', 'ks']
Explanation : Characters size increasing in list.
Method #1 : Using loop + slicing
In this, we perform task of getting chunks using string slicing and keep on increasing chunk size during iteration.
Python3
# Python3 code to demonstrate working of
# Incremental Size Chunks from Strings
# Using loop + slicing
# initializing string
test_str = 'geekforgeeks is best for geeks'
# printing original string
print("The original string is : " + str(test_str))
res = []
idx = 1
while True:
if len(test_str) > idx:
# chunking
res.append(test_str[0 : idx])
test_str = test_str[idx:]
idx += 1
else:
res.append(test_str)
break
# printing result
print("The Incremental sized Chunks : " + str(res))
OutputThe original string is : geekforgeeks is best for geeks
The Incremental sized Chunks : ['g', 'ee', 'kfo', 'rgee', 'ks is', ' best ', 'for gee', 'ks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using generator + slicing
In this, we perform slicing as in above method, difference is that chunks are rendered using generator expression, each chunk yields at runtime in loop.
Python3
# Python3 code to demonstrate working of
# Incremental Size Chunks from Strings
# Using generator + slicing
# generator function
def gen_fnc(test_str):
strt = 0
stp = 1
while test_str[strt : strt + stp]:
# return chunks runtime while looping
yield test_str[strt : strt + stp]
strt += stp
stp += 1
# initializing string
test_str = 'geekforgeeks is best for geeks'
# printing original string
print("The original string is : " + str(test_str))
# calling fnc.
res = list(gen_fnc(test_str))
# printing result
print("The Incremental sized Chunks : " + str(res))
OutputThe original string is : geekforgeeks is best for geeks
The Incremental sized Chunks : ['g', 'ee', 'kfo', 'rgee', 'ks is', ' best ', 'for gee', 'ks']
Time Complexity: O(n)
Space Complexity: O(n)
Method 3: Using recursion.
Python3
# Python3 code to demonstrate working of
# Incremental Size Chunks from Strings
# Using recursion
# recursive function to generate chunks
def recursive_fnc(test_str, res, strt=0, stp=1):
if not test_str[strt:strt+stp]:
return res
else:
res.append(test_str[strt:strt+stp])
return recursive_fnc(test_str, res, strt+stp, stp+1)
# initializing string
test_str = 'geekforgeeks is best for geeks'
# printing original string
print("The original string is : " + str(test_str))
# calling recursive function to generate chunks
res = recursive_fnc(test_str, [])
# printing result
print("The Incremental sized Chunks : " + str(res))
OutputThe original string is : geekforgeeks is best for geeks
The Incremental sized Chunks : ['g', 'ee', 'kfo', 'rgee', 'ks is', ' best ', 'for gee', 'ks']
The time complexity of this approach is also O(n^2), where n is the length of the input string.
The space complexity is O(n), as the function generates chunks one at a time and stores them in a list.
Similar Reads
Python | Reverse Incremental String Slicing Sometimes, while working with Python strings, we can have a problem in which we need to perform the slice and print of strings in reverse order. This can have applications in day-day programming. Let us discuss certain ways in which this task can be performed. Method #1: Using loops This is the brut
4 min read
Python - Custom space size padding in Strings List In this article given a Strings List, the task is to write a Python program to pad each string with spaces with specified leading and trailing number of spaces required. Examples: Input: test_list = ["Gfg", "is", "Best"], lead_size = 3, trail_size = 2 Output: [' Gfg ', ' is ', ' Best '] Explanation:
3 min read
Size of String in Memory - Python There can be situations in which we require to get the size that the string takes in bytes using Python which is usually useful in case one is working with files. Let's discuss certain ways in which this can be performed.Using sys.getsizeof()This task can also be performed by one of the system calls
2 min read
Divide String into Equal K chunks - Python The task is to split the string into smaller parts, or "chunks," such that each chunk has exactly k characters. If the string cannot be perfectly divided, the last chunk will contain the remaining characters.Using List ComprehensionList comprehension allows for creating a new list by applying an exp
2 min read
Python - Split a String by Custom Lengths Given a String, perform split of strings on the basis of custom lengths. Input : test_str = 'geeksforgeeks', cus_lens = [4, 3, 2, 3, 1] Output : ['geek', 'sfo', 'rg', 'eek', 's'] Explanation : Strings separated by custom lengths.Input : test_str = 'geeksforgeeks', cus_lens = [10, 3] Output : ['geeks
2 min read
Python | Convert String to N chunks tuple Sometimes, while working with Python Strings, we can have a problem in which we need to break a string to N sized chunks to a tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + tuple This is one approach in which this task can be performed.
2 min read