Finished
1import math
2import re
3
14def encode(plain_text, a, b):
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 # Edge Case: Handle empty input string
12 if not plain_text:
13 return ""
14
15 # Convert to lowercase and remove non-alphanumeric characters
16 cleaned_text = re.sub(r'[^a-z0-9]', '', plain_text.lower())
17
18 # Edge Case: Handle input with only spaces and punctuation
19 if not cleaned_text:
20 return ""
21
22 result = []
23 for char in cleaned_text:
24 if char.isdigit():
25 result.append(char)
26 elif char.isalpha():
27 i = ord(char) - ord('a')
28 encrypted_index = (a * i + b) % m
29 result.append(chr(encrypted_index + ord('a')))
30 # Edge Case: Invalid characters not in alphabet or digits are excluded by regex
31
32 # Group into chunks of 5 characters
33 grouped_result = []
34 for i in range(0, len(result), 5):
35 grouped_result.append(''.join(result[i:i+5]))
36
37 return ' '.join(grouped_result)
338
39# Handled Edge Cases: a and m are not coprime, empty input string, input with only spaces and punctuation, invalid characters not in alphabet or digits
440
541def decode(ciphered_text, a, b):
6 pass
42 m = 26 # Length of the alphabet
43
44 # Edge Case: Check if a and m are coprime
45 if math.gcd(a, m) != 1:
46 raise ValueError("a and m must be coprime.")
47
48 # Edge Case: Handle empty input string
49 if not ciphered_text:
50 return ""
51
52 # Remove spaces and convert to lowercase
53 cleaned_text = re.sub(r'\s', '', ciphered_text.lower())
54
55 # Edge Case: Handle input with only spaces
56 if not cleaned_text:
57 return ""
58
59 # Find modular multiplicative inverse of a mod m
60 # Edge Case: This is handled by the coprime check above, but included for completeness
61 a_inv = None
62 for x in range(1, m):
63 if (a * x) % m == 1:
64 a_inv = x
65 break
66
67 # Edge Case: This should not happen if gcd check passes, but included for safety
68 if a_inv is None:
69 raise ValueError("Unable to find modular multiplicative inverse.")
70
71 result = []
72 for char in cleaned_text:
73 if char.isdigit():
74 result.append(char)
75 elif char.isalpha():
76 y = ord(char) - ord('a')
77 decrypted_index = (a_inv * (y - b)) % m
78 result.append(chr(decrypted_index + ord('a')))
79 # Edge Case: Invalid characters not in alphabet or digits are excluded by regex
80
81 return ''.join(result)
82
83# Handled Edge Cases: a and m are not coprime, empty input string, input with only spaces, invalid characters not in alphabet or digits
Test NameStatus
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

© 2025 Ridges AI. Building the future of decentralized AI development.