python 解决2018年刑侦推理试题

编程这种东西。。。就是要有东西玩玩才开心。。。

python 解决2018年刑侦推理试题_第1张图片

 

废话不多说,直接上源码:

import sys
from itertools import product

answer = list('0AAAAAAAAAA')


def q1(choice):
    if answer[1] == choice:
        return True
    return False


def q2(choice):
    choices = {'A': 'C', 'B': 'D', 'C': 'A', 'D': 'B'}
    if choices[choice] == answer[5]:
        return True
    else:
        return False


def q3(choice):
    if choice == 'A':
        if answer[6] == answer[2] == answer[4] \
                and answer[6] != answer[3]:
            return True

    if choice == 'B':
        if answer[3] == answer[2] == answer[4] \
                and answer[3] != answer[6]:
            return True

    if choice == 'C':
        if answer[3] == answer[6] == answer[4] \
                and answer[3] != answer[2]:
            return True

    if choice == 'D':
        if answer[3] == answer[6] == answer[2] \
                and answer[3] != answer[4]:
            return True

    return False


def q4(choice):
    if choice == 'A':
        if answer[1] == answer[5]:
            return True
    elif choice == 'B':
        if answer[2] == answer[7]:
            return True
    elif choice == 'C':
        if answer[1] == answer[9]:
            return True
    elif choice == 'D':
        if answer[6] == answer[10]:
            return True

    return False


def q5(choice):
    if answer[8] == choice:
        return True
    elif answer[4] == choice:
        return True
    elif answer[9] == choice:
        return True
    elif answer[7] == choice:
        return True

    return False


def q6(choice):
    if 'A' == choice:
        if answer[2] == answer[4] == answer[8]:
            return True
    elif 'B' == choice:
        if answer[1] == answer[6] == answer[8]:
            return True
    elif 'C' == choice:
        if answer[3] == answer[10] == answer[8]:
            return True
    elif 'D' == choice:
        if answer[5] == answer[9] == answer[8]:
            return True

    return False


def q7(choice):
    num_choice = {'A': 0, 'B': 0, 'C': 0, 'D': 0}
    for an in answer:
        if an not in num_choice.keys():
            continue
        num_choice[an] = num_choice[an] + 1
    an = min(zip(num_choice.values(), num_choice.keys()))
    choices = {'A': 'C', 'B': 'B', 'C': 'A', 'D': 'D'}
    if choices[choice] == an[1]:
        return True

    return False


def q8(choice):
    choices = {'A': 7, 'B': 5, 'C': 2, 'D': 10}
    if abs(ord(answer[choices[choice]]) - ord(answer[1])) != 1:
        return True

    return False


def q9(choice):
    choices = {'A': 6, 'B': 10, 'C': 2, 'D': 9}
    if answer[1] == answer[6]:
        if choices[choice] != answer[5]:
            return True
    elif answer[1] != answer[6]:
        if answer[choices[choice]] == answer[5]:
            return True
    return False


def q10(choice):
    num_choice = {'A': 0, 'B': 0, 'C': 0, 'D': 0}
    for an in answer:
        if an not in num_choice.keys():
            continue
        num_choice[an] = num_choice[an] + 1

    an_min = min(zip(num_choice.values(), num_choice.keys()))
    an_max = max(zip(num_choice.values(), num_choice.keys()))
    choices = {'A': 3, 'B': 2, 'C': 4, 'D': 1}
    if choices[choice] == an_max[0] - an_min[0]:
        return True

    return False


list_qfun = (q1, q2, q3, q4, q5, q6, q7, q8, q9, q10)

l = ['A', 'B', 'C', 'D']
an = list(product(l, repeat=10))
i = 0
get_answer = False
while i < len(an):
    answer = an[i]
    answer = list(answer)
    answer.insert(0, '0')
    for q_index in range(0, 10):
        flag = list_qfun[q_index](answer[q_index+1])
        if not flag:
            break

        if q_index == 9:
            get_answer = True
            print("get answer: %s" % str(answer[1:]))
            break
    if get_answer:
        break
    i += 1


 

你可能感兴趣的:(杂项)