



from __future__ import annotations

UpperCAmelCase__     = tuple[int, int, int]
UpperCAmelCase__     = tuple[str, str, str]


# used alphabet --------------------------
# from string.ascii_uppercase
UpperCAmelCase__     = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

# -------------------------- default selection --------------------------
# rotors --------------------------
UpperCAmelCase__     = "EGZWVONAHDCLFQMSIPJBYUKXTR"
UpperCAmelCase__     = "FOBHMDKEXQNRAULPGSJVTYICZW"
UpperCAmelCase__     = "ZJXESIUQLHAVRMDOYGTNFWPBKC"
# reflector --------------------------
UpperCAmelCase__     = {
    "A": "N",
    "N": "A",
    "B": "O",
    "O": "B",
    "C": "P",
    "P": "C",
    "D": "Q",
    "Q": "D",
    "E": "R",
    "R": "E",
    "F": "S",
    "S": "F",
    "G": "T",
    "T": "G",
    "H": "U",
    "U": "H",
    "I": "V",
    "V": "I",
    "J": "W",
    "W": "J",
    "K": "X",
    "X": "K",
    "L": "Y",
    "Y": "L",
    "M": "Z",
    "Z": "M",
}

# -------------------------- extra rotors --------------------------
UpperCAmelCase__     = "RMDJXFUWGISLHVTCQNKYPBEZOA"
UpperCAmelCase__     = "SGLCPQWZHKXAREONTFBVIYJUDM"
UpperCAmelCase__     = "HVSICLTYKQUBXDWAJZOMFGPREN"
UpperCAmelCase__     = "RZWQHFMVDBKICJLNTUXAGYPSOE"
UpperCAmelCase__     = "LFKIJODBEGAMQPXVUHYSTCZRWN"
UpperCAmelCase__     = "KOAEGVDHXPQZMLFTYWJNBRCIUS"





def 							A			(  _UpperCAmelCase       :     RotorPositionT							,     _UpperCAmelCase       :     RotorSelectionT							,     _UpperCAmelCase       :     str					)       ->				tuple[RotorPositionT, RotorSelectionT, dict[str, str]]:







			'''simple docstring'''
			# Checks if there are 3 unique rotors

			if (unique_rotsel := len(set(_UpperCAmelCase					)					)) < 3:
						_UpperCAmelCase	 =     F"Please use 3 unique rotors (not {unique_rotsel})"
						raise Exception(_UpperCAmelCase					)

			# Checks if rotor positions are valid
			_UpperCAmelCase ,  _UpperCAmelCase ,  _UpperCAmelCase	 =     rotpos
			if not 0 < rotorposa <= len(_UpperCAmelCase					):
						_UpperCAmelCase	 =     F"First rotor position is not within range of 1..26 ({rotorposa}"
						raise ValueError(_UpperCAmelCase					)
			if not 0 < rotorposa <= len(_UpperCAmelCase					):
						_UpperCAmelCase	 =     F"Second rotor position is not within range of 1..26 ({rotorposa})"
						raise ValueError(_UpperCAmelCase					)
			if not 0 < rotorposa <= len(_UpperCAmelCase					):
						_UpperCAmelCase	 =     F"Third rotor position is not within range of 1..26 ({rotorposa})"
						raise ValueError(_UpperCAmelCase					)

			# Validates string and returns dict
			_UpperCAmelCase	 =     _plugboard(_UpperCAmelCase					)

			return rotpos, rotsel, pbdict





def 							A			(  _UpperCAmelCase       :     str					)       ->				dict[str, str]:







			'''simple docstring'''

			# tests the input string if it
			# a) is type string
			# b) has even length (so pairs can be made)
			if not isinstance(_UpperCAmelCase							,     _UpperCAmelCase					):
						_UpperCAmelCase	 =     F"Plugboard setting isn't type string ({type(_UpperCAmelCase					)})"
						raise TypeError(_UpperCAmelCase					)
			elif len(_UpperCAmelCase					) % 2 != 0:
						_UpperCAmelCase	 =     F"Odd number of symbols ({len(_UpperCAmelCase					)})"
						raise Exception(_UpperCAmelCase					)
			elif pbstring == "":
						return {}

			pbstring.replace(' '							,     ''					)

			# Checks if all characters are unique
			_UpperCAmelCase	 =     set()
			for i in pbstring:
						if i not in abc:
									_UpperCAmelCase	 =     F"'{i}' not in list of symbols"
									raise Exception(_UpperCAmelCase					)
						elif i in tmppbl:
									_UpperCAmelCase	 =     F"Duplicate symbol ({i})"
									raise Exception(_UpperCAmelCase					)
						else:
									tmppbl.add(_UpperCAmelCase					)
			del tmppbl

			# Created the dictionary
			_UpperCAmelCase	 =     {}
			for j in range(0							,     len(_UpperCAmelCase					) - 1							,     2					):
						_UpperCAmelCase	 =     pbstring[j + 1]
						_UpperCAmelCase	 =     pbstring[j]

			return pb





def 							A			(  _UpperCAmelCase       :     str							,     _UpperCAmelCase       :     RotorPositionT							,     _UpperCAmelCase       :     RotorSelectionT = (rotora, rotora, rotora)							,     _UpperCAmelCase       :     str = ""							,     )       ->				str:







			'''simple docstring'''
			_UpperCAmelCase	 =     text.upper()
			_UpperCAmelCase ,  _UpperCAmelCase ,  _UpperCAmelCase	 =     _validator(
			    _UpperCAmelCase							,     _UpperCAmelCase							,     plugb.upper()					)

			_UpperCAmelCase ,  _UpperCAmelCase ,  _UpperCAmelCase	 =     rotor_position
			_UpperCAmelCase ,  _UpperCAmelCase ,  _UpperCAmelCase	 =     rotor_selection
			rotorposa -= 1
			rotorposa -= 1
			rotorposa -= 1

			_UpperCAmelCase	 =     []

			# encryption/decryption process --------------------------
			for symbol in text:
						if symbol in abc:
									# 1st plugboard --------------------------
									if symbol in plugboard:
												_UpperCAmelCase	 =     plugboard[symbol]

									# rotor ra --------------------------
									_UpperCAmelCase	 =     abc.index(_UpperCAmelCase					) + rotorposa
									_UpperCAmelCase	 =     rotora[index % len(_UpperCAmelCase					)]

									# rotor rb --------------------------
									_UpperCAmelCase	 =     abc.index(_UpperCAmelCase					) + rotorposa
									_UpperCAmelCase	 =     rotora[index % len(_UpperCAmelCase					)]

									# rotor rc --------------------------
									_UpperCAmelCase	 =     abc.index(_UpperCAmelCase					) + rotorposa
									_UpperCAmelCase	 =     rotora[index % len(_UpperCAmelCase					)]

									# reflector --------------------------
									# this is the reason you don't need another machine to decipher

									_UpperCAmelCase	 =     reflector[symbol]

									# 2nd rotors
									_UpperCAmelCase	 =     abc[rotora.index(_UpperCAmelCase					) - rotorposa]
									_UpperCAmelCase	 =     abc[rotora.index(_UpperCAmelCase					) - rotorposa]
									_UpperCAmelCase	 =     abc[rotora.index(_UpperCAmelCase					) - rotorposa]

									# 2nd plugboard
									if symbol in plugboard:
												_UpperCAmelCase	 =     plugboard[symbol]

									# moves/resets rotor positions
									rotorposa += 1
									if rotorposa >= len(_UpperCAmelCase					):
												_UpperCAmelCase	 =     0
												rotorposa += 1
									if rotorposa >= len(_UpperCAmelCase					):
												_UpperCAmelCase	 =     0
												rotorposa += 1
									if rotorposa >= len(_UpperCAmelCase					):
												_UpperCAmelCase	 =     0

        # else:
        #    pass
        #    Error could be also raised
        #    raise ValueError(
        #       'Invalid symbol('+repr(symbol)+')')
						result.append(_UpperCAmelCase					)

			return "".join(_UpperCAmelCase					)


if __name__ == "__main__":
							UpperCAmelCase__     = "This is my Python script that emulates the Enigma machine from WWII."
							UpperCAmelCase__     = (1, 1, 1)
							UpperCAmelCase__     = "pictures"
							UpperCAmelCase__     = (rotora, rotora, rotora)
							UpperCAmelCase__     = enigma(message, rotor_pos, rotor_sel, pb)

							print("Encrypted message:", en)
							print("Decrypted message:", enigma(en, rotor_pos, rotor_sel, pb))





def 							A			(  _UpperCAmelCase       :     int							,     _UpperCAmelCase       :     int					)       ->				str:







			'''simple docstring'''
			if a < 0 or b < 0:
						raise ValueError('the value of both inputs must be positive'					)

			_UpperCAmelCase	 =     str(bin(_UpperCAmelCase					)					)[2:]  # remove the leading "0b"
			_UpperCAmelCase	 =     str(bin(_UpperCAmelCase					)					)[2:]  # remove the leading "0b"

			_UpperCAmelCase	 =     max(len(_UpperCAmelCase					)							,     len(_UpperCAmelCase					)					)

			return "0b" + "".join(
			    str(int(char_a != char_b					)					)
			    for char_a, char_b in zip(a_binary.zfill(_UpperCAmelCase					)							,     b_binary.zfill(_UpperCAmelCase					)					)					)


if __name__ == "__main__":
							import doctest

							doctest.testmod()
