PYTHON Realizes Scheduling (Elimination System)

PYTHON Realizes Scheduling (Elimination System)

Source of ideas

Recently, in the summer's tail, Xiao shin's company wants to build a sports team to compete in badminton, table tennis and other competitions, because the number of registered people is not even, there will be odd numbers, some people will be idle.
According to the idea of the organizer of the tournament, in fact, it is the departmental boss that the single person will not be idle, and will catch one more match. In this way, in order to avoid the imbalance in the hearts of the multi-tournament people, adhering to the principle of openness and fairness, we have the code for this tournament.

thinking

  1. It is necessary to determine whether the number of applicants can directly arrange the elimination schedule, that is, the power of the number of applicants is 2:2,4,8,16,32.
  2. If there is an extra number of people, we need to catch them right and race one more round.

Used Libraries

import math,random

judge

def judge(num):
    power=math.log(num,2)
    floor=math.floor(power)
    flag=0
    rest=0
    if power-floor==0:
        flag=1
        rest=0
    else:
        flag=0
        rest=num-(2**floor)
    return [flag,floor,rest]

lag: 1 is the integral power of 2, 0 is not

floor: The number of rounds in the race

rest: the number of people selected for one more round

Schedule

def sortgame(personlist1):
    personlist=personlist1.copy()
    num=len(personlist)
    game_sort_list=[]
    if num>1 :
        re=judge(num)
        
        if re[0]==1:
            for i in range(2**(re[1]-1)):
                
                person1=random.choice(personlist)
                personlist.remove(person1)
                person2=random.choice(personlist)
                personlist.remove(person2)
                
                game_sort_list.append(person1+' VS '+person2)
        elif re[0]==0:
            restmatch=[]
            for i in range(re[2]):
                person1=random.choice(personlist)
                personlist.remove(person1)
                person2=random.choice(personlist)
                personlist.remove(person2)
                restmatch.append('[ '+person1+' VS '+person2+' ]')
            for i in restmatch:
                person3=random.choice(personlist)
                personlist.remove(person3)
                game_sort_list.append(i+' VS '+person3)
                
            rest=num-re[2]*3
        
            for i in range(int(rest/2)):
                person1=random.choice(personlist)
                personlist.remove(person1)
                person2=random.choice(personlist)
                personlist.remove(person2)
                
                game_sort_list.append(person1+' VS '+person2)
                
    else:
        print('only one person!')
    return game_sort_list

The choice function is used here, one person is randomly selected from the list and removed using the remove function.
The value returned here is a string spliced by two players, such as `Team 1 vs Team 2'. Of course, in order to facilitate the drawing operation, the format of the value obtained in the function can be modified.

Demo

male_single=['Xiaoming','Xiaohong','Xiaopeng','Xiaowen','Xiao Chen','Xiaoxu',
             'Xiaoqi','petty thief','Xiaogu','Xiaocao','Xiaobin','Xiaocai',
             'Week','ten-cent or twenty-cent silver coin','Xiaojian','Xiaobai','Microfacies','Xiaoxing','Xiaoxia']
a=sortgame(male_single)
print(a)

Result

['[ Xiaobai VS Xiaoxing ] VS Xiaocao',
 '[ ten-cent or twenty-cent silver coin VS petty thief ] VS Xiaohong',
 '[ Xiaowen VS Microfacies ] VS Week',
 'Xiaoxu VS Xiaogu',
 'Xiaobin VS Xiaoqi',
 'Xiao Chen VS Xiaoxia',
 'Xiaopeng VS Xiaoming',
 'Xiaocai VS Xiaojian']

If there is a simpler way, please tell me, here is Xiao shin. Welcome to give me some advice. Thank you.

Keywords: REST Python

Added by J-C on Wed, 09 Oct 2019 02:21:46 +0300