Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this question is to write a complete Python program that computes the frequencies of palindromes in some English language text. The text

image text in transcribed
image text in transcribed
image text in transcribed
The purpose of this question is to write a complete Python program that computes the frequencies of palindromes in some English language text. The text is in a file named relativity.bxt, which you must download from Moodle. Insert the following function near the beginning of your program. def getWords(filename): filename -> list of words from the file Given the name of a file return a list of words from the file. = filename - the name of the file containing the words #infile - the file descriptor data-the content of the file as one string infile = open(filename, Y) data = infile.reado infile.close retum data split Later in the course we will learn what each statement does. This function reads the text from the file and splits the file up into a list of words and then returns the list of words. Write a function that begins with the following header: def is Palindrome(word): This function is given a string of characters known as word. The characters might not form an actual word, but that doesn't matter. The word is a palindrome if there are at least three characters in the word and the characters read the same from right to left and from left to right. For example, the word level is a palindrome. Return True if the word is a palindrome, otherwise return False. Note: You are not allowed to use the reverse function or slices in this function. Write a function that begins with the following header: def findPalindromes(text) This function is given a list of strings named text. Most of the strings are words but some of them are not, but that doesn't matter. Create two lists named palindromes and frequencies. The list palindromes holds the unique palindromes from the list named text. The list frequencies holds the frequencies of the unique palindromes. For each word in text call isPalindrome to determine if the word is a palindrome. If the word is a palindrome and is not in the list of palindromes add the word to the list of palindromes and add 1 to the list of frequencies. If the word is a palindrome and is in the list of palindromes add 1 to the corresponding frequency in the list of frequencies. Return the list of unique palindromes followed by the list of the frequencies of the palindromes. Hint: The index function can be used to find the position of the palindrome in the list of palindromes. Write a function that begins with the following header: def displayPalindromes(palindromes, frequencies): This function is given the list of palindromes and the list of frequencies. Create a new list that holds the palindromes sorted in ascending order. Use the sorted function to do this. Display the heading as shown in the sample run of the program. Display the palindromes in ascending order along with the associated frequencies one pair of values per line as shown in the sample run of the program. The palindrome is left justified in 20 character positions. The frequency is in 9 character positions. This function does not return anything, Write a function that begins with the following header: def main This function does the following: prints a line of dashes using string replication calls input to get the name of the file containing the text loops as long as the name of the file is an empty string in the loop: o displays the error message as shown in the sample run of the program o calls input to get the name of the file containing the text calls getWords to get the list of words that make up the text calls findPalindromes to get the lists of palindromes and frequencies calls displayPalindromes to display the lists of palindromes and frequencies calls display Termination Message to display the termination message The main program, not to be confused with the function named main, contains all the import statements, the functions and a call to the function named main. Acknowledgment: The data in the file relativity.Ext is from the Project Gutenberg EBook of Relativity: The Special and General Theory, by Albert Einstein This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.net A sample run of the program is shown below. Enter the name of the file containing the text. The name of the file must not be an empty string! Enter the name of the file containing the text: relativity.txt Palindrome Frequency 3 did 5 refer 3 tenet 1 Programmed by Stew Dent. Date: Tue Feb 16 14:01:26 2021 End of processing Hand in your complete program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions

Question

=+Do you think your results are valid? Explain.

Answered: 1 week ago

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago