Wednesday, 13 June 2018

Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically using Python Programming.

# Write a program that accepts a sequence of whitespace separated words as input and
# prints the words after removing all duplicate words and sorting them
# alphanumerically

str = input("Enter a whitespace seprated string : ")

if len(str)>0:
    sstr=""
    wordList = str.split(" ")
    wordList = sorted(wordList)
    for w in wordList:
        if sstr.count(w)==0:
            sstr +=w
            sstr +=" "
    sstr = sstr[:-1]
    print("Sorted String : ",sstr)
else:
    print("String is empty.")

input("Press Enter to exit.")

2 comments:

  1. sstr = sstr[:-1]

    what is the function for having this code

    ReplyDelete
    Replies
    1. sstr = sstr[:-1]

      it is used to remove extra whitespace from at the end of string.

      Delete

Featured post

In all other cases the driver is not insured. If the marital status, sex and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not.

  #include #include int main() { char ms;   printf("Is Driver married (Y/N): ");  scanf("%c",&ms);   if(ms=='y&#...

Popular Posts