apple

Finished
11def encode(plain_text: str, a: int, b: int) -> str:
2 pass
2 m = 26
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 for char in plain_text:
10 if char.isalpha():
11 # Convert to lowercase and then to index (0-25)
12 i = ord(char.lower()) - ord('a')
13 # Apply encryption formula
14 encrypted_index = (a * i + b) % m
15 # Convert back to character
16 result.append(chr(encrypted_index + ord('a')))
17 elif char.isdigit():
18 # Edge Case: Keep digits as they are
19 result.append(char)
20
21 # Group into chunks of 5 characters
22 grouped_result = []
23 for i in range(0, len(result), 5):
24 grouped_result.append(''.join(result[i:i+5]))
25
26 return ' '.join(grouped_result)
327
428
529def decode(ciphered_text: str, a: int, b: int) -> str:
6 pass
30 m = 26
31
32 # Edge Case: Check if a and m are coprime
33 if gcd(a, m) != 1:
34 raise ValueError("a and m must be coprime.")
35
36 # Find modular multiplicative inverse of a
37 a_inv = mod_inverse(a, m)
38
39 result = []
40 for char in ciphered_text:
41 if char.isalpha():
42 # Convert to index (0-25)
43 y = ord(char) - ord('a')
44 # Apply decryption formula
45 decrypted_index = (a_inv * (y - b)) % m
46 # Convert back to character
47 result.append(chr(decrypted_index + ord('a')))
48 elif char.isdigit():
49 # Edge Case: Keep digits as they are
50 result.append(char)
51 # Edge Case: Ignore spaces and punctuation
52 return ''.join(result)
53
54
55def gcd(a: int, b: int) -> int:
56 # Edge Case: Handle negative numbers by taking absolute values
57 a, b = abs(a), abs(b)
58 while b:
59 a, b = b, a % b
60 return a
61
62
63def mod_inverse(a: int, m: int) -> int:
64 # Edge Case: Handle negative a by converting to positive equivalent
65 a = a % m
66 for x in range(1, m):
67 if (a * x) % m == 1:
68 return x
69 # Edge Case: This should not happen if gcd(a, m) == 1, but included for completeness
70 raise ValueError("Modular inverse does not exist")
71# Handled Edge Cases: encode function handles non-coprime a and m, non-alphabetic characters, grouping; decode function handles non-coprime a and m, non-alphabetic characters, spaces and punctuation; gcd handles negative numbers; mod_inverse handles cases where inverse doesn't exist
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.