import copy
   
t_horse_time = dict([('t1', 1.5), ('t2', 2.5), ('t3', 3.5)])
q_horse_time = dict([('q1', 1), ('q2', 2), ('q3', 3)])
q_horse = ['q1', 'q2', 'q3']
def compare(t, q):
    """To Compare which one wins the horse race
        
    t, q: the horse sequence list
    """
    t_won_cnt = 0
    for i in range(len(t)):
        print(t_horse_time[t[i]], q_horse_time[q[i]])
        if t_horse_time[t[i]] < q_horse_time[q[i]]:
            t_won_cnt += 1
    if (t_won_cnt > (len(t) / 2)):
        print('田忌获胜!')
    else:
        print('齐王获胜!')
        
def permulation(horses, result):
    """列出所有可能的比赛方式
    
    horses: 目前还有多少马没有出战, result: 保存当前已经出战的马匹顺序
    """
    # 所有马匹都已经出战,判断哪方获胜,输出结果
    if (len(horses) == 0):
        print(result)
        compare(result, q_horse)
        return
    for cnt in range(len(horses)):
        #从剩下的未出战马匹中,选择一匹,加入结果
        new_result = copy.deepcopy(result)
        new_result += [horses[cnt]]
        #将已选择的马匹从未出战的列表中移出
        new_horses = copy.deepcopy(horses)
        del new_horses[cnt]
        #递归调用,对于剩余的马匹继续生成排列
        permulation(new_horses, new_result)
if __name__ == '__main__':
    horses = ['t1', 't2', 't3']
    permulation(horses, [])
 展开