In: Computer Science
Python
This part involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back.
The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing.
Create a test program that takes a string as input and then calls the function over and over until the user enters "Done." If the string "Done" is entered, the program sorts the list and prints it out line by line.
Python code:
#defining unique fnction
def unique(string,lst):
#checking if strng is not in lst
if(string not in lst):
#adding string to
lst
lst.append(string)
#returning lst
return lst
#asking for string
string=input("Enter string: ")
#initializing lst
lst=[]
#looping till done is entered
while(string!="Done"):
#calling unique function
lst=unique(string,lst)
#asking for string
string=input("Enter string: ")
#printing the contents in sorted array
print(*sorted(lst),sep="\n")
Screenshot:
Input and Output: