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

Function solution

project_euler/problem_059/sol1.py:101–124  ·  view source on GitHub ↗

Test the ciphertext against all possible 3-character keys, then narrow down the possibilities by filtering using common words until there's only one possible decoded message. >>> solution("test_cipher.txt") 3000

(filename: str = "p059_cipher.txt")

Source from the content-addressed store, hash-verified

99
100
101def solution(filename: str = "p059_cipher.txt") -> int:
102 """
103 Test the ciphertext against all possible 3-character keys, then narrow down the
104 possibilities by filtering using common words until there's only one possible
105 decoded message.
106 >>> solution("test_cipher.txt")
107 3000
108 """
109 ciphertext: list[int]
110 possibles: list[str]
111 common_word: str
112 decoded_text: str
113 data: str = Path(__file__).parent.joinpath(filename).read_text(encoding="utf-8")
114
115 ciphertext = [int(number) for number in data.strip().split(",")]
116
117 possibles = filter_valid_chars(ciphertext)
118 for common_word in COMMON_WORDS:
119 possibles = filter_common_word(possibles, common_word)
120 if len(possibles) == 1:
121 break
122
123 decoded_text = possibles[0]
124 return sum(ord(char) for char in decoded_text)
125
126
127if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 3

filter_valid_charsFunction · 0.85
filter_common_wordFunction · 0.85
splitMethod · 0.80

Tested by

no test coverage detected