MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _validator

Function _validator

ciphers/enigma_machine2.py:74–111  ·  view source on GitHub ↗

Checks if the values can be used for the ``enigma`` function >>> _validator((1,1,1), (rotor1, rotor2, rotor3), 'POLAND') ((1, 1, 1), ('EGZWVONAHDCLFQMSIPJBYUKXTR', 'FOBHMDKEXQNRAULPGSJVTYICZW', \ 'ZJXESIUQLHAVRMDOYGTNFWPBKC'), \ {'P': 'O', 'O': 'P', 'L': 'A', 'A': 'L', 'N': 'D', 'D': '

(
    rotpos: RotorPositionT, rotsel: RotorSelectionT, pb: str
)

Source from the content-addressed store, hash-verified

72
73
74def _validator(
75 rotpos: RotorPositionT, rotsel: RotorSelectionT, pb: str
76) -> tuple[RotorPositionT, RotorSelectionT, dict[str, str]]:
77 """
78 Checks if the values can be used for the ``enigma`` function
79
80 >>> _validator((1,1,1), (rotor1, rotor2, rotor3), 'POLAND')
81 ((1, 1, 1), ('EGZWVONAHDCLFQMSIPJBYUKXTR', 'FOBHMDKEXQNRAULPGSJVTYICZW', \
82'ZJXESIUQLHAVRMDOYGTNFWPBKC'), \
83{'P': 'O', 'O': 'P', 'L': 'A', 'A': 'L', 'N': 'D', 'D': 'N'})
84
85 :param rotpos: rotor_positon
86 :param rotsel: rotor_selection
87 :param pb: plugb -> validated and transformed
88 :return: (`rotpos`, `rotsel`, `pb`)
89 """
90 # Checks if there are 3 unique rotors
91
92 if (unique_rotsel := len(set(rotsel))) < 3:
93 msg = f"Please use 3 unique rotors (not {unique_rotsel})"
94 raise Exception(msg)
95
96 # Checks if rotor positions are valid
97 rotorpos1, rotorpos2, rotorpos3 = rotpos
98 if not 0 < rotorpos1 <= len(abc):
99 msg = f"First rotor position is not within range of 1..26 ({rotorpos1}"
100 raise ValueError(msg)
101 if not 0 < rotorpos2 <= len(abc):
102 msg = f"Second rotor position is not within range of 1..26 ({rotorpos2})"
103 raise ValueError(msg)
104 if not 0 < rotorpos3 <= len(abc):
105 msg = f"Third rotor position is not within range of 1..26 ({rotorpos3})"
106 raise ValueError(msg)
107
108 # Validates string and returns dict
109 pbdict = _plugboard(pb)
110
111 return rotpos, rotsel, pbdict
112
113
114def _plugboard(pbstring: str) -> dict[str, str]:

Callers 1

enigmaFunction · 0.85

Calls 1

_plugboardFunction · 0.85

Tested by

no test coverage detected