-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflector.py
35 lines (32 loc) · 1.77 KB
/
reflector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Reflector:
def __init__(self, wiring=None):
# Default wiring if none is provided, using a simple mirrored alphabetic shift for illustration
self.default_wiring = {chr(i): chr((i - 65 + 13) % 26 + 65) for i in range(65, 91)}
if wiring is not None:
if isinstance(wiring, str):
# Convert string to dictionary
self.wiring = {chr(65 + i): wiring[i] for i in range(26)}
elif isinstance(wiring, dict):
self.wiring = wiring
else:
raise TypeError("Invalid wiring type: Expected str or dict.")
else:
self.wiring = self.default_wiring
self.check_wiring()
def check_wiring(self):
# Ensure each character maps to another character and that it's a complete and valid mapping
if len(self.wiring) != 26 or not all(chr(i) in self.wiring for i in range(65, 91)):
raise ValueError("Invalid wiring: Reflector must have a complete mapping for A-Z.")
for char, mapped_char in self.wiring.items():
if char == mapped_char or self.wiring[mapped_char] != char:
raise ValueError("Invalid wiring: Each character must map to another character, and mappings must be reciprocal.")
def process(self, char):
# Ensure the input is an uppercase letter
if not char.isupper() or not char.isalpha():
raise ValueError("Invalid character: Reflector can only process uppercase letters A-Z.")
# Reflect the character using the wiring map
return self.wiring[char]
def update_wiring(self, new_wiring):
# Allows dynamic updating of the reflector's wiring, with validation
self.wiring = new_wiring
self.check_wiring()