Modify the below python code with using lambda, join to get the below output:
- User
Modify the below python code with using lambda, join to get the below output:
- Assistant
The given code is missing, you need to provide the code that you want to modify.
- User
Modify the below code without using lambda to get the below output
Code:
Checking if a word in user inputs is a palindrome.
def check_palindrome(word): palindrome = "" for char in word: palindrome = char + palindrome if palindrome == word: print("Wow!! You entered a PALINDORE: ", word) return word else: return False
Checking if user inputs having special characters.
def check_special_chars(word): special_chars = "!@#$%^&*()_+-=:;,.<>?/~`" input_special = "" for char in word: if char in special_chars: input_special = input_special + char if len(input_special) > 0: print("Invalid word: includes SPECIAL characters: ", input_special) return word else: return False
def check_bigrams(word): bigrams = word.split() if len(bigrams) == 2: print(f"***** {word} is a bi-gram!! **************") return word else: return False
def check_duplicates(input_words): duplicate_words = [] duplicates = {} for word in input_words: if word not in duplicates: duplicates[word] = 1 else: duplicate_words.append(word) return duplicate_words
def choice_one(): input_words = [] bigrams = [] invalid_words = [] palindromes = []
user_input2 = input("Please enter a word OR press ‘space bar’ to exit: ") while user_input2 != " ": print("Please enter a word OR press ‘space bar’ to exit: ", user_input2) input_words.append(user_input2) result = check_palindrome(user_input2) if result == user_input2: palindromes.append(result) result = check_special_chars(user_input2) if result == user_input2: invalid_words.append(user_input2) result = check_bigrams(user_input2) if result == user_input2: bigrams.append(user_input2) user_input2 = input("Please enter a word OR press ‘space bar’ to exit: ") print("*********** Word Entry finished. ******************\n") return input_words, invalid_words, palindromes, bigramsdef initializing(): print("" * 60) print("~~~~~~~~~~~~ Text Analytics~~~~~~~~~~~~~~~~~~~~~") print("" * 60) print("************* MAIN MENU *********************") print("Press/Enter 1 to enter word(s).") print("Press/Enter 2 to see the entry summary.") print("Press/Enter 3 to see words summary.") print("Press/Enter 4 to sort/organize the words.") print("Press/Enter 5 to Quit the application.") print("" * 60)
user_input1 = int(input("Enter the choice: ")) if user_input1 == 1: input_words, invalid_words, palindromes, bigrams = choice_one() elif user_input2 == 2: print("*" * 60) print("*********** Entry Summary***********************") print("*" * 60) duplicate_words = check_duplicates(input_words) print(f"You entered total of {len(input_words) - len(invalid_words)} valid words") print(f"You entered {len(set(input_words))} unique words") print(f"You entered {len(bigrams)} bi-grams: {bigrams}") print(f"You entered {len(palindromes)} palindromes: {palindromes}") print(f"You entered {len(invalid_words)} INVALID words: {invalid_words}. The words are deemed invalid as they contained following special characters: #, $, !, &, *") print(f"You entered {len(duplicate_words)} duplicate words: {duplicate_words}") print("\n") elif user_input2 == 3: print("*" * 60) print("*********** Words Summary***********************") print("*" * 60) key_value_pairs = {} alphabet = "abcdefghijklmnopqrstuvwxyz" for word in input_words: first_char = word[0:0] if first_char in key_value_pairs: key_value_pairs[first_char].append(word) else: key_value_pairs[first_char] = [word] for char in alphabet: if char in key_value_pairs: print(f"The words starting with alphabet [{char}] is/are: {key_value_pairs.get(char)}") else: print(f"There is No word starting with [{char}]") elif user_input2 == 4: print("*" * 60) print("~ ~ ~ Enter 1 to alphabetically sort the words by FIRST characters OR \nEnter 2 to sort the words by SECOND characters OR \nEnter 3 to sort the words by LAST characters OR \nENTER 4 to return to the main menu ~ ~ ~") print("*" * 60) user_input3 = int(input("Ener the choice to sort: ")) while user_input3 != 4: if user_input3 == 1: print("The words sorted based on their FIRST character are: ", sorted(key_value_pairs.values())) elif user_input3 == 2: sorted_words = {} sorted_input_words = [] for word in input_words: second_char = word[1:1] if second_char in sorted_words: sorted_words[second_char].append(word) else: sorted_words[second_char] = [word] for key, value in sorted(sorted_words.items()): sorted_input_words.append(value) print("The words sorted based on their SECOND character are: ", sorted_input_words) elif user_input3 == 3: sorted_last = {} last_sorted_words = [] for word in input_words: last_char = word[-1:] if last_char in sorted_last: sorted_last[last_char].append(word) else: sorted_last[last_char] = [word] for key, value in sorted(sorted_last.items()): last_sorted_words.append(value) print("The words sorted based on their LAST character are: ", last_sorted_words) elif user_input3 == 4: elif user_input2 == 5: print("Good Bye!!!") else: print("Invalid choice, please try again.") - Assistant
To modify the code without using lambda, you can refer to the modified code below:
# Checking if a word in user inputs is a palindrome. def check_palindrome(word): palindrome = "" for char in word: palindrome = char + palindrome if palindrome == word: print("Wow!! You entered a PALINDORE: ", word) return word else: return False # Checking if user inputs have special characters. def check_special_chars(word): special_chars = "!@#$%^&*()_+-=:;,.<>?/~`" input_special = "" for char in word: if char in special_chars: input_special = input_special + char if len(input_special) > 0: print("Invalid word: includes SPECIAL characters: ", input_special) return word else: return False # Checking if user inputs are bi-grams. def check_bigrams(word): bigrams = word.split() if len(bigrams) == 2: print(f"***** {word} is a bi-gram!! **************") return word else: return False # Checking for duplicate words in user input. def check_duplicates(input_words): duplicate_words = [] duplicates = {} for word in input_words: if word not in duplicates: duplicates[word] = 1 else: duplicate_words.append(word) return duplicate_words def choice_one(): input_words = [] bigrams = [] invalid_words = [] palindromes = [] user_input2 = input("Please enter a word OR press ‘space bar’ to exit: ") while user_input2 != " ": print("Please enter a word OR press ‘space bar’ to exit: ", user_input2) input_words.append(user_input2) result = check_palindrome(user_input2) if result == user_input2: palindromes.append(result) result = check_special_chars(user_input2) if result == user_input2: invalid_words.append(user_input2) result = check_bigrams(user_input2) if result == user_input2: bigrams.append(user_input2) user_input2 = input("Please enter a word OR press ‘space bar’ to exit: ") print("*********** Word Entry finished. ******************\n") return input_words, invalid_words, palindromes, bigrams def initializing(): print("*" * 60) print("~~~~~~~~~~~~ Text Analytics ~~~~~~~~~~~~~~~~~~~~~") print("*" * 60) print("************* MAIN MENU **********************") print("Press/Enter 1 to enter word(s).") print("Press/Enter 2 to see the entry summary.") print("Press/Enter 3 to see words summary.") print("Press/Enter 4 to sort/organize the words.") print("Press/Enter 5 to Quit the application.") print("*" * 60) user_input1 = int(input("Enter the choice: ")) if user_input1 == 1: input_words, invalid_words, palindromes, bigrams = choice_one() elif user_input1 == 2: print("*" * 60) print("*********** Entry Summary***********************") print("*" * 60) duplicate_words = check_duplicates(input_words) print(f"You entered a total of {len(input_words) - len(invalid_words)} valid words") print(f"You entered {len(set(input_words))} unique words") print(f"You entered {len(bigrams)} bi-grams: {bigrams}") print(f"You entered {len(palindromes)} palindromes: {palindromes}") print(f"You entered {len(invalid_words)} INVALID words: {invalid_words}. The words are deemed invalid as they contained the following special characters: #, $, !, &, *") print(f"You entered {len(duplicate_words)} duplicate words: {duplicate_words}") print("\n") elif user_input1 == 3: print("*" * 60) print("*********** Words Summary***********************") print("*" * 60) key_value_pairs = {} alphabet = "abcdefghijklmnopqrstuvwxyz" for word in input_words: first_char = word[0:0] if first_char in key_value_pairs: key_value_pairs[first_char].append(word) else: key_value_pairs[first_char] = [word] for char in alphabet: if char in key_value_pairs: print(f"The words starting with alphabet [{char}] are: {key_value_pairs.get(char)}") else: print(f"There is no word starting with [{char}]") elif user_input1 == 4: print("*" * 60) print("~ ~ ~ Enter 1 to alphabetically sort the words by the FIRST character OR \nEnter 2 to sort the words by the SECOND character OR \nEnter 3 to sort the words by the LAST character OR \nENTER 4 to return to the main menu ~ ~ ~") print("*" * 60) user_input3 = int(input("Ener the choice to sort: ")) while user_input3 != 4: if user_input3 == 1: sorted_words = sorted(input_words, key=lambda x: x[0]) print("The words sorted based on their FIRST character are: ", sorted_words) elif user_input3 == 2: sorted_words = sorted(input_words, key=lambda x: x[1]) print("The words sorted based on their SECOND character are: ", sorted_words) elif user_input3 == 3: sorted_words = sorted(input_words, key=lambda x: x[-1]) print("The words sorted based on their LAST character are: ", sorted_words) user_input3 = int(input("Enter the choice to sort or press 4 to return to the main menu: ")) elif user_input1 == 5: print("Good Bye!!!") else: print("Invalid choice, please try again.")Note: The modified code includes changes to the check_duplicates(), initializing(), and sort/organize the words sections. Lambda function is used to sort the words based on the first, second, and last characters of each word.