Define a function named get_words_from_file(filename) that takes a filename as a parameter. The function reads the conte
Posted: Tue May 24, 2022 8:16 am
Define a function named get_words_from_file(filename) that takes a filename as a parameter. The function reads the contents of the file specified in the parameter and returns a list of words. The words in the file are separated by whitespace. Note: remember to close the file. Click here to download a sample text file. For example: Test Result print (get_words_from_file("input2.txt")) ['the', 'heavy', 'rain', 'is', 'to', 'ease', 'tonight', 'however', 'further', 'showers', 'are', 'expected', 'tomorrow']
Define a function named build_dictionary (words_list) that takes a list of words as a parameter. The function creates a dictionary by looping through each element in the list and creating a corresponding key: value item. The keys consist of integers and the values are lists of uniqu words in lowercase where the length of each word equals the key value. Note: • You can assume there will be no punctuation characters in the words in the parameter words_list (i.e. only letters). However, you should convert all words to lower case. • You can assume that the file only contains unique words. • Each list of words must be sorted in ascending order. For example: Test Result data = ['The', 'heavy', 'rain', 'is', 'to', 'ease', 'tonight', 'however', 'further', 'showers', 'Are', 'expected', 'tomorrow'] 2 ['is', 'to'] a_dict = build_dictionary (data) 3 ['are', 'the'] for key in sorted (a_dict): 4 ['ease', 'rain'] 5 ['heavy'] print(key, a_dict [key]) 7 ['further', 'however', 'showers', 'tonight'] 8 ['expected', 'tomorrow'] 3 ['age', 'ant', 'cat', 'dog', 'eye', 'ist', 'red', 'tea', 'ted', 'ten', 'the'] data = ['ist', 'tea', 'eye', 'the', 'ant', 'ten', 'Ted', 'age', 'dog', 'CAT', 'red'] a_dict= build_dictionary (data) for key in sorted (a_dict): print(key, a dict[key])
Define a function named get_word (words_list) that takes a list of words as a parameter. The function returns a random word from the parameter words list. Note: Do not import the random module. This has been done for you as part of the CodeRunner question. For example: Test Result random.seed (30) a data = get_word(['this', 'is', 'a', 'test']) print (data) random.seed (50) data = ['icy', 'any', 'are', 'hot', 'why'] print(get_word (data)) hot