-1

Okay so what I need to do is as follows:

I create a game lobby and 8 players join and their wins are as follows

Player1 = 10 wins
Player2 = 150 wins
Player3 = 100 wins
Player4 = 23 wins
Player5 = 76 wins
Player6 = 92 wins
Player7 = 1088 wins
Player8 = 0 wins

Now that we have the players and their wins the teams need to be split into two groups so that the teams are fair for example it would be unfair for the players with:

0, 10, 23, 76 wins to be

VS

92, 150, 100 and 1088 wins.

I've looked all over and can't find anything that does this any guidance would be great.

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
  • 4
    SO is about fixing _your_ Code - not implementing your ideas. Please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic) again and if you have questions provide your code as [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Patrick Artner Nov 18 '18 at 17:33
  • Try to get a "fair" grouping by hand: [7,8,4,1] <> [2,3,6,5] - then think about you can achieve this with coding. If thats what you consider "fair" you got an algorythm going - code it, come back if you get specific problems. – Patrick Artner Nov 18 '18 at 17:37
  • Hi, interesting question but I think you need to show what you have tried so far (code) so someone could help you out. – Ermac Nov 18 '18 at 19:55

1 Answers1

-1

Let's say we have each player and score in a dict like this

players = {player1:0, player2:15, ...}

You could probably sort players into two groups, say team1 and team2 by iterating over each player score and sorting that way

 team1 = {}
 team2 = {}
 for player, score in players.items():
      # Do something there to split over the two groups
      # This will depend on what you consider "fair"
      # In the end, you should be able to get your game set up. Good luck! 
      pass

If you just wat to sort "one then the other", you could probably use something like

players = {"player1":0, "player2":1000, "player3":500, "player4":1582}
team1 = {}
team2={}

i = 0
for key in sorted(players, key=lambda p: players[p]):
     i += 1
     if i % 2 == 0:
          team2[key] = players[key]
     else:
          team1[key] = players[key]

print(team1)
print(team2) 
WayToDoor
  • 1,180
  • 9
  • 24
  • Please explain why you are downvoting – WayToDoor Nov 18 '18 at 17:37
  • Because your code does nothing to solve the question - you provide some noise-code and the complete logic is still not solved. Beside that - this Questions as currently stated is far too broad to be solved by any answer - too much is unclear and would lead to forht and back because of ill-defined-ness. Iterating over sorted dict entries also makes not much sense - probably better to use a sorted queue and take from front end/back end depending if you need a low scored or high scored player until empty. – Patrick Artner Nov 18 '18 at 17:38
  • @PatrickArtner This is why I setup the OP a starting point, but didn't implement the fair-ness algorithm. They are now free to iterate over the players and find the "fair" grouping. I can't guess for them if they want the avrerage to be close, one winner then the next to alternate between groups, ... – WayToDoor Nov 18 '18 at 17:40
  • I think the best way to do it is first sort them by ascending order and then put the 1st and second value in a team iterating through the list until they are all sorted, not sure how i would achieve this though – Poppadomus Nov 18 '18 at 17:44
  • @Poppadomus do you know about `list`'s? if not: https://docs.python.org/3/tutorial/introduction.html#lists - if so, sort it, then take from front and end and populate 2 more lists that are your teams. You showed no code at all so its difficult to help you because nobody can see how advanced your python skills are. You can read here: https://stackoverflow.com/questions/19199984/sort-a-list-in-python how to sort something. If you use tuples `[(4,"Olaf"),(6,"Bjoern"),(2,"Astrid")]` and sort them they'll get sorted by the first value (and if draw-on the name) so you have your data representation – Patrick Artner Nov 18 '18 at 17:51
  • @Poppadomus I added a suggestion of what you could do then. You should improve that based n your data structures – WayToDoor Nov 18 '18 at 18:02
  • @WayToDoor that doesn't really help me though as it doesn't run in python 3? – Poppadomus Nov 18 '18 at 18:18
  • Fixed. It was a misplaced comma – WayToDoor Nov 18 '18 at 18:19