朋友遇到一个面试题,让我帮忙实现,题目如下:

红队有A1,B1,C1三名队员,蓝队有A2,B2,C2三名队员,每轮比赛各队出一名队员参加,一名队员只能参加一次比赛,假设A1不会和B2打,B1不会和B2和C2打,那么可能出现的组合情况是什么?

这个面试题的难点在于如何算出所有可能的组合,考虑扩展性的话,还得考虑两队人数不相同的问题,因此有了下面代码:

# coding: utf-8
import copy def get_team_group_list(team1, team2):
if len(team1) == 1 or len(team2) == 1:
team_group_list = list()
for user1 in team1:
for user2 in team2:
user_group = {
"U1": user1,
"U2": user2
}
user_group_list = [user_group]
team_group_list.append(user_group_list)
return team_group_list
else:
sub_team1 = team1[1:]
user1 = team1[0]
team_group_list = list()
for user2 in team2:
sub_team2 = filter(lambda x: x != user2, team2)
sub_team_group_list = get_team_group_list(sub_team1, sub_team2)
for user_group_list in sub_team_group_list:
tmp_user_group_list = copy.deepcopy(user_group_list)
user_group = {
"U1": user1,
"U2": user2
}
tmp_user_group_list.append(user_group)
team_group_list.append(tmp_user_group_list)
return team_group_list def test():
team1 = ["A1", "B1", "C1"]
team2 = ["A2", "B2", "C2"]
exclude_condition_list = [
{
"U1": "A1",
"U2": "B2"
},
{
"U1": "B1",
"U2": "B2"
},
{
"U1": "B1",
"U2": "C2"
} ]
team_group_list = get_team_group_list(team1, team2)
for num in range(0, len(team_group_list)):
print("肯能组合{0}:".format(num))
print(team_group_list[num])
for num in range(0, len(team_group_list)):
is_valid = True
team_group = team_group_list[num]
for exclude_condition in exclude_condition_list:
match_list = filter(lambda user_group:
(
user_group["U1"] == exclude_condition["U1"] and
user_group["U2"] == exclude_condition["U2"]
),
team_group)
if len(match_list) > 0:
is_valid = False
if is_valid:
print("组合{0}满足条件:".format(num))
print(team_group_list[num]) test()

显示效果为:

肯能组合0:
[{'U1': 'C1', 'U2': 'C2'}, {'U1': 'B1', 'U2': 'B2'}, {'U1': 'A1', 'U2': 'A2'}]
肯能组合1:
[{'U1': 'C1', 'U2': 'B2'}, {'U1': 'B1', 'U2': 'C2'}, {'U1': 'A1', 'U2': 'A2'}]
肯能组合2:
[{'U1': 'C1', 'U2': 'C2'}, {'U1': 'B1', 'U2': 'A2'}, {'U1': 'A1', 'U2': 'B2'}]
肯能组合3:
[{'U1': 'C1', 'U2': 'A2'}, {'U1': 'B1', 'U2': 'C2'}, {'U1': 'A1', 'U2': 'B2'}]
肯能组合4:
[{'U1': 'C1', 'U2': 'B2'}, {'U1': 'B1', 'U2': 'A2'}, {'U1': 'A1', 'U2': 'C2'}]
肯能组合5:
[{'U1': 'C1', 'U2': 'A2'}, {'U1': 'B1', 'U2': 'B2'}, {'U1': 'A1', 'U2': 'C2'}]
组合4 满足条件:
[{'U1': 'C1', 'U2': 'B2'}, {'U1': 'B1', 'U2': 'A2'}, {'U1': 'A1', 'U2': 'C2'}]

通过递归方式来解决

===================================================

其实啥都是虚的,你们都是来看妹子的,是不是!!!

python--求参赛两队所有可能的比赛组合情况的更多相关文章

  1. python中对两个 list 求交集,并集和差集

    python中对两个 list 求交集,并集和差集: 1.首先是较为浅白的做法: >>> a=[1,2,3,4,5,6,7,8,9,10] >>> b=[1,2,3 ...

  2. Python 求两个文本文件以行为单位的交集 并集 差集

    Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...

  3. Python 求点到直线的垂足

    Python 求点到直线的垂足 在已知一个点,和一条已知两个点的直线的情况下 运算公式参考链接:https://www.cnblogs.com/mazhenyu/p/3508735.html def ...

  4. 使用python求字符串或文件的MD5

    使用python求字符串或文件的MD5 五月 21st, 2008 #以下可在python3000运行. #字符串md5,用你的字符串代替'字符串'中的内容. import hashlib md5=h ...

  5. python求微分方程组的数值解曲线01

    本人最近在写一篇关于神经网络同步的文章,其一部分模型为: x_i^{\Delta}(t)= -a_i*x_i(t)+ b_i* f(x_i(t))+ \sum\limits_{j \in\{i-1, ...

  6. 基础知识:编程语言介绍、Python介绍、Python解释器安装、运行Python解释器的两种方式、变量、数据类型基本使用

    2018年3月19日 今日学习内容: 1.编程语言的介绍 2.Python介绍 3.安装Python解释器(多版本共存) 4.运行Python解释器程序两种方式.(交互式与命令行式)(♥♥♥♥♥) 5 ...

  7. 周一02.3运行python程序的两种方式

    一.运行python程序的两种方式 方法一:交互式:                     优点:输入一行代码立刻返回结果                      缺点:无法永久保存代码 方法二: ...

  8. 执行python解释器的两种方式

    执行python解释器的两种方式 1.交互式 python是高级语言,是解释型语言,逐行翻译,写一句翻译一句 print ('hello world') 2.命令行式 python和python解释器 ...

  9. 比较python类的两个instance(对象) 是否相等

    http://www.yihaomen.com/article/python/281.htm 比较python类的两个instance(对象) 是否相等 作者:轻舞肥羊 日期:2012-10-25 字 ...

随机推荐

  1. Robotframework与unittest对比

    都可以自动挂ui测试 都可以自动化接口测试

  2. 转载 html div三列布局占满全屏(左右两列定宽或者百分比、中间自动适应,div在父div中居底)

    原文地址:http://blog.csdn.net/duyelang/article/details/20558899 <p><!DOCTYPE html> <html ...

  3. 使用SpringMVC的@CrossOrigin注解解决跨域请求问题

    跨域问题,通俗说就是用ajax请求其他站点的接口,浏览器默认是不允许的.同源策略(Same-orgin policy)限制了一个源(orgin)中加载脚本或脚本与来自其他源(orgin)中资源的交互方 ...

  4. 建库,建表,添加数据 SQL命令

    create database ssm default character set utf8; use ssm; create table flower( id int(10) primary key ...

  5. java笔记--问题总结

    1. 垃圾回收算法 标记-清除算法 标记-清除算法是最基本的算法,和他的名字一样,分为两个步骤,一个步骤是标记需要回收的对象.在标记完成后统一回收被标记的对象.这个算法两个问题.一个是效率问题,标记和 ...

  6. 透过摩拜和ofo,看产品从0到1时如何取舍需求(转)

    大纲 背景介绍 从0至1,我们成功的关键是什么? 从0到1,我们为什么选择做?又为什么选择不做? 从0到1,我们面临什么选择?我们作出了什么选择? 从0到1,我们为什么作出了这种选择? 背景 在资本注 ...

  7. 存储引擎中MYIASM是什么意思

  8. 关于上级机构的冲突性测试bug修复

    描述: 1.上级机构可以为空. 2.机构添加时,选择了上级机构,在未提交前,另一用户将该机构删除,然后前一用户再提交表单,提示会保存成功,本操作应该保存失败. 思路:在上级机构不为空时,保存前进行查询 ...

  9. (6)How language shapes the way we think

    https://www.ted.com/talks/lera_boroditsky_how_language_shapes_the_way_we_think/transcript 00:12So, I ...

  10. IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA环境搭建,JPA实现非字符型设置长度

    1.在github上已有配置环境,如下链接,(需要环境JDK1.8及以上版本,Tomcat1.8及以上版本,搭建maven库,使用编译器IntellJ IDEA) https://github.com ...