Write a python program that accepts a hyphen separated sequence of words as input
and print the words in a hyphen separated sequence after sorting them alphabetically.
Ex (Input): green-red-yellow-black-white
Output: black-green-red-white-yellow
Answers
Answered by
5
words = [w for w in input("words: ").split("-")]
words.sort()
print("-".join(words))
Answered by
37
Program: (Python 3)
print( '-'.join ( sorted ( list ( input().split('-') ) ) ) )
(or)
string=sorted(list(input().strip('-')))
print('-'.join(string))
Input:
green-red-yellow-black-white
Output:
black-green-red-white-yellow
Explanation:
- input() takes in the input string 'green-red-yellow-black-white '.
- Once done, .split('-') splits the string based on every triggered '-' in the string, giving ['green', 'red', 'yellow', 'black', 'white']
- Then, the sorted() method sorts the elements in the resulted list lexicographically, giving the resultant sorted list ['black', 'green', 'red', 'white', 'yellow']
- And this sorted list is passed as a parameter into the join() method that joins each of the elements in the list with '-' in the same sorted series leading one joined resultant string.
- And the resultant string will be black-green-red-white-yellow
Learn more:
1. Printing all the palindromes formed by a palindrome word.
https://brainly.in/question/19151384
2. Python program to find absolute difference between the odd and even numbers in the inputted number
https://brainly.in/question/11611140
Similar questions