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

红队有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. How to execute sudo command in remote host via SSH

    Question: I have an interactive shell script, that at one place needs to ssh to another machine (Ubu ...

  2. [FreeMind] 绘制思维时遇到的常见问题解决办法

    如何改变节点的摆放方向? 如果是新建节点,选择要放置节点的那一侧,按enter键,或者鼠标右键,插入平行节点即可. 如果是已经建好的节点,可以用ctrl+x, ctrl+v粘贴到另一边,或者选中子节点 ...

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

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

  4. solr之创建core(搜索核心,包括索引和数据)的方法

    我的solrhome为D:\solrHome\solr step1:进入solrHome会看到collection1文件夹,创建该文件夹的副本,重命名为product 进入product文件夹,进入d ...

  5. Java类加载机制及自定义加载器

    转载:https://www.cnblogs.com/gdpuzxs/p/7044963.html Java类加载机制及自定义加载器 一:ClassLoader类加载器,主要的作用是将class文件加 ...

  6. [ASP.NET]static变量和viewstate的使用方法

    在.Net平台下进行CS软件开发时,我们经常遇到以后还要用到某些变量上次修改后的值,为了简单起见,很多人都习惯用static来定义这些变量,我也是.这样非常方便,下一次调用某个函数时该变量仍然保存的是 ...

  7. 20155312张竞予 20170510实践一:在IDEA中以TDD的方式对String类和Arrays类进行学习

    实践题目 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Arrays类 sort binarySea ...

  8. 819. Most Common Word

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 2019.01.26 codeforces 632E. Thief in a Shop(生成函数)

    传送门 题意简述:给nnn个物件,物件iii有一个权值aia_iai​,可以选任意多个.现在要求选出kkk个物件出来(允许重复)问最后得到的权值和的种类数. n,k,ai≤1000n,k,a_i\le ...

  10. Win7 VS2013环境编译Lua5.3.1

    主要参考这篇文章,原文有几个错误顺便改正了. 在Windows下使用Visual Studio编译Lua5.3 写本文时Lua官方网站放出的新版本为5.3.1,然后我不知道为啥,神奇的国内不能访问Lu ...