src.ch01.practice package

Submodules

src.ch01.practice.p1_pig_latin module

Takes a word as input and returns its Pig Latin equivalent.

src.ch01.practice.p1_pig_latin.encode(word: str) → str[source]

Check if word starts with vowel, then translate to Pig Latin.

If a word begins with a consonant, move the consonant to the end of the word and add ‘ay’ to the end of the new word. If a word begins with a vowel in VOWELS, add ‘way’ to the end of the word.

Parameters

word (str) – Word to encode to Pig Latin.

Returns

Encoded Pig Latin word.

Raises

TypeError – If word is not a string.

src.ch01.practice.p1_pig_latin.main()[source]

Demonstrate Pig Latin encoder.

src.ch01.practice.p2_poor_bar_chart module

Takes a sentence as input and returns a ‘bar chart’ of each letter.

src.ch01.practice.p2_poor_bar_chart.freq_analysis(sentence: str) → dict[source]

Perform frequency analysis of letters in sentence.

Iterate through each letter in the sentence and add it to a dictionary of lists using collections.defaultdict.

Parameters

sentence (str) – String to count letters of.

Returns

defaultdict with each letter as keys and a list with letters repeated based on their frequency as values.

Example

>>> from src.ch01.practice.p2_poor_bar_chart import freq_analysis
>>> test = 'aaabbbccc'
>>> freq_analysis(test)
defaultdict(<class 'list'>, {'a': ['a', 'a', 'a'],
                             'b': ['b', 'b', 'b'],
                             'c': ['c', 'c', 'c']})
Raises

TypeError – If sentence is not a string.

src.ch01.practice.p2_poor_bar_chart.main()[source]

Demonstrates the Poor Bar Chart.

src.ch01.practice.p2_poor_bar_chart.print_bar_chart(freq_dict: dict) → None[source]

Print dictionary to terminal.

Use pprint.pprint() to print dictionary with letter frequency analysis to terminal.

Parameters

freq_dict (dict) – Dictionary with frequency analysis from freq_analysis().

Returns

None. Prints freq_dict.

Raises

TypeError – If freq_dict is not a dictionary.

Module contents

Chapter 1 Practice Projects.

src.ch01.practice.VOWELS

Tuple containing characters of the English vowels (except for ‘y’)

Type

tuple

src.ch01.practice.ENCODE_ERROR

String with TypeError for Pig Latin encode().

Type

str

src.ch01.practice.FREQ_ANALYSIS_ERROR

String with TypeError for Poor Bar Chart freq_analysis().

Type

str

src.ch01.practice.PRINT_BAR_CHART_ERROR

String with TypeError for Poor Bar Chart print_bar_chart().

Type

str