Write a program that accepts a hash-separated sequence of words as input and prints the words in a hash-separated sequence after sorting them alphabetically in reverse order.
Input:
hey#input#bye
Output:
input#hey#bye
Python Code:
# Taking input of hash-separated sequence of words
input_str = input()
# Splitting the input string into a list of words
words_list = input_str.split("#")
# Sorting the list of words alphabetically in reverse order
sorted_words = sorted(words_list, reverse=True)
# Joining the sorted words into a hash-separated sequence
output_str = "#".join(sorted_words)
# Printing the sorted words in a hash-separated sequence
print(output_str,end="")
Explanation:
- We take the input of a hash-separated sequence of words using the
input()
function and store it in theinput_str
variable. - We split the
input_str
string into a list of words using thesplit()
method and the "#" character as a separator. We store the list of words in thewords_list
variable. - We sort the
words_list
alphabetically in reverse order using thesorted()
function with thereverse=True
parameter. We store the sorted list of words in thesorted_words
variable. - We join the sorted words into a hash-separated sequence using the
join()
method with "#" as the separator. We store the hash-separated sequence in theoutput_str
variable. - We print the sorted words in a hash-separated sequence using the
print()
function with theend
parameter set to an empty string. Theend
parameter specifies what to print at the end of the string, and setting it to an empty string prevents theprint()
function from adding a newline character at the end of the output.
Note: This code assumes that the input string contains only words separated by "#" characters. If the input string contains other characters or symbols, the code may produce unexpected results. Additionally, the code assumes that the input string is not empty, otherwise, the code may produce an error.
No comments:
Post a Comment