hangman

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 # Edge Case: Ignore spaces and punctuation
21
22 # Group into chunks of 5 characters
23 grouped_result = []
24 for i in range(0, len(result), 5):
25 grouped_result.append(''.join(result[i:i+5]))
26
27 return ' '.join(grouped_result)
328
429
530def decode(ciphered_text: str, a: int, b: int) -> str:
6 pass
31 m = 26
32
33 # Edge Case: Check if a and m are coprime
34 if gcd(a, m) != 1:
35 raise ValueError("a and m must be coprime.")
36
37 # Find modular multiplicative inverse of a
38 a_inv = mod_inverse(a, m)
39
40 result = []
41 for char in ciphered_text:
42 if char.isalpha():
43 # Convert to index (0-25)
44 y = ord(char) - ord('a')
45 # Apply decryption formula
46 decrypted_index = (a_inv * (y - b)) % m
47 # Convert back to character
48 result.append(chr(decrypted_index + ord('a')))
49 elif char.isdigit():
50 # Edge Case: Keep digits as they are
51 result.append(char)
52 # Edge Case: Ignore spaces and punctuation
53
54 return ''.join(result)
55
56
57def gcd(a: int, b: int) -> int:
58 # Edge Case: Handle negative numbers by taking absolute values
59 a, b = abs(a), abs(b)
60 while b:
61 a, b = b, a % b
62 return a
63
64
65def mod_inverse(a: int, m: int) -> int:
66 # Extended Euclidean Algorithm to find modular inverse
67 # Edge Case: Handle negative a
68 a = a % m
69 # Add iteration limit to prevent potential infinite loop
70 for x in range(1, m + 1):
71 if (a * x) % m == 1:
72 return x
73 # Edge Case: This should not happen if gcd(a, m) == 1
74 raise ValueError("Modular inverse does not exist")
75
76# Handled Edge Cases:
77# - Check if a and m are coprime in both encode and decode
78# - Ignore spaces and punctuation in both encode and decode
79# - Keep digits unchanged in both encode and decode
80# - Convert all letters to lowercase in encode
81# - Group encoded text into chunks of 5 characters separated by spaces
82# - Handle negative numbers in gcd function
83# - Handle negative a in mod_inverse function
84# - Raise ValueError when modular inverse does not exist (should not happen if gcd check passes)
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.