aboutsummaryrefslogtreecommitdiff
path: root/challenge-046/lubos-kolouch/python/ch-1.py
blob: 9a8ce0e699aef41d4b8ddd11b9dbee2bf3b259e0 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import Counter


def decrypt_message(lines: [str]) -> str:
    """
    Decrypts the message by selecting the most frequent character at each position.

    Args:
        lines (list of str): The repeated message lines.

    Returns:
        str: The decrypted message.
    """
    message_length = len(lines[0])
    decrypted_message = ""

    for i in range(message_length):
        column_chars = [line[i] for line in lines]
        most_common_char = Counter(column_chars).most_common(1)[0][0]
        decrypted_message += most_common_char

    return decrypted_message


if __name__ == "__main__":
    repeated_message = [
        "P + 2 l ! a t o",
        "1 e 8 0 R $ 4 u",
        "5 - r ] + a > /",
        "P x w l b 3 k \\",
        "2 e 3 5 R 8 y u",
        "< ! r ^ ( ) k 0",
    ]

    decrypted_message = decrypt_message(repeated_message)
    print(decrypted_message)