Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python. Implement a program to identify palindromes and word squares. You will implement a program to identify palindromes and word squares. Recall that a palindrome
Python. Implement a program to identify palindromes and word squares.
You will implement a program to identify palindromes and word squares. Recall that a palindrome is a phrase that ignoring casing and non-letter characters is spelled the same forwards as backwards. An example of a palindrome is the title of this lab. A word square of order N consists of N words each of ength N written in a square grid (one word per line), such that the first word can also be read using the first letters of each word, and the second word can be read using the second letters of each word, etc. For example, here is a word square of order 4 THEN HERA ERRS NASA Functions You will write the following functions: 1 def print_menu(prints the main menu to the screen. (a) Function behavior: i. Prints a menu with three options: "Check a palindrome"; "Check a word square and "Quit" (b) Restrictions: i. Do not use input to read a menu choice from the user 2. def get menu_choice() (a) Function behavion i. Read the user's selected menu option using input(), and validate it From now on, validate always implies a loop that repeatedly asks for input until the input is valid. i. The validated selected option is returned. (b) Restrictions i. No other functions are called from get menu_choice. 3. def get phrase): (a) Function behavior: i. Ask the user to input an English phrase as a string li. Validate that at least one character is entered. i Return the validated phrase (b) Restrictions: i. . No other functions are called from get phrase. 4.def is_palindrome(phrase): given a string named phrase, this function returns True if the string is a palindrome, and False otherwise. (a) Function behavior: i. Given phrase, you must determine if the string is identical forwards and backwards, if we ignore upperlowercase and any non-letter characters like spaces and punctuation. i. Adapt the loop from lecture, which assumes that the entered string is lower- case and does not contain non-letter characters. i In each iteration of the loop you are adapting, you will need to write two more loops. The first loop should increment i as long as index i in phrase is not a letter; we can determine if a single character is a letter by taking that letter and calling isalpha0 on it, which will be True only if the character is a letter The second loop should decrement (decrease) j as long as index j in phrase is not a letter iv. v. Once the two loops have finished, you know that index i is a letter and index j is a letter. You can now compare them and perhaps make a decision, or perhaps not. vi Once you are sure the string is not a palindrome, you should return False. Once you are sure the string is a palindrome, return True (b) Restrictions: i. You cannot print or call input from this function. i. You cannot use any function that removes or replaces characters from a string We haven't learned these yet; do not use solutions from your friends or the You cannot remove or replace characters from the phrase by hand, without iv. You cannot use any function that reverses a string for you, including using [-1] v. You cannot reverse the string without using a function. Web, because they probably break this rule using a function. like you might find on the Internet. vi In general, you can only make one passthrough the string. This means that once you examine a particular character in the string, you can never examine that character again. All the restrictions above would violate this rule if ignored. Example: if we wrote a loop to go through the phrase and remove any non- Example: if we wrote a loop to go through the phrase and remove any non- letter character, and then go through the modified phrase to see if it is a palindrome, we would be making two passes through the string... so we can't do that. vii. You should not assume that phrase is all lowercase. You can use the lower) function to create a copy of phrase that is converted to all lowercase. 5. def menu_check_palindrome(): this function implements Menu Option 1, "check a phrase to see if it is a palindrome." (a) Function behavior i. Call get_phrase to read the user's selected phrase. i. is_palindrome with the phrase you saved from get phrase, and save the resulting Boolean value. i. Use the Boolean value from is palindrome to print either a. "X is a palindome!"; or b. "X is not a palindrome iv. where X is the phrase entered by the user (b) Restrictions: i. Do not use input in this function. i. Do not perform any of the logic to detect a palindrome in this function. i. Do not convert the phrase to lowercase or remove any characters from it. iv. When you print the final response message, the Xmust appear exactly as the user entered it. 6. def get_word_squarel): asks the user to input the words of a word square, one per line. (a) Function behavion: Ask the user to input the first line of a word square. From this input, you carn calculate the order of the square, which is equal to the length of the first line of the square Then repeatedly ask the user to input the next line(s) of the word square. You must repeat this until you have read in a total number of lines equal to the order of the square. ii. i Each line of the square should be concatenated into a single string variable. For example, the order-4 square from the assignment's introduction would be concatenated into the string THENHERAERRSNASA. iv. See the Example Output to understand how this function behaves. v Return the concatenated string. (b) Restrictions: i You cannot use lists or dictionaries in this function. (We haven't covered these yet.) i. No other functions are called from get_word_square. ili Do not assume the order of the square; you must allow squares of any size to be entered. 7. def check word square(square): given a concatenated string representing a potential word square, this function determines if the string actually is a word square. (a) Function behavior i. Determine the order of the square based on the length of the concatenated string. For example, note that our order-4 example square was concatenated into a string of length 16. What length would we get from an order-3 square? li. Suppose that the order of the square is N. We must identify the first word of the square (which is the first N characters) using a slice, and see if that is equal to a string formed by concatenating the first letter of each of the N words in the square. We then repeat that process for each word in the square. Note the following pattern: The first letter of the first word is at index 0. The first letter of the second word is at index N. The first letter of the third word is at index 2N. The second letter of the first word is at index 1. The second letter of the second word is at index N+1. The second letter of the second word is at index 2N +1 In other words, the pth letter of the mth word is at index... what? (In terms of p and m and N). a. b. c. ii. You will return True if and only if the given square is a word square, and False otherwise (b) Restrictions . You cannot print or call input from this function. 8. def menu_check_word_square(): this function implements Menu Option 2, "Check a word square. (a) Function behavior: i. Call get_word_square to read the user's (potential) word square string i is_word_square with the string you saved from the previous step, and save the resulting Boolean value. i. Use the Boolean value from the previous step to print either: is a word square!": or b. "X is not a word square where X is the string entered by the user. Each word of the square must be printed on its own line. i. Same as menu_check_palindrome. 9. def main(): implement the main of this program. (a) Function behavion: i. Display the menu. ii Read the user's menu choice. ii Execute the function corresponding to the menu choice. iv. Repeat until option 3 (Quit) is selected - including printing the menu on each loop (b) Restrictions: i. main() cannot call main(), and neither can any of your other functions. Use a loop. ii. You cannot print or call input from this function. You will implement a program to identify palindromes and word squares. Recall that a palindrome is a phrase that ignoring casing and non-letter characters is spelled the same forwards as backwards. An example of a palindrome is the title of this lab. A word square of order N consists of N words each of ength N written in a square grid (one word per line), such that the first word can also be read using the first letters of each word, and the second word can be read using the second letters of each word, etc. For example, here is a word square of order 4 THEN HERA ERRS NASA Functions You will write the following functions: 1 def print_menu(prints the main menu to the screen. (a) Function behavior: i. Prints a menu with three options: "Check a palindrome"; "Check a word square and "Quit" (b) Restrictions: i. Do not use input to read a menu choice from the user 2. def get menu_choice() (a) Function behavion i. Read the user's selected menu option using input(), and validate it From now on, validate always implies a loop that repeatedly asks for input until the input is valid. i. The validated selected option is returned. (b) Restrictions i. No other functions are called from get menu_choice. 3. def get phrase): (a) Function behavior: i. Ask the user to input an English phrase as a string li. Validate that at least one character is entered. i Return the validated phrase (b) Restrictions: i. . No other functions are called from get phrase. 4.def is_palindrome(phrase): given a string named phrase, this function returns True if the string is a palindrome, and False otherwise. (a) Function behavior: i. Given phrase, you must determine if the string is identical forwards and backwards, if we ignore upperlowercase and any non-letter characters like spaces and punctuation. i. Adapt the loop from lecture, which assumes that the entered string is lower- case and does not contain non-letter characters. i In each iteration of the loop you are adapting, you will need to write two more loops. The first loop should increment i as long as index i in phrase is not a letter; we can determine if a single character is a letter by taking that letter and calling isalpha0 on it, which will be True only if the character is a letter The second loop should decrement (decrease) j as long as index j in phrase is not a letter iv. v. Once the two loops have finished, you know that index i is a letter and index j is a letter. You can now compare them and perhaps make a decision, or perhaps not. vi Once you are sure the string is not a palindrome, you should return False. Once you are sure the string is a palindrome, return True (b) Restrictions: i. You cannot print or call input from this function. i. You cannot use any function that removes or replaces characters from a string We haven't learned these yet; do not use solutions from your friends or the You cannot remove or replace characters from the phrase by hand, without iv. You cannot use any function that reverses a string for you, including using [-1] v. You cannot reverse the string without using a function. Web, because they probably break this rule using a function. like you might find on the Internet. vi In general, you can only make one passthrough the string. This means that once you examine a particular character in the string, you can never examine that character again. All the restrictions above would violate this rule if ignored. Example: if we wrote a loop to go through the phrase and remove any non- Example: if we wrote a loop to go through the phrase and remove any non- letter character, and then go through the modified phrase to see if it is a palindrome, we would be making two passes through the string... so we can't do that. vii. You should not assume that phrase is all lowercase. You can use the lower) function to create a copy of phrase that is converted to all lowercase. 5. def menu_check_palindrome(): this function implements Menu Option 1, "check a phrase to see if it is a palindrome." (a) Function behavior i. Call get_phrase to read the user's selected phrase. i. is_palindrome with the phrase you saved from get phrase, and save the resulting Boolean value. i. Use the Boolean value from is palindrome to print either a. "X is a palindome!"; or b. "X is not a palindrome iv. where X is the phrase entered by the user (b) Restrictions: i. Do not use input in this function. i. Do not perform any of the logic to detect a palindrome in this function. i. Do not convert the phrase to lowercase or remove any characters from it. iv. When you print the final response message, the Xmust appear exactly as the user entered it. 6. def get_word_squarel): asks the user to input the words of a word square, one per line. (a) Function behavion: Ask the user to input the first line of a word square. From this input, you carn calculate the order of the square, which is equal to the length of the first line of the square Then repeatedly ask the user to input the next line(s) of the word square. You must repeat this until you have read in a total number of lines equal to the order of the square. ii. i Each line of the square should be concatenated into a single string variable. For example, the order-4 square from the assignment's introduction would be concatenated into the string THENHERAERRSNASA. iv. See the Example Output to understand how this function behaves. v Return the concatenated string. (b) Restrictions: i You cannot use lists or dictionaries in this function. (We haven't covered these yet.) i. No other functions are called from get_word_square. ili Do not assume the order of the square; you must allow squares of any size to be entered. 7. def check word square(square): given a concatenated string representing a potential word square, this function determines if the string actually is a word square. (a) Function behavior i. Determine the order of the square based on the length of the concatenated string. For example, note that our order-4 example square was concatenated into a string of length 16. What length would we get from an order-3 square? li. Suppose that the order of the square is N. We must identify the first word of the square (which is the first N characters) using a slice, and see if that is equal to a string formed by concatenating the first letter of each of the N words in the square. We then repeat that process for each word in the square. Note the following pattern: The first letter of the first word is at index 0. The first letter of the second word is at index N. The first letter of the third word is at index 2N. The second letter of the first word is at index 1. The second letter of the second word is at index N+1. The second letter of the second word is at index 2N +1 In other words, the pth letter of the mth word is at index... what? (In terms of p and m and N). a. b. c. ii. You will return True if and only if the given square is a word square, and False otherwise (b) Restrictions . You cannot print or call input from this function. 8. def menu_check_word_square(): this function implements Menu Option 2, "Check a word square. (a) Function behavior: i. Call get_word_square to read the user's (potential) word square string i is_word_square with the string you saved from the previous step, and save the resulting Boolean value. i. Use the Boolean value from the previous step to print either: is a word square!": or b. "X is not a word square where X is the string entered by the user. Each word of the square must be printed on its own line. i. Same as menu_check_palindrome. 9. def main(): implement the main of this program. (a) Function behavion: i. Display the menu. ii Read the user's menu choice. ii Execute the function corresponding to the menu choice. iv. Repeat until option 3 (Quit) is selected - including printing the menu on each loop (b) Restrictions: i. main() cannot call main(), and neither can any of your other functions. Use a loop. ii. You cannot print or call input from this functionStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started