Concatenation of Alphabetical Group – Infytq 2019 Solve

Problem: The task is to form a group of characters and print them in a specific order.

Given string: “HeLloOWorLDd”
(In order they appear in the string)

Group d -> Dd
Group l -> LlL
Group h ->
Group r -> r
Group o -> oOo ……..etc. Then arrange the Group in ascending order.

Explanation:

Sample Input:

HeLloOWorLDd

Sample Output:

DdWerHoOoLlL

Solution: We strongly recommend you to try the problem first before moving to the solution.

Python

# Code to understand Concatenation of Alphabetical group in Python
# www.codewindow.in

str_list=list(input())
char_list=[]

for i in range (len(str_list)):
    if str_list[i]=='':
        continue
    char_grp=str_list[i]
    
    for j in range (i+1, len(str_list)):
        if str_list[i].lower()==str_list[j].lower():
            char_grp += str_list[j]
            str_list[j]=''
    str_list[i]=''
    char_list.append(char_grp)
for i in range (len(char_list)):
    
    for j in range (i+1, len(char_list)):
        
        if char_list[i].lower() > char_list[j].lower():
            temp=char_list[j]
            char_list[j]=char_list[i]
            char_list[i]=temp
length=len(char_list)

for i in range (length//2):
    print(char_list[i]+char_list[length-i-1], end='')
if length%2!=0:
    print(char_list[length//2])

Input:

HeLloOWorLDd 

Output:

DdWerHoOoLlL

Follow Us

You Missed


Leave a Comment

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