-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
42 lines (34 loc) · 1.13 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def decode(message_file):
OVERWRITE_DUPLICATE = True
sentence = {}
steps = []
with open(message_file, "r") as file:
message = file.read().split("\n")
for line in message:
n, word = line.split(" ")
n = int(n)
nKey = (n * (n + 1)) // 2
length = len(steps)
maxKey = (length * (length + 1)) // 2
# add more steps only when needed
if nKey > maxKey:
while nKey >= maxKey:
steps.append(maxKey)
sentence[maxKey] = None
length += 1
maxKey = (length * (length + 1)) // 2
# add the word to the sentence dictionary
if n in sentence:
if OVERWRITE_DUPLICATE:
sentence[n] = word
elif sentence.get(n - 1) is None:
sentence[n] = word
# compile the sentence dictionary into a decoded string
decoded_string = ""
for val in steps:
if sentence[val] is not None:
decoded_string += sentence[val] + " "
return decoded_string[:-1]
# Example usage:
decoded_message = decode("example.txt")
print(decoded_message)