Question: 1- This program reads a file called 'test.txt'. You are required to write two functions that build a wordlist out of all of the words
1-
This program reads a file called 'test.txt'. You are required to write two functions that build a wordlist out of all of the words found in the file and print all of the unique words found in the file. Remove punctuations using 'string.punctuation' and 'strip()' before adding words to the wordlist.
Write a function build_wordlist() that takes a 'file pointer' as an argument and reads the contents, builds the wordlist after removing punctuations, and then returns the wordlist. Another function find_unique() will take this wordlist as a parameter and return another wordlist comprising of all unique words found in the wordlist.
Example:
Contents of 'test.txt':
test file another line in the test file
Output:
['another', 'file', 'in', 'line', 'test', 'the']
This the skeleton for 1:
#build_wordlist() function goes here
#find_unique() function goes here
def main(): infile = open("test.txt", 'r') word_list = build_wordlist(infile) new_wordlist = find_unique(word_list) new_wordlist.sort() print(new_wordlist) main()
2-
Write a function called 'game_of_eights()' that accepts a list of numbers as an argument and then returns 'True' if two consecutive eights are found in the list. For example: [2,3,8,8,9] -> True. The main() function will accept a list of numbers separated by commas from the user and send it to the game_of_eights() function. Within the game_of_eights() function, you will provide logic such that:
the function returns True if consecutive eights (8) are found in the list; returns False otherwise.
the function can handle the edge case where the last element of the list is an 8 without crashing.
the function prints out an error message saying 'Error. Please enter only integers.' if the list is found to contain any non-numeric characters. Note that it only prints the error message in such cases, not 'True' or 'False'.
Examples:
Enter elements of list separated by commas: 2,3,8,8,5 True
Enter elements of list separated by commas: 3,4,5,8 False
Enter elements of list separated by commas: 2,3,5,8,8,u Error. Please enter only integers.
Hint: You will need to use try-except to catch exceptions.
This is the skeleton for 2:
#game_of_eights() function goes here
def main(): a_list = input("Enter elements of list separated by commas: ").split(',') result = game_of_eights(a_list)
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
