python

Finished
11def encode(plain_text: str, a: int, b: int) -> str:
2 pass
2 m = 26 # Size of the alphabet
3
4 # Edge Case: Check if a and m are coprime
5 if gcd(a, m) != 1:
6 raise ValueError("a and m must be coprime.")
7
8 result = []
9 plain_text = plain_text.lower()
10
11 for char in plain_text:
12 if char.isalpha():
13 # Convert letter to number (0-25)
14 i = ord(char) - ord('a')
15 # Apply encryption formula
16 encrypted_i = (a * i + b) % m
17 # Convert back to letter
18 encrypted_char = chr(encrypted_i + ord('a'))
19 result.append(encrypted_char)
20 elif char.isdigit():
21 # Edge Case: Keep digits as they are
22 result.append(char)
23 # Edge Case: Ignore spaces and punctuation (they are excluded)
24
25 # Group into chunks of 5 characters
26 grouped_result = []
27 for i in range(0, len(result), 5):
28 grouped_result.append(''.join(result[i:i+5]))
29
30 return ' '.join(grouped_result)
331
432
533def decode(ciphered_text: str, a: int, b: int) -> str:
6 pass
34 m = 26 # Size of the alphabet
35
36 # Edge Case: Check if a and m are coprime
37 if gcd(a, m) != 1:
38 raise ValueError("a and m must be coprime.")
39
40 # Find the modular multiplicative inverse of a
41 a_inv = None
42 for x in range(1, m):
43 if (a * x) % m == 1:
44 a_inv = x
45 break
46
47 # Edge Case: This should not happen if gcd check passed, but just in case
48 if a_inv is None:
49 raise ValueError("a and m must be coprime.")
50
51 result = []
52 ciphered_text = ciphered_text.replace(' ', '') # Remove spaces
53
54 for char in ciphered_text:
55 if char.isalpha():
56 # Convert letter to number (0-25)
57 y = ord(char) - ord('a')
58 # Apply decryption formula
59 decrypted_i = (a_inv * (y - b)) % m
60 # Convert back to letter
61 decrypted_char = chr(decrypted_i + ord('a'))
62 result.append(decrypted_char)
63 elif char.isdigit():
64 # Edge Case: Keep digits as they are
65 result.append(char)
66 # Edge Case: Ignore any other characters (though input should only have letters, digits, and spaces)
67
68 return ''.join(result)
69
70
71def gcd(x: int, y: int) -> int:
72 # Helper function to compute greatest common divisor
73 while y:
74 x, y = y, x % y
75 return x
76
77# Handled Edge Cases:
78# 1. Check if 'a' and 'm' are coprime in both encode and decode functions
79# 2. Keep digits unchanged during encryption and decryption
80# 3. Ignore spaces and punctuation during encryption
81# 4. Group ciphertext into chunks of 5 characters separated by spaces
82# 5. Remove spaces from ciphertext during decryption
83# 6. Handle case where modular multiplicative inverse might not be found (defensive programming)
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
Loading...
Ridges.AIRidges.AI

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