Python program to get all possible slices of a string for K number of slices
Given a string, the task is to write a Python program to get all possible slices for K number of slices.
Input : test_str = “Gfg4all”, K = 3
Output : [[‘G’, ‘f’, ‘g4all’], [‘G’, ‘fg’, ‘4all’], [‘G’, ‘fg4’, ‘all’], [‘G’, ‘fg4a’, ‘ll’], [‘G’, ‘fg4al’, ‘l’], [‘Gf’, ‘g’, ‘4all’], [‘Gf’, ‘g4’, ‘all’], [‘Gf’, ‘g4a’, ‘ll’], [‘Gf’, ‘g4al’, ‘l’], [‘Gfg’, ‘4’, ‘all’], [‘Gfg’, ‘4a’, ‘ll’], [‘Gfg’, ‘4al’, ‘l’], [‘Gfg4’, ‘a’, ‘ll’], [‘Gfg4’, ‘al’, ‘l’], [‘Gfg4a’, ‘l’, ‘l’]]
Explanation : All possible 3 slices for constructing string are returned.
Input : test_str = “Gfg4all”, K = 2
Output : [[‘G’, ‘fg4all’], [‘Gf’, ‘g4all’], [‘Gfg’, ‘4all’], [‘Gfg4’, ‘all’], [‘Gfg4a’, ‘ll’], [‘Gfg4al’, ‘l’]]
Explanation : All possible 2 slices for constructing string are returned.
Method #1 : Using list comprehension + slicing + loop
In this, consecutive slices are computed incrementally starting from size 1. The last element is always split into 2 in all the ways in acc. to other strings.
Python3
# Python3 code to demonstrate working of # All possible slices for K length # Using list comprehension + string slicing + loop # initializing string test_str = "Gfg4all" # printing original string print ( "The original string is : " + str (test_str)) # initializing number of slices K = 3 res = [[test_str]] for idx in range (K - 1 ): # slicing initial strings with difference sizes. res = [[ * strt, end[:y], end[y:]] for * strt, end in res for y in range ( 1 , len (end) - K + idx + 2 )] # printing result print ( "All possible slices for K strings : " + str (res)) |
Output:
The original string is : Gfg4all
All possible slices for K strings : [[‘G’, ‘f’, ‘g4all’], [‘G’, ‘fg’, ‘4all’], [‘G’, ‘fg4’, ‘all’], [‘G’, ‘fg4a’, ‘ll’], [‘G’, ‘fg4al’, ‘l’], [‘Gf’, ‘g’, ‘4all’], [‘Gf’, ‘g4’, ‘all’], [‘Gf’, ‘g4a’, ‘ll’], [‘Gf’, ‘g4al’, ‘l’], [‘Gfg’, ‘4’, ‘all’], [‘Gfg’, ‘4a’, ‘ll’], [‘Gfg’, ‘4al’, ‘l’], [‘Gfg4’, ‘a’, ‘ll’], [‘Gfg4’, ‘al’, ‘l’], [‘Gfg4a’, ‘l’, ‘l’]]
Method #2 : Using combinations() + zip() + list comprehension
In this, combinations is used to get all possible substrings for the ranges computed using slicing during iteration.
Python3
# Python3 code to demonstrate working of # All possible slices for K length # Using combinations() + zip() + list comprehension from itertools import combinations # initializing string test_str = "Gfg4all" # printing original string print ( "The original string is : " + str (test_str)) # initializing number of slices K = 3 # combinations used to perform all possible slices res = [[test_str[idx: j] for idx, j in zip ([ None , * sub], [ * sub, None ])] for sub in combinations( range ( 1 , len (test_str)), K - 1 )] # printing result print ( "All possible slices for K strings : " + str (res)) |
Output:
The original string is : Gfg4all
All possible slices for K strings : [[‘G’, ‘f’, ‘g4all’], [‘G’, ‘fg’, ‘4all’], [‘G’, ‘fg4’, ‘all’], [‘G’, ‘fg4a’, ‘ll’], [‘G’, ‘fg4al’, ‘l’], [‘Gf’, ‘g’, ‘4all’], [‘Gf’, ‘g4’, ‘all’], [‘Gf’, ‘g4a’, ‘ll’], [‘Gf’, ‘g4al’, ‘l’], [‘Gfg’, ‘4’, ‘all’], [‘Gfg’, ‘4a’, ‘ll’], [‘Gfg’, ‘4al’, ‘l’], [‘Gfg4’, ‘a’, ‘ll’], [‘Gfg4’, ‘al’, ‘l’], [‘Gfg4a’, ‘l’, ‘l’]]