Tournament bracket placement algorithm

前端 未结 10 630
生来不讨喜
生来不讨喜 2020-12-02 09:37

Given a list of opponent seeds (for example seeds 1 to 16), I\'m trying to write an algorithm that will result in the top seed playing the lowest seed in that round, the 2nd

10条回答
  •  庸人自扰
    2020-12-02 09:58

    # Here's one in python - it uses nested list comprehension to be succinct:
    
    from math import log, ceil
    
    def seed( n ):
        """ returns list of n in standard tournament seed order
    
        Note that n need not be a power of 2 - 'byes' are returned as zero
        """
    
        ol = [1]
    
        for i in range( ceil( log(n) / log(2) ) ):
    
            l = 2*len(ol) + 1
    
            ol = [e if e <= n else 0 for s in [[el, l-el] for el in ol] for e in s]
    
        return ol
    

提交回复
热议问题