====== Fahrad-Polo-Shuffle ====== ---- datatemplateentry project ---- template : :sys:tpl:project description : Zufallsgenerator für eine faire Team zusammensetzung bein Fahrad-Polo coordinators : [[:users:pweza:]], [[:users:skruppy:]] confidants : state_tags : working, finished type_tags : software, python ---- Also nach dem das immer total nervig is, dass beim Polo immer alle rumheulen, dass sie sich benachteiligt fuehlen, will ich das mal mit nem Shuffle APP loesen. ich wollt das ganze erstmal in Python schreiben, das isch darueber python lernen laesst und ich Apple doff find (lizenzen und so) das uebertragen in ne App for kann fisch ja immer noch tun wen das programm mal funtzt und fuers iPhone giebts eh nen python App Also zur Programm anforderung: Klar wie ja schon gesagt faires Shuffle :) die situtuation is folgende: es kommen uebern tag verteilt immer wieder spieler dazu, dafuer gehen andere auch wieder frueher.\\ \\ Primaer * Ein team besteht aus 3 Leuten * Es spielen 2 teams auf einen Feld * Keine/r soll laenger warten muessen als die anderen (das is der haupt grund) * Es giebt n durchlaeufe Was noch toll währe * es giebt n court's * Jede/r sollte mal mit und auch mal gegen jede/n andren spielen ===== Implementierungen ===== ==== pweza ==== === code === #! /usr/bin/env python import random # kommt noch nen input hin welches file f = open('shuffle.txt', 'r+') playerlist = [] playerstack = [] team1 = [] team2 = [] cond = 0 # List die Spielerliste aus der Datei aus und schreibt diese in den STACK playerlist for line in f: ram = line.strip() playerlist.append(ram) f.close() # Schreibt playerlist in zufaelliger folge in den STACK playerstack for player in playerlist: rand = random.randint(0, len(playerlist)-1) playerstack.append(playerlist.pop(rand)) # weis net ob das mit STACK's ueberhaupt geht aber ich haette es gerne, das an dieser stelle jedem player eine varieabel mit der anzal an spielen hinzugefuegt wird, die dann im naechsten Shuffle mit beruecksichtigt wird # Stellt team1 aus dem STACK zusammen und loescht diese aus selbigen if len(playerstack) >= 3: team1.append(playerstack.pop(0)) team1.append(playerstack.pop(1)) team1.append(playerstack.pop(2)) print 'Team1:{}'.format(team1) # die verbleibenden player werden in die Team varieable uebernommen um nach nach einen neuen Shuffle mit weitern ergaenst zu werden else: while len(playerstack) > 0: team1.append(playerstack.pop()) # Stellt team2 aus dem STACK zusammen und loescht diese aus selbigen if len(playerstack) >= 3: team2.append(playerstack.pop(0)) team2.append(playerstack.pop(1)) team2.append(playerstack.pop(2)) print 'Team2:{}'.format(team2) # die verbleibenden player werden in die Team varieable uebernommen um nach nach einen neuen Shuffle mit weitern ergaenst zu werden else: while len(playerstack) > 0: team2.append(playerstack.pop()) #print player[rand] #print x #print playerstack ==== Skruppy ==== === Mathe === Die Anzahl der Kombinationen berechnet sich wie follgt. * ''%%n%%'' Leute stehen zur auswahl * ''%%k_1%%'' Leute in Team 1 * ''%%k_2%%'' Leute in Team 2 == Wenn k_1 == k_2 == \[ {n \choose k_1} \cdot {n - k_1 \choose k_2} = \frac{n!}{2 \cdot k_1! \cdot k_2! \cdot [n - k_1 - k_2]!} \] == Wenn k_1 != k_2 == \[{n \choose k_1} \cdot {n - k_1 \choose k_2} \cdot \frac{1}{2} = \frac{n!}{2 \cdot k_1! \cdot k_2! \cdot [n - k_1 - k_2]!} \] === Code === #!/usr/bin/python import copy import random def evSum(combination): i = 0 for a in combination: for b in a: i += b return i def evQuad(combination): i = 0 for a in combination: for b in a: i += 2**b return i available = range(1, 18) ratingFunctions = [evSum, evQuad] bestCombinations = [] bestRating = -10000 def combine(combination, available, start, position, team): global bestCombinations, bestRating if team == -1: ratingPoints = 0 for ratingFunction in ratingFunctions: ratingPoints += ratingFunction(combination) a = copy.deepcopy(combination) if ratingPoints > bestRating: bestCombinations = [a] bestRating = ratingPoints elif ratingPoints == bestRating: bestCombinations.append(a) elif position == -1: a = copy.copy(available) for el in combination[team]: a.remove(el) combine(combination, a, \ start=len(a) - 1, position=len(combination[team]) - 1, team=team - 1) else: for i in range(start, position - 1, -1): combination[team][position] = available[i] combine(combination, available, \ start=i - 1, position=position - 1, team=team) combine([[0,0,0], [0,0,0]], available, \ start=len(available) - 1, position=2, team=1) #for combination in bestCombinations: # print(combination) combCnt = len(bestCombinations) winner = random.randint(0, combCnt - 1) print("The winner from %i best combinations is:" % combCnt) print(bestCombinations[winner])