Design a function that takes in a string and outputs a list of all the unique words in the string.
Design a function that takes in a string and outputs a list of all the unique words in the string. Example input: "I am unique" Example output: ["I", "am", "unique"]
def unique_words(string): This function takes in a string and outputs a list of all the unique words in the string. # split the string into a list of words words = string.split() # create a set of unique words unique_words = set(words) # convert the set to a list unique_words = list(unique_words) # return the list return unique_words # test the function print(unique_words("I am unique")) print(unique_words("I am unique I am unique")) print(unique_words("I am unique I am unique I am unique")) This solution is optimal because it uses a set to create a list of unique words.
This solution is correct and uses an efficient approach.
Evaluated at: 2022-11-20 10:16:26