python 用Python编写的循环算法实现。 #round-robin #scheduling #algorithm #python

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用Python编写的循环算法实现。 #round-robin #scheduling #algorithm #python相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python

div1 = ["Lions", "Tigers", "Jaguars", "Cougars"]
div2 = ["Whales", "Sharks", "Piranhas", "Alligators"]
div3 = ["Cubs", "Kittens", "Puppies", "Calfs"]

def create_schedule(list):
    """ Create a schedule for the teams in the list and return it"""
    s = []

    if len(list) % 2 == 1: list = list + ["BYE"]

    for i in range(len(list)-1):

        mid = len(list) / 2
        l1 = list[:mid]
        l2 = list[mid:]
        l2.reverse()	

        # Switch sides after each round
        if(i % 2 == 1):
            s = s + [ zip(l1, l2) ]
        else:
            s = s + [ zip(l2, l1) ]

        list.insert(1, list.pop())

    return s


def main():
    for round in create_schedule(div1):
        for match in round:
            print match[0] + " - " + match[1]
    print
    for round in create_schedule(div2):
        for match in round:
            print match[0] + " - " + match[1]
    print
    for round in create_schedule(div3): 
        for match in round:
            print match[0] + " - " + match[1]
    print
    for round in create_schedule(div1+div2+div3): 
        for match in round:
            print match[0] + " - " + match[1]
        print


if __name__ == "__main__":
    main()

以上是关于python 用Python编写的循环算法实现。 #round-robin #scheduling #algorithm #python的主要内容,如果未能解决你的问题,请参考以下文章

用 Python 编写游戏循环的正确方法是啥?

用 Python 编写的强大的服务器无限循环

python 实现有向图和相关的DFS,循环检测算法

排序算法(Python实现)

Python数据结构与算法(2.5)——循环链表

如果未找到匹配项,Python 2.7 将仅用一行退出 while 循环