Winner

Finished
11def encode(plain_text: str, a: int, b: int) -> str:
2 pass
3
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 # Edge Case: Skip non-alphanumeric characters
13 if not char.isalnum():
14 continue
15
16 if char.isalpha():
17 # Edge Case: Encrypt alphabetic characters
18 i = ord(char) - ord('a')
19 encrypted_i = (a * i + b) % m
20 result.append(chr(encrypted_i + ord('a')))
21 else:
22 # Edge Case: Keep digits as they are
23 result.append(char)
24
25 # Edge Case: Group the result 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)
431
532def decode(ciphered_text: str, a: int, b: int) -> str:
6 pass
33 m = 26 # Size of the alphabet
34
35 # Edge Case: Check if a and m are coprime
36 if gcd(a, m) != 1:
37 raise ValueError("a and m must be coprime.")
38
39 # Edge Case: Calculate modular multiplicative inverse of a
40 a_inv = mod_inverse(a, m)
41
42 result = []
43 ciphered_text = ciphered_text.lower()
44
45 for char in ciphered_text:
46 # Edge Case: Skip non-alphanumeric characters and spaces
47 if not char.isalnum():
48 continue
49
50 if char.isalpha():
51 # Edge Case: Decrypt alphabetic characters
52 y = ord(char) - ord('a')
53 decrypted_y = (a_inv * (y - b)) % m
54 result.append(chr(decrypted_y + ord('a')))
55 else:
56 # Edge Case: Keep digits as they are
57 result.append(char)
58
59 return ''.join(result)
60
61# Helper function to calculate greatest common divisor
62def gcd(a: int, b: int) -> int:
63 while b:
64 a, b = b, a % b
65 return a
66
67# Helper function to calculate modular multiplicative inverse
68def mod_inverse(a: int, m: int) -> int:
69 # Edge Case: Find x such that (a * x) % m == 1
70 for x in range(1, m):
71 if (a * x) % m == 1:
72 return x
73 # This should never happen if a and m are coprime
74 raise ValueError("Modular multiplicative inverse does not 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.