In the JetBrains Mono1 font, letter E is written with 4 straight lines, letter R with 2 straight and 1 curved line, letter G with 1 straight and 1 curved line, and letter O with 1 curved line. In that font, all letters of the word ERGONOMICS are written with 17 straight and 6 curved lines.
In the JetBrains Mono font, all letters of the word SUDOKU are written with 5 straight lines and 5 curved lines.
A font description is stored in a text file. Each line of the file contains three space-separated fields: i) a single character, ii) the number of straight lines in the character and iii) the number of curved lines in the character. Each character appears at most once in the file.
For example, this file describes all uppercase letters of the JetBrains Mono font (font.txt2):
A 3 0 B 1 2 C 0 1 D 1 1 E 4 0 … X 2 0 Y 3 0 Z 3 0
Your task:
Write a function read_font that takes the location (str) of a text file containing a font description. The function must return a dictionary (dict) that maps each character (str) in the file onto a tuple with the number of straight lines (int) and the number of curved lines (int) in the character.
Write a function count_straight_lines that takes two arguments: i) a word $$w$$ (str) and ii) a font description $$\mathcal{F}$$ (dict). The function must return the number (int) of straight lines used in writing the word $$w$$ in font $$\mathcal{F}$$.
Write a function count_curved_lines that takes two arguments: i) a word $$w$$ (str) and ii) a font description $$\mathcal{F}$$ (dict). The function must return the number (int) of curved lines used in writing the word $$w$$ in font $$\mathcal{F}$$.
Write a function straight_lines_only that takes two arguments: i) the location (str) of a text file containing a word list (one word per line) and ii) a font description $$\mathcal{F}$$ (dict). The function must return a set containing all words (str) in the given list that are only written with straight lines in font $$\mathcal{F}$$.
Write a function curved_lines_only that takes two arguments: i) the location (str) of a text file containing a word list (one word per line) and ii) a font description $$\mathcal{F}$$ (dict). The function must return a set containing all words (str) in the given list that are only written with curved lines in font $$\mathcal{F}$$.
Write a function balanced_lines that takes two arguments: i) the location (str) of a text file containing a word list (one word per line) and ii) a font description $$\mathcal{F}$$ (dict). The function must return a set containing all words (str) in the given list that are written with and equal number of straight and curved lines in font $$\mathcal{F}$$.
A dictionary $$\mathcal{F}$$ (dict) that is passed as a second argument to the functions count_straight_lines, count_curved_lines, straight_lines_only, curved_lines_only and balanced_lines must be formatted as the dictionaries returned by the function read_font. In counting lines, these function must only take into account characters in word $$w$$ that appear as keys in dictionary $$\mathcal{F}$$. Other characters of word $$w$$ must be ignored. These functions must also make a distinction between uppercase and lowercase letters.
In the following interactive session we assume the text files font.txt3 and dictionary.txt4 to be located in the current directory.
>>> font = read_font('font.txt5')
>>> font['E']
(4, 0)
>>> font['R']
(2, 1)
>>> font['G']
(1, 1)
>>> font['O']
(0, 1)
>>> count_straight_lines('ERGONOMICS', font)
17
>>> count_curved_lines('ERGONOMICS', font)
6
>>> straight_lines_only('dictionary.txt6', font)
{'ALKALINE', 'MILLENNIAL', 'INFINITELY'}
>>> curved_lines_only('dictionary.txt7', font)
{'COCCUS', 'COUSCOUS'}
>>> balanced_lines('dictionary.txt8', font)
{'AUGUSTUS', 'CROSSROAD', 'SUDOKU', 'COLUMBUS', 'PROCESSORS'}