MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / create_cipher_map

Function create_cipher_map

ciphers/simple_keyword_cypher.py:20–43  ·  view source on GitHub ↗

Returns a cipher map given a keyword. :param key: keyword to use :return: dictionary cipher map

(key: str)

Source from the content-addressed store, hash-verified

18
19
20def create_cipher_map(key: str) -> dict[str, str]:
21 """
22 Returns a cipher map given a keyword.
23
24 :param key: keyword to use
25 :return: dictionary cipher map
26 """
27 # Create a list of the letters in the alphabet
28 alphabet = [chr(i + 65) for i in range(26)]
29 # Remove duplicate characters from key
30 key = remove_duplicates(key.upper())
31 offset = len(key)
32 # First fill cipher with key characters
33 cipher_alphabet = {alphabet[i]: char for i, char in enumerate(key)}
34 # Then map remaining characters in alphabet to
35 # the alphabet from the beginning
36 for i in range(len(cipher_alphabet), 26):
37 char = alphabet[i - offset]
38 # Ensure we are not mapping letters to letters previously mapped
39 while char in key:
40 offset -= 1
41 char = alphabet[i - offset]
42 cipher_alphabet[alphabet[i]] = char
43 return cipher_alphabet
44
45
46def encipher(message: str, cipher_map: dict[str, str]) -> str:

Callers 1

mainFunction · 0.85

Calls 1

remove_duplicatesFunction · 0.70

Tested by

no test coverage detected