Untitled

 avatar
unknown
plain_text
2 years ago
1.3 kB
4
Indexable
def decode(message_file):
    """
    Decodes a message based on a specific encoding rule from a given file.

    The file contains pairs of numbers and words. The function constructs a 'pyramid',
    where each line of the pyramid contains one more number than the previous one.
    Only the numbers at the end of each pyramid line are used to decode the message.

    Args:
    message_file (str): The path to the file containing the encoded message.

    Returns:
    str: The decoded message.
    """

    # Read the content of the file and create a dictionary mapping numbers to words
    with open(message_file, 'r') as file:
        lines = file.readlines()
        word_dict = {int(line.split()[0]): line.split()[1].strip() for line in lines if line.strip()}

    message = []
    current_num = 1
    increment = 2

    # Loop through the numbers, following the pyramid pattern, to decode the message
    while current_num in word_dict:
        message.append(word_dict[current_num])
        current_num += increment
        increment += 1

    # Join the decoded words into a single string and return
    return 
' '.join(message)

# Example usage:
file_path = 'coding_qual_input.txt'

# Assuming the file 'encoded_message.txt' exists and is properly formatted
decoded_message = decode(file_path)
print(decoded_message)
Editor is loading...
Leave a Comment