Q. Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically.


Sample Items: green-red-yellow-black-white

Expected Result: black-green-red-white-yellow



Answer =

def word (x) :
    for i in range (len(x) + 1) :
        for j in range (len(x) - 1) :
            if x[ j ][ 0 ] > x[ j + 1][ 0 ] :
                x[ j ], x[ j + 1 ] = x[ j + 1 ], x[ j ]
                
    string = ("-").join(x)
    print("Sequence After sorting :- ", string)

str = input ("Enter hyphen-separated sequence of words :- ")
a = str.split("-")
word(a)

Output :-

Enter hyphen-separated sequence of words :- green-red-yellow-black-white
Sequence After sorting :-  black-green-red-white-yellow

>>> 

 

2 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post