1

I made a function 'Match' which takes two country and return a winner according to my algorithm.
EX)

def Match(England, Brazil) ----> England, 
def Match(Mexico, France) ---> France  

I need to write function Winner which takes list of 2^n nations and run a tournament and find a winner.
EX)

def Winner([England, Brazil, Mexico, France]) ---> France 

England---Brazil,        Mexico---France  (Semifinal)

England---France (final)

France (winner) Return value

The length of nation list differs and I cannot make a winner choosing algorithm. The match-up is It would be great if the code uses while loop rather than for loop ^^.

태주강
  • 23
  • 4

1 Answers1

0

Your tournament method should make matches between consecutive pairs of players, for ["a", "b", "c", "d", "e", "f", "g", "h"] it should be a/b, c/d, e/f and g/h

You can achieve these with slicing and zip

  • countries[::2] takes 1 on 2 so ["a", "c", "e", "g"]

  • countries[1::2] same but starting at 1 so ["b", "d", "f", "h"]

  • zip pairs these 2 lists to create pairs of opponents

Keep the winner of each match, and call tournament recursivly with the next round of players which contains half of players

# FOR DEMO PURPOSER
def match(country_a, country_b):
    return random.choice([country_a, country_b])

def tournament(countries):
    n = len(countries)
    if not ((n & (n - 1) == 0) and n != 0):
        raise Exception("Size isn't power of 2")

    if n == 2:
        return match(*countries)

    next_round = []
    for player1, player2 in zip(countries[::2], countries[1::2]):
        winner = match(player1, player2)
        next_round.append(winner)

    return tournament(next_round)

Using list-comprehension the for-loop and return can be replaced with

return tournament([match(p1, p2) for p1, p2 in zip(countries[::2], countries[1::2])])

Improvement full code

After some time and discussion with the OP here's a major improvement of the full code with the major rules :

  • don't call the exact same method in a loop, do it outside
  • don't call a method again and again if it does the same, store the data somewhere
azro
  • 53,056
  • 7
  • 34
  • 70
  • It worked and I finally understand the code. – 태주강 Apr 11 '21 at 08:36
  • However, I was working on a tutor program and popped a Time Limit Exceeded problem... HaHa... – 태주강 Apr 11 '21 at 08:37
  • 1
    @태주강 a time limit ? I can't see how to improve that, it does the minimum of matches possible, the only way to gain time is on `match` method, can you edit your initial post and add your `match` code ? You can think about voting up that answer, and accepting it (green tick on left) it if satisfies you ;) – azro Apr 11 '21 at 08:42
  • I actually had many codes before the 'tournament' method....Whole code is over 100 length and just like you said, the previous code occurs the problem. – 태주강 Apr 11 '21 at 08:47
  • I will try my best to simplify my code to shorter the operation time – 태주강 Apr 11 '21 at 08:48
  • I don't know what you mean at the last sentence. You mean posting my code here? – 태주강 Apr 11 '21 at 08:53
  • 1
    @태주강 As I asked already 2 times : it would be nice to post YOUR `match` code INTO your initial post up there yes, you may click 'edit' on your question post and add it (see my comment under your post, it also contains the 'edit' link for YOUR post) – azro Apr 11 '21 at 08:54
  • Got it. I was just little bit sorry(?) to post my long code and you to read it all... – 태주강 Apr 11 '21 at 09:02