• + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    import string
    from collections import defaultdict
    
    class Decoder:
        _symbols = string.punctuation
    
        def __init__(self, trigram_list):
            self.final_string = ""
            self.base_string = ""
            self.trigram_list = trigram_list
            self.first_alphanumeric_pos = -1
            self.last_alphanumeric_pos = -1
            self.run_decoder()
    
        def run_decoder(self):
            self.construct_base_string()
            self.construct_final_string()
    
        def is_symbol(self, element):
            return element in self._symbols
    
        def update_first_alphanumeric_pos(self, position):
            self.first_alphanumeric_pos = position
            return True
    
        def update_last_alphanumeric_pos(self, position):
            self.last_alphanumeric_pos = position
            return True
    
        def remove_symbols(self):
            string_range = self.base_string[self.first_alphanumeric_pos: self.last_alphanumeric_pos]
            string_range_sanitized = re.sub(f"[{self._symbols}]", " ", string_range).replace("  ", " ")
            return string_range_sanitized
    
        def construct_final_string(self):
            for position, element in enumerate(self.base_string):
                self.first_alphanumeric_pos == -1 and not self.is_symbol(element) and self.update_first_alphanumeric_pos(
                    position)
                element != " " and not self.is_symbol(element) and self.update_last_alphanumeric_pos(position + 1)
    
            first_string_part = self.base_string[:self.first_alphanumeric_pos]
            second_string_part = self.remove_symbols()
            final_string_part = self.base_string[self.last_alphanumeric_pos:]
    
            self.final_string = first_string_part + second_string_part + final_string_part
    
            return self.final_string
    
        def construct_base_string(self):
            trigrams_ = enumerate(self.trigram_list)
            trigrams_ = {
                pos: dict(enumerate(trigram))
                for pos, trigram in trigrams_
            }
            join_by_index = defaultdict(list)
            for symbols in trigrams_.values():
                for pos, symbol in symbols.items():
                    join_by_index[pos] += symbol
    
            values = []
            for v in join_by_index.values():
                values += v
    
            self.base_string = "".join(values)
    
    
    first_multiple_input = input().rstrip().split()
    
    n = int(first_multiple_input[0])
    
    m = int(first_multiple_input[1])
    
    matrix = []
    
    for _ in range(n):
        matrix_item = input()
        matrix.append(matrix_item)
        
        
    decorder = Decoder(matrix)
    print(decorder.final_string)