3个教室,9个老师平均分配问题

课室和老师分别为:

classes = [[],[],[]]

teacheres = ['A','B','C','D','E','F','G','H','L']

 先是随机分配:

for teacher in teacheres:
    index = random.randint(0, 2)
    classes[index].append(teacher)

print(classes)

平均分配递归的方法:

def append_teacher(teacher):
    index = random.randint(0, 2)
    if len(classes[index]) <= 2:
        classes[index].append(teacher)
    else:
        return append_teacher(teacher)


for teacher in teacheres:
    append_teacher(teacher)

平均分配循环的方法:

while len(teacheres) !=0:
    for teacher in teacheres:
        index = random.randint(0, 2)
        if len(classes[index]) <= 2:
            classes[index].append(teacher)
            teacheres.remove(teacher)

 

posted @ 2020-06-23 10:35  Tracydzf  阅读(245)  评论(0编辑  收藏  举报