Python解决爱因斯坦的逻辑题

问题:

  • 有5个具有不同颜色的房间;
  • 每个房间里住着一个不同国籍的人;
  • 每个人都在喝一种特定品牌的饮料;
  • 抽一特定品牌的香烟;
  • 养一特定的宠物;
  • 没有任意的两个人在抽相同品牌的烟、或喝相同品牌饮料、或养相同的宠物。
  • 同时提供下面这些线索:
  1. 英国人住在红房子里
  2. 瑞典人养狗
  3. 丹麦人喝茶
  4. 绿房子紧挨着白房子,在白房子的左边
  5. 绿房子主人喝咖啡
  6. 抽PALL MALL牌香烟的人养鸟
  7. 黄房子里的人抽DUNHILL牌的烟
  8. 住中间房子的人喝牛奶
  9. 挪威人住在第一个房子里(最左边)
  10. 抽BLENDS香烟的人和养猫的人相邻
  11. 养马的人和抽DUNHILL牌香烟的人相邻
  12. 抽BLUEMASTER牌香烟的人喝啤酒
  13. 德国人抽PRINCE牌香烟
  14. 挪威人和住蓝房子的人相邻
  15. 抽BLENDS牌香烟的人和喝矿泉水的人相邻。
  • 问:谁在养鱼?

Solution:

# -*- coding: GBK -*-
import time
import itertools

def handle(data):

    # 英国人住红色房子
    index = data[0].index('红色')
    if data[1][index] !='英国':
        return -1
 
    # 瑞典人养狗
    index = data[4].index('狗')
    if data[1][index] !='瑞典':
        return -2
 
    # 丹麦人喝茶
    index = data[2].index('茶')
    if data[1][index] !='丹麦':
        return -3
 
    # 抽Pall Mall香烟的人养鸟
    index = data[3].index('Pall Mall')
    if data[4][index] != '鸟':
        return -6
 
    # 黄色房子主人抽Dunhill香烟
    index = data[3].index('Dunhill')
    if data[0][index] != '黄色':
        return -7
 
    # 抽Blue Master的人喝啤酒
    index = data[3].index('BlueMaster')
    if data[2][index] != '啤酒':
        return -12
 
    # 德国人抽Prince香烟
    index = data[3].index('Prince')
    if data[1][index] != '德国':
        return -13
 
    # 绿色房子在白色房子左面
    index = data[0].index('绿色')
    if index == 4:  # 绿色房子在最后
        return -4
    if data[0][index + 1] != '白色':
        return -4
 
    # 绿色房子主人喝咖啡
    if data[2][index] != '咖啡':
        return -5
 
    # 抽Blends香烟的人住在养猫的人隔壁
    index = data[3].index('Blends')
    cat_index = data[4].index('猫')
    if cat_index - index != 1 and cat_index - index != -1:
        return -10
 
    # 养马的人住抽Dunhill香烟的人隔壁
    index = data[3].index('Dunhill')
    horse_index = data[4].index('马')
    if horse_index - index != 1 and horse_index - index != -1:
        return -11
 
    # 抽Blends香烟的人有一个喝水的邻居
    index = data[3].index('Blends')
    water_index = data[2].index('水')
    if water_index - index != 1 and water_index - index != -1:
        return -15
 
    print('找到答案:')
    for d_item in data:
        print(d_item)
    return 0

colour_list_ = list(itertools.permutations(['红色', '黄色', '蓝色', '白色', '绿色'], 5))
country_list_ = list(itertools.permutations(['英国', '丹麦', '挪威', '德国', '瑞典'], 5))
drinks_list_ = list(itertools.permutations(['茶', '水', '咖啡', '啤酒', '牛奶'], 5))
smoke_list_ = list(itertools.permutations(['Pall Mall', 'Dunhill', 'BlueMaster', 'Blends', 'Prince'], 5))
pet_list_ = list(itertools.permutations(['猫', '马', '鱼', '鸟', '狗'], 5))

colour_list = []
country_list = []
drinks_list = []
smoke_list = []
pet_list = []

for colour in colour_list_:
    colour = list(colour)
    if colour[1] == '蓝色':
        colour_list.append(colour)

for country in country_list_:
    country = list(country)
    if country[0] == '挪威':
        country_list.append(country)

for drinks in drinks_list_:
    drinks = list(drinks)
    if drinks[2] == '牛奶':
        drinks_list.append(drinks)

for i in range(120):
    smoke_list.append(list(smoke_list_[i]))
    
for i in range(120):
    pet_list.append(list(pet_list_[i]))

# ~ num = 0
start = time.time()


for country in country_list:
    for drinks in drinks_list:
        for smoke in smoke_list:
            for pet in pet_list:
                for colour in colour_list:
                    
                    data_list = [colour, country, drinks, smoke, pet]
                    # ~ num += 1
                    if handle(data_list) == 0:
                        # ~ print("总运行次数 %d" % num)
                        exit()
        end = time.time()
        print("计算用时:%0.4f秒" % (end - start))

Output:
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值