Prepare the plaintext by up-casing it and separating repeated letters with X's
(dirty: str)
| 34 | |
| 35 | |
| 36 | def prepare_input(dirty: str) -> str: |
| 37 | """ |
| 38 | Prepare the plaintext by up-casing it |
| 39 | and separating repeated letters with X's |
| 40 | """ |
| 41 | |
| 42 | dirty = "".join([c.upper() for c in dirty if c in string.ascii_letters]) |
| 43 | clean = "" |
| 44 | |
| 45 | if len(dirty) < 2: |
| 46 | return dirty |
| 47 | |
| 48 | for i in range(len(dirty) - 1): |
| 49 | clean += dirty[i] |
| 50 | |
| 51 | if dirty[i] == dirty[i + 1]: |
| 52 | clean += "X" |
| 53 | |
| 54 | clean += dirty[-1] |
| 55 | |
| 56 | if len(clean) & 1: |
| 57 | clean += "X" |
| 58 | |
| 59 | return clean |
| 60 | |
| 61 | |
| 62 | def generate_table(key: str) -> list[str]: |