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 字 ...
随机推荐
- 异步Servlet和异步过虑器
异步处理功能可以节约容器线程.此功能的作用是释放正在等待完成的线程,是该线程能够被另一请求所使用. 要编写支持异步处理的 Servlet 或者过虑器,需要设置 asyncSupported 属性为 t ...
- Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用
Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019869 ...
- .core 学习文档
https://docs.microsoft.com/zh-cn/aspnet/core/razor-pages/?view=aspnetcore-2.1&tabs=visual-studio
- Longest Turbulent Subarray LT978
A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: For i <= k < j ...
- ros 编程习惯
1.设置ros的info,warning,debug,error等编写的时候要思考,何时该使用,以及在开头要使用设置rosconsole的级别来对应输出,以方便调试. 2.在使用ros_info等的时 ...
- R语言2版本3版本安装
./configure --prefix=/YZpath/public/software/R/R-3.5.0 --with-readline=no --with-x=no make make inst ...
- java8 forEach Map List[转载]
java8 forEach 在Map和List中的使用 原始的使用 Map<String, Integer> items = new HashMap<>(); items.pu ...
- 2019.01.21 NOIP训练 可持久化序列【模板】(可持久化treap)
传送门 题意简述:支持在把某个数插入到某版本的第k个位置,删除某版本第k个数,询问第k个数. 思路:用可持久化treaptreaptreap维护区间第kkk个位置的数是啥就可以了. 代码
- python 安装教程
1) 安装python2.7,下载地址 https://www.python.org/downloads/ ----2.7 安装完成后,设置环境变量加入path --d:/ruanjian/p ...
- java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory的解决办法
Tomcat7早就出来正式版,但是一直都没有用过,尤其是针对于我还一直在用Myeclipse6.5的人来说,它在配置tomcat的时候没有tomcat7的选项,所以就报了错误信息. java.lang ...