from string import punctuation

# file = open('111.txt', 'a', encoding='utf-8') # a - открыть для дозаписи
# file.write('sdfsd')
# file.close()
# print(file.read(3))
# print(file.read(3))
# file.seek(0)
# print(file.read(3))
# print(file.readline())
# print(file.readline())
# print(file.readline())


# for row in file:
#     for letter in row:
#         print(letter)

# s = file.readlines()
# print(s)


def create_file_with_numbers(n: int) -> None:
    name = "range_" + str(n) + ".txt"
    file = open(name, 'w', encoding='utf-8')
    for i in range(1, n + 1):
        file.write(str(i) + '\n')
    file.close()

# create_file_with_numbers(5)

def longest_word_in_file(file_name: str) -> str:
    long_word = ""
    row_list = []
    max_len = 0
    file = open(file_name, 'r', encoding='utf-8')
    for row in file:
        row_list = [x.strip(punctuation) for x in row.split()]
        for word in row_list:
            if len(word) >= max_len:
                max_len = len(word)
                long_word = word
    file.close()
    return long_word
    
# longest_word_in_file('111.txt')


def find_numbers():
    file = open('numbers.txt', 'r', encoding='utf-8')
    cnt = 0
    s = 0
    for num in file:
        if len(num) == 4:
            cnt += 1
            print(num)
        if len(num) == 3:
            s += int(num)
    file.close()

    print(cnt, s)

# find_numbers()


def find_lines_len_more_6(file_name : str) -> int:
    count = 0
    with open(file_name, 'r', encoding='utf-8') as f:
        for row in f:
            if len(row) - 1 > 6:
                count += 1
    return count
    

# print(find_lines_len_more_6('111.txt'))


def find_unic_words(file_name : str) -> int:
    my_dict = {}
    row_list = []
    with open(file_name, 'r', encoding='utf-8') as f:
        for row in f:
            row_list = [x.lower() for x in row.split()]
            for word in row_list:
                if word in my_dict.keys():
                    num = my_dict.get(word)
                    my_dict.update({word: num + 1})
                else:
                    my_dict.setdefault(word, 1)

    print(len(my_dict))

# find_unic_words('lorem.txt')


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def find_words_count(file_name : str) -> None:
    words = {}
    row_list = []
    with open(file_name, 'r', encoding='utf-8') as f:
        for row in f:
            row_list = [x.upper() for x in row.split()]
            for word in row_list:
                if word in words.keys():
                    num = words.get(word)
                    words.update({word: num + 1})
                else:
                    words.setdefault(word, 1)
    

# find_words_count("lorem.txt")

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def file_process(file_name):
    my_list = []
    with open(file_name, 'r', encoding='utf-8') as file:
        for word in file:
            if word.upper().endswith('ЕЯ\n'):
                my_list.append(word.upper().rstrip('\n'))
    my_list = list(set(my_list))    
    my_list = sorted(my_list, key=lambda x: (len(x), x))
    print(my_list)



file_process("words.txt")


my_list = []
with open(file_name, 'r', encoding='utf-8') as file:
    for word in file:
        if word.upper().endswith('ЕЯ\n'):
            my_list.append(word.upper().rstrip('\n'))
my_list = list(set(my_list))    
my_list = sorted(my_list, key=lambda x: (len(x), x))
print(my_list)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~