| 1 | 1 | def encode(plain_text: str, a: int, b: int) -> str: |
| 2 | pass | |
| 2 | m = 26 | |
| 3 | # Edge Case: Check if a and m are coprime | |
| 4 | if gcd(a, m) != 1: | |
| 5 | raise ValueError("a and m must be coprime.") | |
| 6 | ||
| 7 | result = [] | |
| 8 | for char in plain_text: | |
| 9 | if char.isalpha(): | |
| 10 | # Convert to lowercase | |
| 11 | char = char.lower() | |
| 12 | i = ord(char) - ord('a') | |
| 13 | encrypted_i = (a * i + b) % m | |
| 14 | result.append(chr(encrypted_i + ord('a'))) | |
| 15 | elif char.isdigit(): | |
| 16 | result.append(char) | |
| 17 | ||
| 18 | # Group into chunks of 5 | |
| 19 | grouped_result = [] | |
| 20 | for i in range(0, len(result), 5): | |
| 21 | grouped_result.append(''.join(result[i:i+5])) | |
| 22 | ||
| 23 | return ' '.join(grouped_result) | |
| 3 | 24 | |
| 4 | 25 | |
| 5 | 26 | def decode(ciphered_text: str, a: int, b: int) -> str: |
| 6 | pass | |
| 27 | m = 26 | |
| 28 | # Edge Case: Check if a and m are coprime | |
| 29 | if gcd(a, m) != 1: | |
| 30 | raise ValueError("a and m must be coprime.") | |
| 31 | ||
| 32 | # Find modular multiplicative inverse of a | |
| 33 | a_inv = None | |
| 34 | for x in range(1, m): | |
| 35 | if (a * x) % m == 1: | |
| 36 | a_inv = x | |
| 37 | break | |
| 38 | ||
| 39 | # Edge Case: MMI should exist as we checked a and m are coprime | |
| 40 | # But as a safety check: | |
| 41 | if a_inv is None: | |
| 42 | raise ValueError("Cannot find modular multiplicative inverse.") | |
| 43 | ||
| 44 | result = [] | |
| 45 | for char in ciphered_text: | |
| 46 | if char.isalpha(): | |
| 47 | char = char.lower() | |
| 48 | y = ord(char) - ord('a') | |
| 49 | decrypted_y = (a_inv * (y - b)) % m | |
| 50 | # Handle negative mod results | |
| 51 | decrypted_y = decrypted_y % m | |
| 52 | result.append(chr(decrypted_y + ord('a'))) | |
| 53 | elif char.isdigit(): | |
| 54 | result.append(char) | |
| 55 | # Ignore spaces and punctuation | |
| 56 | ||
| 57 | return ''.join(result) | |
| 58 | ||
| 59 | ||
| 60 | def gcd(x: int, y: int) -> int: | |
| 61 | while y: | |
| 62 | x, y = y, x % y | |
| 63 | return x | |
| 64 | ||
| 65 | # Handled Edge Cases: | |
| 66 | # 1. a and m not coprime in encode | |
| 67 | # 2. a and m not coprime in decode | |
| 68 | # 3. Non-alphabetic characters (digits preserved, others ignored) | |
| 69 | # 4. Case conversion (uppercase to lowercase) | |
| 70 | # 5. Grouping output in encode | |
| 71 | # 6. Handling spaces in decode input | |
| 72 | # 7. Negative mod results in decode |
| Test Name | Status |
|---|---|
test_decode_a_sentence | Pass |
test_decode_all_the_letters | Pass |
test_decode_exercism | Pass |
test_decode_numbers | Pass |
test_decode_with_a_not_coprime_to_m | Pass |
test_decode_with_no_spaces_in_input | Pass |
test_decode_with_too_many_spaces | Pass |
test_encode_all_the_letters | Pass |
test_encode_deep_thought | Pass |
test_encode_mindblowingly | Pass |
test_encode_no | Pass |
test_encode_numbers | Pass |
test_encode_o_m_g | Pass |
test_encode_omg | Pass |
test_encode_with_a_not_coprime_to_m | Pass |
test_encode_yes | Pass |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
