A helper function that generates the triagrams and assigns each letter of the alphabet to its corresponding triagram and stores this in a dictionary (`character_to_number` and `number_to_character`) after confirming if the alphabet's length is ``27``. >>> test = __prepare
(
message: str, alphabet: str
)
| 60 | |
| 61 | |
| 62 | def __prepare( |
| 63 | message: str, alphabet: str |
| 64 | ) -> tuple[str, str, dict[str, str], dict[str, str]]: |
| 65 | """ |
| 66 | A helper function that generates the triagrams and assigns each letter of the |
| 67 | alphabet to its corresponding triagram and stores this in a dictionary |
| 68 | (`character_to_number` and `number_to_character`) after confirming if the |
| 69 | alphabet's length is ``27``. |
| 70 | |
| 71 | >>> test = __prepare('I aM a BOy','abCdeFghijkLmnopqrStuVwxYZ+') |
| 72 | >>> expected = ('IAMABOY','ABCDEFGHIJKLMNOPQRSTUVWXYZ+', |
| 73 | ... TEST_CHARACTER_TO_NUMBER, TEST_NUMBER_TO_CHARACTER) |
| 74 | >>> test == expected |
| 75 | True |
| 76 | |
| 77 | Testing with incomplete alphabet |
| 78 | |
| 79 | >>> __prepare('I aM a BOy','abCdeFghijkLmnopqrStuVw') |
| 80 | Traceback (most recent call last): |
| 81 | ... |
| 82 | KeyError: 'Length of alphabet has to be 27.' |
| 83 | |
| 84 | Testing with extra long alphabets |
| 85 | |
| 86 | >>> __prepare('I aM a BOy','abCdeFghijkLmnopqrStuVwxyzzwwtyyujjgfd') |
| 87 | Traceback (most recent call last): |
| 88 | ... |
| 89 | KeyError: 'Length of alphabet has to be 27.' |
| 90 | |
| 91 | Testing with punctuation not in the given alphabet |
| 92 | |
| 93 | >>> __prepare('am i a boy?','abCdeFghijkLmnopqrStuVwxYZ+') |
| 94 | Traceback (most recent call last): |
| 95 | ... |
| 96 | ValueError: Each message character has to be included in alphabet! |
| 97 | |
| 98 | Testing with numbers |
| 99 | |
| 100 | >>> __prepare(500,'abCdeFghijkLmnopqrStuVwxYZ+') |
| 101 | Traceback (most recent call last): |
| 102 | ... |
| 103 | AttributeError: 'int' object has no attribute 'replace' |
| 104 | """ |
| 105 | # Validate message and alphabet, set to upper and remove spaces |
| 106 | alphabet = alphabet.replace(" ", "").upper() |
| 107 | message = message.replace(" ", "").upper() |
| 108 | |
| 109 | # Check length and characters |
| 110 | if len(alphabet) != 27: |
| 111 | raise KeyError("Length of alphabet has to be 27.") |
| 112 | if any(char not in alphabet for char in message): |
| 113 | raise ValueError("Each message character has to be included in alphabet!") |
| 114 | |
| 115 | # Generate dictionares |
| 116 | character_to_number = dict(zip(alphabet, TEST_CHARACTER_TO_NUMBER.values())) |
| 117 | number_to_character = { |
| 118 | number: letter for letter, number in character_to_number.items() |
| 119 | } |
no outgoing calls
no test coverage detected