src.ch03 package

Submodules

src.ch03.c1_anagram_generator module

Generate phrase anagrams from a word or phrase.

src.ch03.c1_anagram_generator.anagram_generator(word: str) → list[source]

Generate phrase anagrams.

Make phrase anagrams from a given word or phrase.

Parameters

word (str) – Word to get phrase anagrams of.

Returns

list of phrase anagrams of word.

src.ch03.c1_anagram_generator.extend_anagram_dict(word_list: list, dictionary: dict)[source]

Extend an anagram dictionary.

Adds words from given word list to a given anagram dictionary.

Parameters
  • word_list (list) – List of words to add to anagram dictionary.

  • dictionary (dict) – Anagram dictionary to add words to.

Returns

None. If words in word_list are in dictionary they are not added. Otherwise, they are added.

src.ch03.c1_anagram_generator.find_anagram_phrases(phrases: list, word: str, anagram_dict: dict, phrase: list) → None[source]

Find anagram phrases.

Recursively finds anagram phrases of word by removing unusable words from the anagram_dict, finding remaining anagrams given the phrase, then adding any found anagram phrases to phrases.

Parameters
  • phrases (list) – List of anagram phrases.

  • word (str) – Current word to find anagram phrases of.

  • anagram_dict (dict) – Current anagram dictionary to find anagrams with.

  • phrase (list) – Current anagram phrase candidate.

Returns

None. phrases is updated with any found anagram phrases.

src.ch03.c1_anagram_generator.find_anagrams(word: str, anagram_dict: dict) → list[source]

Find anagrams in word.

Find all anagrams in a given word (or phrase) using anagram dictionary.

Parameters
Returns

list of str with all anagrams in word.

src.ch03.c1_anagram_generator.get_anagram_dict(word_list: list) → dict[source]

Get an anagram dictionary from word_list.

Get the ID of each word in word list and add it to a dictionary with the ID as the key.

Parameters

word_list (list) – List of words to make into anagram dictionary.

Returns

defaultdict of list with an ID (int) as the key and words whose product of letters equal that ID as values.

src.ch03.c1_anagram_generator.get_id(word: str) → int[source]

Get ID number of word.

Assign a unique prime number to each letter in ascii_lowercase. The product of each letter in word is its ID number.

Parameters

word (str) – Word to get ID of.

Returns

int representing ID of word.

src.ch03.c1_anagram_generator.get_primes(length: int = 26, min_prime: int = 2, max_prime: int = 101) → list[source]

Get list of primes.

Given a length, minimum, and maximum prime number, return a list of prime numbers.

Parameters
  • length (int) – Number of prime numbers to return. Defaults to 26.

  • min_prime (int) – Smallest prime number to return. Defaults to 2.

  • max_prime (int) – Largest prime number to return. Defaults to 101.

Returns

list of length prime numbers with min_prime as the smallest prime number and max_prime as the largest prime number in the list.

src.ch03.c1_anagram_generator.main()[source]

Demonstrate the Anagram Generator.

src.ch03.c1_anagram_generator.multi_get_anagram_dict(word_list: list) → dict[source]

Multithreaded get anagram dictionary.

Uses os.cpu_count() and threading.Thread to use all CPUs to make an anagram dictionary with the intent of being more efficient than get_anagram_dict().

Parameters

word_list (list) – List of words to make into anagram dictionary.

Returns

defaultdict of list with an ID (int) as the key and words whose product of letters equal that ID as values.

Warning

Avoids race conditions by heavily relying on CPython’s Global Interpreter Lock. More info about Thread Objects.

src.ch03.c1_anagram_generator.remove_unusable_words(anagram_dict: dict, usable_letters: list) → dict[source]

Remove unusable words from anagram dictionary.

Creates new anagram dictionary by including only IDs that can be IN usable_letters.

Parameters
  • anagram_dict (dict) – Anagram dictionary to prune.

  • usable_letters (list) – List of letters that must be used.

Returns

defaultdict of list with an ID (int) as the key and words whose product of letters equal that ID as values.

src.ch03.c1_anagram_generator.split(a_list: list, parts: int) → list[source]

Split a list into parts.

Split given list into given number of parts.

Parameters
  • a_list (list) – List to split.

  • parts (int) – Number of parts to split list into.

Returns

List of lists with a_list split into parts.

Example

>>> import src.ch03.c1_anagram_generator.split as split
>>> some_list = ['this', 'is', 'a', 'list']
>>> split_list = split(some_list, 2)
>>> print(split_list)
[['this', 'is'], ['a', 'list']]

src.ch03.p1_digram_counter module

Counts the occurrence of all possible digrams of a word in a dictionary.

src.ch03.p1_digram_counter.count_digrams(digrams: set, dict_list: list) → dict[source]

Count digrams in word dictionary.

Count frequency of each digram in the set in a word dictionary list.

Parameters
  • digrams (set) – Set of digrams to count frequency of.

  • dict_list (list) – Word dictionary list.

Returns

Counter with digrams as keys and their counts as values.

Raises

TypeError – If digrams isn’t a set or if dict_list isn’t a list.

src.ch03.p1_digram_counter.digram_counter(word: str, dict_file: str = '/usr/share/dict/american-english') → dict[source]

Wrap get_digrams, count_digrams, and read_from_file.

Send word through get_digrams() to get a set of digrams which is then passed through count_digrams() along with the list made by passing dict_file through read_from_file().

Parameters
  • word (str) – Word to get digrams of.

  • dict_file (str) – Path of dictionary file to get a frequency analysis of each digram. Defaults to DICTIONARY_FILE_PATH.

Returns

Counter with digrams as keys and their counts as values.

src.ch03.p1_digram_counter.get_digrams(word: str) → set[source]

Get a set of digrams given a word.

Generate all possible digrams of a given word.

Parameters

word (str) – String to get digrams of.

Returns

set of all possible digrams of the given word.

Raises

TypeError – If word isn’t a string.

src.ch03.p1_digram_counter.main()[source]

Demonstrate the digram counter.

Module contents

Chapter 3.

src.ch03.GET_DIGRAMS_ERROR

String with TypeError for get_digrams().

Type

str

src.ch03.COUNT_DIGRAMS_ERROR

String with TypeError for count_digrams().

Type

str