编辑代码

from itertools import product

# 提供的数字范围,仅考虑第一位和第四位
provided_digits_str = "2345789 0134679"
provided_digits = [set(map(int, digits)) for digits in provided_digits_str.split()]

# 全部数字范围
all_digits = set(range(10))

# 初始化字典存储结果
combinations = {i: [] for i in range(3)}  # 0 到 2 (最多 2 个位置匹配)

# 遍历所有可能的组合
for first in all_digits:
    for second in all_digits:
        for third in all_digits:
            for fourth in all_digits:
                combo = (first, second, third, fourth)
                matched_positions = sum(1 for i, digit in enumerate(combo) if (i == 0 or i == 3) and digit in provided_digits[i//3])

                if matched_positions == 2:  # 特殊情况:第一位和第四位必须匹配
                    if combo[0] in provided_digits[0] and combo[3] in provided_digits[1]:
                        combinations[2].append("".join(map(str, combo)))
                else:
                    combinations[matched_positions].append("".join(map(str, combo)))

# 直接打印结果
for value, combo_list in combinations.items():
    print(f"Value {value}:")
    print(" ".join(combo_list))
    print()