def validate(): """ 알고리즘 검증 : 기작성된 find_palindrome 이용 """ import find_palindromes word_list = load_dictionary.load_file("2of4brif.txt") for word in word_list: if find_palindromes.is_palindrome(word) != is_palindrome(word): print("Wrong") print("Correct")
def find_palingram_in_dictionary(): """ 성능 테스트를 위해 메인 코드 분리 """ word_list = load_dictionary.load_file("2of4brif.txt") palingram_list = [] for word in word_list: palingrams_wrt_word = find_palingram(word, word_list) palingram_list = palingram_list + palingrams_wrt_word return palingram_list
def find_palindromes(): """ 사전 파일에서 회문을 모두 찾아 출력 """ word_list = load_dictionary.load_file("2of4brif.txt") palindrome_list = [] for word in word_list: if is_palindrome(word): palindrome_list.append(word) print("\nNumber of palindromes found = {}\n".format(len(palindrome_list))) print(*palindrome_list, sep="\n")
"""This program uses a method called recursion to identify palindromes These are words which read the same forwards as backwards. """ from load_dictionary import load_file # Read in the dictionary file file_name = '6of12.txt' words = load_file(file_name) def find_palindromes(word_list): """Find palindromes in a list of words""" # Initialise empty list for palindromes palindromes = [] for word in word_list: new_word = word while True: length = len(new_word) # Words with 1 or 0 characters are palindromes if length == 0 or length == 1: # Append the original word instead of the chopped word palindromes.append(word) break # If first and last don't match then not palindrome if new_word[0] != new_word[length - 1]: # Not a palindrome break
"""Find and display all of the palingrams in a dictionary, a palingram is a phrase which reads the same way forwards and backwards such as 'nurses run'. """ from load_dictionary import load_file import time start_time = time.time() # Read the dictionary into a list file_name = '2of4brif.txt' word_list = load_file(file_name) def find_palingrams(): """Find dictionary palingrams.""" pali_list = [] words_list = set(word_list) for word in words_list: # Get the length of the word and reverse it end = len(word) reverse = word[::-1] # Only interested in words longer than one letter if end > 1: for i in range(end): # Check if a reveresed word fragment is connected to a # Palindromic sequence if word[i:] == reverse[:end-i] and reverse[end-i:] in words_list: pali_list.append((word, reverse[end-i:])) if word[:i] == reverse[end-i:] and reverse[:end-i] in words_list:
"""Find all the anagrams of a user inputted word and ouput them in a neatly formatted way. Example word: pots""" import load_dictionary # Load the dictionary into a list of words filename = '2of4brif.txt' words = load_dictionary.load_file(filename) # input a SINGLE word or SINGLE name below to find its anagram(s) user_word = input('Enter a word to be searched for anagrams: ') user_word = user_word.lower() print(f"Word for analysis: {user_word}") anagrams = [] # Loop through words in dictionary and determine if the word is an anagram for word in words: word = word.lower() # Omit the word if it is the same as one in the dictionary if word != user_word: if sorted(word) == sorted(user_word): anagrams.append(word) # print a list of the anagrams if len(anagrams) == 0: print('That word has no anagrams in the dictionary you have selected') else: print("Anagrams = ", *anagrams, sep='\n')
import sys from collections import Counter import load_dictionary dict_file = load_dictionary.load_file('2of4brif.txt') # Ensure "a" and "i" are included dict_file.append('a') dict_file.append('i') dict_file = sorted(dict_file) # Take users name ini_name = input("Enter a name: ") ini_name = ini_name.lower() def find_anagrams(name, word_list): """Read name and dictionary file and display all anagrams in name""" # Form a dictionary of the name in the form of "letter: number of letter" name_letter_map = Counter(name) anagrams = [] # Loop through every word in the dictionary for word in word_list: # test string holds all the letters in dictionary word available to use test = '' word_letter_map = Counter(word.lower()) for letter in word: # add letters to test if they fit into the name # and confirm if the it is the full dictionary word if word_letter_map[letter] <= name_letter_map[letter]: test += letter