| 60 | |
| 61 | |
| 62 | def generate_table(key: str) -> list[str]: |
| 63 | # I and J are used interchangeably to allow |
| 64 | # us to use a 5x5 table (25 letters) |
| 65 | alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ" |
| 66 | # we're using a list instead of a '2d' array because it makes the math |
| 67 | # for setting up the table and doing the actual encoding/decoding simpler |
| 68 | table = [] |
| 69 | |
| 70 | # copy key chars into the table if they are in `alphabet` ignoring duplicates |
| 71 | for char in key.upper(): |
| 72 | if char not in table and char in alphabet: |
| 73 | table.append(char) |
| 74 | |
| 75 | # fill the rest of the table in with the remaining alphabet chars |
| 76 | for char in alphabet: |
| 77 | if char not in table: |
| 78 | table.append(char) |
| 79 | |
| 80 | return table |
| 81 | |
| 82 | |
| 83 | def encode(plaintext: str, key: str) -> str: |