task1

 avatar
unknown
python
a year ago
1.5 kB
2
Indexable
from openpyxl import load_workbook  # pip install openpyxl
from openpyxl.formula.translate import Translator
from pycel import ExcelCompiler  # pip install pycel
from itertools import product


columns = {1: "A", 2: "B", 3: "C", 4: "D", 5: "E", 6: "F"}

wb = load_workbook(filename="task1.xlsx")  # Нужно создать файл task1.xlsx и вбить в него начальные данные
ws = wb.active


def fill_table(formula):
    """
    Заполняет диапазон B2":"D4" формулой formula
    """
    
    for cellObj in ws["B2":"D4"]:
        for cell in cellObj:
            cords = columns[cell.column] + str(cell.row)
            ws[cords] = Translator(formula, "B2").translate_formula(cords)


def check(formula):
    """
    Проверяет лежит ли в D4 нужное значение
    """
    
    fill_table(formula)
    wb.save("task_answer.xlsx")
    excel = ExcelCompiler("task_answer.xlsx")
    value = excel.evaluate("D4")

    if value == 37125:
        return True
    return False


def brute_force():
    """
    Перебирает формулы, если лежит нужное значение, то выводит ответ
    """
    
    for ans in product([0, 1], repeat=6):
        formula = f"={ans[0]*"$"}A{ans[1]*"$"}1*{ans[2]*"$"}B{ans[3]*"$"}1*{ans[4]*"$"}A{ans[5]*"$"}2"
        if check(formula):
            print("".join([str(i) for i in ans]))
            return

brute_force()

Leave a Comment