>>> decrypt_message(6, 'Hlia rDsahrij') 'Harshil Darji'
(key: int, message: str)
| 37 | |
| 38 | |
| 39 | def decrypt_message(key: int, message: str) -> str: |
| 40 | """ |
| 41 | >>> decrypt_message(6, 'Hlia rDsahrij') |
| 42 | 'Harshil Darji' |
| 43 | """ |
| 44 | num_cols = math.ceil(len(message) / key) |
| 45 | num_rows = key |
| 46 | num_shaded_boxes = (num_cols * num_rows) - len(message) |
| 47 | plain_text = [""] * num_cols |
| 48 | col = 0 |
| 49 | row = 0 |
| 50 | |
| 51 | for symbol in message: |
| 52 | plain_text[col] += symbol |
| 53 | col += 1 |
| 54 | |
| 55 | if (col == num_cols) or ( |
| 56 | (col == num_cols - 1) and (row >= num_rows - num_shaded_boxes) |
| 57 | ): |
| 58 | col = 0 |
| 59 | row += 1 |
| 60 | |
| 61 | return "".join(plain_text) |
| 62 | |
| 63 | |
| 64 | if __name__ == "__main__": |