Open In App

Python Program to Join Equi-hierarchy Strings

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of strings with several hierarchies, the task is to write a python program to join those which have the same hierarchies.

Elements in the same dimension before a dimension change are known to be in the same hierarchy.

Input : test_list = [“gfg “, ” best “, [” for “, ” all “], ” all “, [” CS ” , ” geeks “]]

Output : [‘gfg  best ‘, [‘ for  all ‘], ‘ all ‘, [‘ CS  geeks ‘]]

Explanation : Strings at similar hierarchy as joined.

Input : test_list = [“gfg “, ” best “, [” for “, ” all “], [” CS ” , ” geeks “]]

Output : [‘gfg  best ‘, [‘ for  all ‘], [‘ CS  geeks ‘]]

Explanation : Strings at similar hierarchy as joined.

Method 1 : Using type(), loop, recursion and join()

In this, the list element is checked to be string using type(), if found, the joining task is performed using join for that hierarchy. If element is found to be list, the inner list is recurred for similar logic for next level of hierarchy.

Example:

Python3




def hierjoin(test_list):
    res = []
    temp = []
    val = None
    for sub in test_list:
 
        # if string then appended
        if type(sub) == str:
            temp.append(sub)
 
        # if list, the string is joined for hierarchy
        # recurred for inner list
        else:
            res.append(''.join(temp))
            temp = []
            val = hierjoin(sub)
            res.append(val)
    if temp != []:
        res.append(''.join(temp))
    return res
 
 
# initializing list
test_list = ["gfg ", " best ", [" for ", " all "],
             " all ", [" CS ", " geeks "]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# calling recursion
res = hierjoin(test_list)
 
# printing result
print("The joined strings : " + str(res))


Output:

The original list is : [‘gfg ‘, ‘ best ‘, [‘ for ‘, ‘ all ‘], ‘ all ‘, [‘ CS ‘, ‘ geeks ‘]]

The joined strings : [‘gfg  best ‘, [‘ for  all ‘], ‘ all ‘, [‘ CS  geeks ‘]]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 2 : Using join(), map(), recursion and groupby()

In this equihierarchy groups are formed using groupby(). Then map() is used to call the recursion function for inner hierarchy of list strings.

Example:

Python3




from itertools import groupby
 
 
def hierjoin(test_list):
 
    # groups are formed for similar hierarchy using groupby
    return [idx for x, y in groupby(test_list, key=str.__instancecheck__)
            for idx in ([''.join(y)] if x else map(hierjoin, y))]
 
 
# initializing list
test_list = ["gfg ", " best ", [" for ", " all "],
             " all ", [" CS ", " geeks "]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# calling recursion
res = hierjoin(test_list)
 
# printing result
print("The joined strings : " + str(res))


Output:

The original list is : [‘gfg ‘, ‘ best ‘, [‘ for ‘, ‘ all ‘], ‘ all ‘, [‘ CS ‘, ‘ geeks ‘]]

The joined strings : [‘gfg  best ‘, [‘ for  all ‘], ‘ all ‘, [‘ CS  geeks ‘]]

The Time and Space Complexity of all the methods is :

Time Complexity: O(n2)

Space Complexity: O(n)

Approach 3: Using isinstance() and recursion

Python3




# Method 3: Using list comprehensions
 
def hierjoin(test_list):
    res = []
    temp = []
    for sub in test_list:
        if type(sub) == str:
            temp.append(sub)
        else:
            if temp:
                res.append(''.join(temp))
                temp = []
            res.append(hierjoin(sub))
    if temp:
        res.append(''.join(temp))
    return res
 
# Test
test_list = ["gfg ", " best ", [" for ", " all "], " all ", [" CS ", " geeks "]]
print(hierjoin(test_list))
#This code is contributed by Edula Vinay Kumar Reddy


Output

['gfg  best ', [' for  all '], ' all ', [' CS  geeks ']]

Time Complexity: O(n)
Auxiliary Space: O(n)
This approach uses the built-in function isinstance to check if an element is a string or not. The approach is the same as the first method, but using isinstance instead of type for checking the type of an element. The time and space complexity remains the same.



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads