| 1 | 1 | def encode(message: str, rails: int) -> str: |
| 2 | | pass |
| 2 | # Edge Case: zero or negative number of rails |
| 3 | if rails <= 0: |
| 4 | return "" |
| 5 | |
| 6 | # Edge Case: single rail |
| 7 | if rails == 1: |
| 8 | return message.replace(" ", "") |
| 9 | |
| 10 | # Edge Case: empty message |
| 11 | if not message: |
| 12 | return "" |
| 13 | |
| 14 | # Remove spaces from message |
| 15 | message = message.replace(" ", "") |
| 16 | |
| 17 | # Edge Case: empty message after removing spaces |
| 18 | if not message: |
| 19 | return "" |
| 20 | |
| 21 | # Create rails as list of lists |
| 22 | fence = [[] for _ in range(rails)] |
| 23 | |
| 24 | # Track current rail and direction |
| 25 | rail = 0 |
| 26 | direction = 1 |
| 27 | |
| 28 | # Place each character in the appropriate rail |
| 29 | for char in message: |
| 30 | fence[rail].append(char) |
| 31 | |
| 32 | # Change direction at top and bottom rails |
| 33 | if rail == 0: |
| 34 | direction = 1 |
| 35 | elif rail == rails - 1: |
| 36 | direction = -1 |
| 37 | |
| 38 | rail += direction |
| 39 | |
| 40 | # Concatenate all rails |
| 41 | return ''.join(''.join(row) for row in fence) |
| 42 | # Handled Edge Cases: zero or negative number of rails, single rail, empty message |
| 3 | 43 | |
| 4 | 44 | |
| 5 | 45 | def decode(encoded_message: str, rails: int) -> str: |
| 6 | | pass |
| 46 | # Edge Case: zero or negative number of rails |
| 47 | if rails <= 0: |
| 48 | return "" |
| 49 | |
| 50 | # Edge Case: single rail |
| 51 | if rails == 1: |
| 52 | return encoded_message.replace(" ", "") |
| 53 | |
| 54 | # Edge Case: empty encoded message |
| 55 | if not encoded_message: |
| 56 | return "" |
| 57 | |
| 58 | # Remove spaces from encoded message |
| 59 | encoded_message = encoded_message.replace(" ", "") |
| 60 | |
| 61 | # Edge Case: empty encoded message after removing spaces |
| 62 | if not encoded_message: |
| 63 | return "" |
| 64 | |
| 65 | # Edge Case: number of rails greater than message length |
| 66 | if rails > len(encoded_message): |
| 67 | return encoded_message |
| 68 | |
| 69 | # Create a matrix to mark the rail pattern |
| 70 | fence = [[None] * len(encoded_message) for _ in range(rails)] |
| 71 | |
| 72 | # Track current rail and direction |
| 73 | rail = 0 |
| 74 | direction = 1 |
| 75 | |
| 76 | # Mark the positions in the zigzag pattern |
| 77 | for i in range(len(encoded_message)): |
| 78 | fence[rail][i] = '*' |
| 79 | |
| 80 | # Change direction at top and bottom rails |
| 81 | if rail == 0: |
| 82 | direction = 1 |
| 83 | elif rail == rails - 1: |
| 84 | direction = -1 |
| 85 | |
| 86 | rail += direction |
| 87 | |
| 88 | # Fill the matrix with characters from the encoded message |
| 89 | index = 0 |
| 90 | for r in range(rails): |
| 91 | for c in range(len(encoded_message)): |
| 92 | if fence[r][c] == '*': |
| 93 | fence[r][c] = encoded_message[index] |
| 94 | index += 1 |
| 95 | |
| 96 | # Read the message following the zigzag pattern |
| 97 | result = [] |
| 98 | rail = 0 |
| 99 | direction = 1 |
| 100 | |
| 101 | for i in range(len(encoded_message)): |
| 102 | result.append(fence[rail][i]) |
| 103 | |
| 104 | # Change direction at top and bottom rails |
| 105 | if rail == 0: |
| 106 | direction = 1 |
| 107 | elif rail == rails - 1: |
| 108 | direction = -1 |
| 109 | |
| 110 | rail += direction |
| 111 | |
| 112 | return ''.join(result) |
| 113 | # Handled Edge Cases: zero or negative number of rails, single rail, empty encoded message, number of rails greater than message length |