| 1 | import math | |
| 2 | import re | |
| 3 | ||
| 1 | 4 | def encode(plain_text: str, a: int, b: int) -> str: |
| 2 | pass | |
| 5 | m = 26 # length of the alphabet | |
| 6 | ||
| 7 | # Edge Case: Check if a and m are coprime | |
| 8 | if math.gcd(a, m) != 1: | |
| 9 | raise ValueError("a and m must be coprime.") | |
| 10 | ||
| 11 | # Normalize the plain text: convert to lowercase and remove non-alphanumeric | |
| 12 | normalized = re.sub(r'[^a-z0-9]', '', plain_text.lower()) | |
| 13 | ||
| 14 | result = [] | |
| 15 | for char in normalized: | |
| 16 | if char.isdigit(): | |
| 17 | result.append(char) | |
| 18 | else: | |
| 19 | # Calculate E(x) = (ai + b) mod m | |
| 20 | i = ord(char) - ord('a') | |
| 21 | encrypted_index = (a * i + b) % m | |
| 22 | result.append(chr(encrypted_index + ord('a'))) | |
| 23 | ||
| 24 | # Group into chunks of 5 characters | |
| 25 | grouped_result = [] | |
| 26 | for i in range(0, len(result), 5): | |
| 27 | grouped_result.append(''.join(result[i:i+5])) | |
| 28 | ||
| 29 | return ' '.join(grouped_result) | |
| 30 | # Handled Edge Cases: a and m not coprime, non-alphanumeric characters, digit handling, grouping | |
| 3 | 31 | |
| 4 | 32 | |
| 5 | 33 | def decode(ciphered_text: str, a: int, b: int) -> str: |
| 6 | pass | |
| 34 | m = 26 # length of the alphabet | |
| 35 | ||
| 36 | # Edge Case: Check if a and m are coprime | |
| 37 | if math.gcd(a, m) != 1: | |
| 38 | raise ValueError("a and m must be coprime.") | |
| 39 | ||
| 40 | # Find the modular multiplicative inverse of a mod m | |
| 41 | # Edge Case: MMI might not exist if a and m are not coprime (already checked above) | |
| 42 | a_inv = None | |
| 43 | for x in range(1, m): | |
| 44 | if (a * x) % m == 1: | |
| 45 | a_inv = x | |
| 46 | break | |
| 47 | ||
| 48 | # Edge Case: This should not happen due to the coprime check, but added for safety | |
| 49 | if a_inv is None: | |
| 50 | raise ValueError("a and m must be coprime.") | |
| 51 | ||
| 52 | # Remove spaces from ciphered text | |
| 53 | ciphered_text = ciphered_text.replace(' ', '') | |
| 54 | ||
| 55 | result = [] | |
| 56 | for char in ciphered_text: | |
| 57 | if char.isdigit(): | |
| 58 | result.append(char) | |
| 59 | else: | |
| 60 | # Calculate D(y) = a_inv * (y - b) mod m | |
| 61 | y = ord(char) - ord('a') | |
| 62 | decrypted_index = (a_inv * (y - b)) % m | |
| 63 | result.append(chr(decrypted_index + ord('a'))) | |
| 64 | ||
| 65 | return ''.join(result) | |
| 66 | # Handled Edge Cases: a and m not coprime, space removal, digit handling, MMI calculation |
| 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.
