Computer Science
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
Python
Python Functions
2 Likes
Answer
def sort_words(s):
words = s.split('-')
words.sort()
return '-'.join(words)
input_str = input('Enter a hyphen-separated sequence of words: ')
print(sort_words(input_str))
Output
Enter a hyphen-separated sequence of words: green-red-yellow-black-white
black-green-red-white-yellow
Enter a hyphen-separated sequence of words: delhi-bangalore-mumbai-chennai-lucknow-patna
bangalore-chennai-delhi-lucknow-mumbai-patna
Answered By
2 Likes
Related Questions
Write a Python function that takes a number as a parameter and checks whether the number is prime or not.
Write a Python function that checks whether a passed string is a palindrome or not.
Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
What is a recursive function? Write one advantage of recursive function.
What are the two cases required in a recursive function?