2021-10-09 04:30:38 +00:00
|
|
|
def extract_letters(words: list[str]) -> set[str]:
|
|
|
|
"""Extracts unique letters from a list of words
|
2021-10-03 13:41:54 +00:00
|
|
|
|
|
|
|
:param words: list of leafs
|
|
|
|
:return: set of letters
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> leafs = ["a", "a*k"]
|
|
|
|
>>> extract_letters(leafs) == {'a', 'k'}
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
letters = set()
|
|
|
|
for word in words:
|
|
|
|
for c in word:
|
|
|
|
if c.isalpha():
|
|
|
|
letters.add(c)
|
|
|
|
return letters
|
|
|
|
|
|
|
|
|
2021-10-09 04:30:38 +00:00
|
|
|
def eval_words(words: list[str], values: dict[str, int]) -> dict[str, int]:
|
|
|
|
"""Evaluate words replacing letters with values
|
2021-10-03 13:41:54 +00:00
|
|
|
|
|
|
|
:param words: list of words
|
|
|
|
:param values: Dictionary of letters:value
|
|
|
|
:return: Dictionary of evaluated words from generated values
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> leafs = ["a", "a*k"]
|
|
|
|
>>> generated_values = {"a":2, "k":3}
|
|
|
|
>>> eval_words(leafs, generated_values)
|
|
|
|
{'a': 2, 'a*k': 6}
|
|
|
|
"""
|
|
|
|
return {word: eval(word, values) for word in words}
|