Krypto_Grundlagen/utils/AlphabetUtils.py
Patrick Mueller 087a01cd3d
#9: Implementing Stromchiffre
- Also various bug fixes and improvements
2021-10-22 17:35:22 +02:00

51 lines
1.4 KiB
Python

import decimal
LETTERS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z']
def is_letter_of_alphabet(letter: str) -> bool:
"""
Checks if the given letter is a valid letter of the alphabet
:param letter: The letter to check
:return: If the letter is in the alphabet
"""
return letter.upper() in LETTERS
def get_letter_at_index(idx: int, capital: bool = False) -> str:
"""
Returns the letter at the given index
:param idx: The index of the letter to return
:param capital: If the capitalized letter should be returned
:return: The letter
"""
if idx < 0 or idx > 25:
raise AttributeError
return LETTERS[idx] if capital else LETTERS[idx].lower()
def get_index_of_letter(letter: str) -> int:
"""
Returns the index of the given letter
:param letter: The letter to return the index of
:return: The index of the letter
"""
if letter.upper() not in LETTERS:
raise AttributeError
return LETTERS.index(letter.upper())
def get_binary_index_of_letter(letter: str) -> str:
"""
Returns the binary representation of the letter index
:param letter: The letter to return the index for
:return: The binary representation of the letter index
"""
char_index = get_index_of_letter(letter)
return bin(char_index)[2:].zfill(6)
def get_letter_at_binary_index(index: str) -> str:
decimal_index = int(index, 2)
return get_letter_at_index(decimal_index)