python--求参赛两队所有可能的比赛组合情况
朋友遇到一个面试题,让我帮忙实现,题目如下:
红队有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--求参赛两队所有可能的比赛组合情况的更多相关文章
- python中对两个 list 求交集,并集和差集
python中对两个 list 求交集,并集和差集: 1.首先是较为浅白的做法: >>> a=[1,2,3,4,5,6,7,8,9,10] >>> b=[1,2,3 ...
- Python 求两个文本文件以行为单位的交集 并集 差集
Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...
- Python 求点到直线的垂足
Python 求点到直线的垂足 在已知一个点,和一条已知两个点的直线的情况下 运算公式参考链接:https://www.cnblogs.com/mazhenyu/p/3508735.html def ...
- 使用python求字符串或文件的MD5
使用python求字符串或文件的MD5 五月 21st, 2008 #以下可在python3000运行. #字符串md5,用你的字符串代替'字符串'中的内容. import hashlib md5=h ...
- python求微分方程组的数值解曲线01
本人最近在写一篇关于神经网络同步的文章,其一部分模型为: x_i^{\Delta}(t)= -a_i*x_i(t)+ b_i* f(x_i(t))+ \sum\limits_{j \in\{i-1, ...
- 基础知识:编程语言介绍、Python介绍、Python解释器安装、运行Python解释器的两种方式、变量、数据类型基本使用
2018年3月19日 今日学习内容: 1.编程语言的介绍 2.Python介绍 3.安装Python解释器(多版本共存) 4.运行Python解释器程序两种方式.(交互式与命令行式)(♥♥♥♥♥) 5 ...
- 周一02.3运行python程序的两种方式
一.运行python程序的两种方式 方法一:交互式: 优点:输入一行代码立刻返回结果 缺点:无法永久保存代码 方法二: ...
- 执行python解释器的两种方式
执行python解释器的两种方式 1.交互式 python是高级语言,是解释型语言,逐行翻译,写一句翻译一句 print ('hello world') 2.命令行式 python和python解释器 ...
- 比较python类的两个instance(对象) 是否相等
http://www.yihaomen.com/article/python/281.htm 比较python类的两个instance(对象) 是否相等 作者:轻舞肥羊 日期:2012-10-25 字 ...
随机推荐
- h5解决移动端上滑卡顿问题
select{ -webkit-overflow-scrolling: touch;/*解决移动端滑动卡顿问题*/ -webkit-transform: translateZ(0px);/*开启GPU ...
- mybatis 插入数据并返回主键值
<insert id="insert" parameterType="com.pojo.TSubject" useGeneratedKeys=" ...
- 熟悉JSON
JSON是什么 JSON ( JavaScript Object Notation) ,是一种数据交互格式. 为什么有这个技术 Json之前,大家都用 XML 传递数据.XML 是一种纯文本格式,所以 ...
- python3版本main.py执行产生中间__pycache__详解
__pycache__ 用python编写好一个工程,在第一次运行后,总会发现工程根目录下生成了一个__pycache__文件夹,里面是和py文件同名的各种 *.pyc 或者 *.pyo 文件. 先大 ...
- [网络]10M、100M、1000M网线的水晶头接法
在网络维护过程中经常要自己制作网线,水晶头理论上是这样接的: 10M和100M和1000M以太网在使用网线时,对网线各自有不同的要求. 10M和100M在目前来说,连接网络的时候,只用到两对线来传输网 ...
- [Office]Execl取消保护密码
2007版Excel表格中可以按照以下方式建宏: 打开Excel表格中的Excel选项,选择自定义,得到如下画面: 然后在左边侧框栏中选择“查看宏”或者Alt+F11 之后双击或者选择添加按钮,则可 ...
- hdu 6208(后缀自动机、或者AC自动机
题意:给你n个字符串,问你是否存在一个字符串可以从中找到其他n-1个字符串. 思路:其实很简单,找到最长的那个字符串对他进行匹配,看是否能匹配到n-1个字符串. 可以用AC自动机或者后缀自动机做,但是 ...
- GameObject.SendMessage
Message相关有3条指令: 要接收消息的GameObject.SendMessage ("函数名",参数,SendMessageOptions) //自身和父Objec ...
- springboot 碰到的问题
1.在springboot 启动报错 ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentS ...
- pyinstaller基本操作
pyinstaller 打包错误http://www.fmwei.com/linux/pyinstaller-lib-error.html 只需要复制python安装目录下的动态库到系统地动态库目录即 ...