Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Stochastically generate two balanced teams
- import random
- from datetime import datetime
- def generateFinal(playerScores: list, noOfInterations: int):
- random.seed(datetime.now())
- if (len(playerScores) % 2 == 1):
- print("Odd number of players")
- else:
- A = playerScores[:len(playerScores) // 2]
- B = playerScores[len(playerScores) // 2:]
- lowestDif = abs(sum(A) - sum(B))
- for i in range(0, noOfInterations):
- randIndexA = random.randint(0, len(A) - 1)
- randIndexB = random.randint(0, len(B) - 1)
- sumA = sum(A)
- sumB = sum(B)
- newSumA = sumA - A[randIndexA] + B[randIndexB]
- newSumB = sumB - B[randIndexB] + A[randIndexA]
- newDif = abs(newSumA - newSumB)
- if (newDif < lowestDif):
- temp = A[randIndexA]
- A[randIndexA] = B[randIndexB]
- B[randIndexB] = temp
- lowestDif = newDif
- print("Initial: " + str(playerScores) + "\nA: " + str(A) + "\nB: " + str(B) + "\nDif: " + str(lowestDif))
- def generateRandomArray(arrLen: int, minPar: int, maxPar: int):
- random.seed(datetime.now())
- arr = []
- if ((maxPar - minPar) % 2 == 1):
- maxPar += 1
- for i in range(0, arrLen):
- arr.append(random.randint(minPar, maxPar))
- return arr
- lenarr = 100
- #generateFinal(generateRandomArray(lenarr, 23, 1031), lenarr ** 2)
- generateFinal([3,5,6 ,7,22,67,234,251,4,1], 9**5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement