Recursive Split String

The program must accept a string S as the input. The program must split the string recursively based on the following conditions.

  • If the length of the string is even, then the program must split the string into two equal halves. Else consider the middle character for the second half. After dividing the
    string, the program must print the resulting string values.
  • Then the program must repeat the above process of dividing each word in the string S till the length of each word becomes 1.

Boundary Condition(s):
2 <= Length of S <= 100

Input Format:
The first line contains S.

Output Format:
The lines contain the string values separated by a space.

Example Input/Output 1:
Input:
abcdefgh

Output:
abcd efgh
ab cd ef gh
a b c d e f g h

Explanation:
Here the given string is abcdefgh.
1st split: abcd efgh
2nd split: ab cd ef gh
3rd split: a b c d e f g h

Example Input/Output 2:
Input:
exhibition

Output:
exhib ition
ex hib it ion
e x h ib i t i on
e x h i b i t i o n

s=input().strip()
l=[s]
j=0
while j<10 and len(l)<len(s):
    x=[]
    for i in l:
        if len(i)!=1:
            x.append(i[:len(i)//2])
            x.append(i[len(i)//2:])
        else:
            x.append(i)
    l=x[:]
    j+=1
    print(*l)
Previous Article

Array Maximum Sum Divisible By N

Next Article

Friend requests in social network

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *