4

I found a code snippet that gives me exactly what I want in a automated tournament bracket generator: AN ARRAY.

There is an issue. I do not read nor write python, but I am proficient (enough) in Java. I don't know if this is bad stack overflow etiquette, but I am asking for someone to assist in the conversion of this code to a Java method.

def CBseed( 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( int(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

Which returns a nice

2 [1, 2] #seed 1 plays seed 2
3 [1, 0, 2, 3] #seed 1 gets a 'by' game and seed 2 plays seed 3
4 [1, 4, 2, 3] #ETC.
5 [1, 0, 4, 5, 2, 0, 3, 0]
6 [1, 0, 4, 5, 2, 0, 3, 6]
7 [1, 0, 4, 5, 2, 7, 3, 6]
8 [1, 8, 4, 5, 2, 7, 3, 6]
#and so on and so forth till this
31 [1, 0, 16, 17, 8, 25, 9, 24, 4, 29, 13, 20, 5, 28, 12, 21, 2, 31, 15, 18, 7, 26, 10, 23, 3, 30, 14, 19, 6, 27, 11, 22]
32 [1, 32, 16, 17, 8, 25, 9, 24, 4, 29, 13, 20, 5, 28, 12, 21, 2, 31, 15, 18, 7, 26, 10, 23, 3, 30, 14, 19, 6, 27, 11, 22]

So the array kind of increments in twos, with every two being one game.

7
  • If you can code in Java, then why can't you read this Python? It contains assignment, for in loop, range(), int() cast, log(), len, if else, and a return. Commented Aug 12, 2013 at 21:15
  • 4
    I'm guessing the second to last line is throwing him off. Rest is pretty easy to understand. Commented Aug 12, 2013 at 21:16
  • 3
    @BLaZuRE Well, [e if e <= n else 0 for s in [[el, l-el] for el in ol] for e in s] is somewhat convoluted for someone who's not fluent in Python. Commented Aug 12, 2013 at 21:17
  • 1
    @BLaZuRE I have no idea how to read Python. Although the power in this little of code makes me want to read up on it Commented Aug 12, 2013 at 21:20
  • @MohammadS. Yes, exactly Commented Aug 12, 2013 at 21:21

1 Answer 1

5

A direct translation would be something like:

public static List<Integer> cbSeed(int n) {
    List<Integer> ol = new ArrayList<Integer>();
    ol.add(1);

    int max = (int) Math.ceil(Math.log(n) / Math.log(2));

    for (int i = 0; i < max; i++) {
        int l = 2 * ol.size() + 1;

        List<Integer> newOl = new ArrayList<Integer>(ol.size() * 2);
        for (int el : ol) {
            int e = el;
            newOl.add(e <= n ? e : 0);

            e = l - el;
            newOl.add(e <= n ? e : 0);
        }

        ol = newOl;
    }

    return ol;
}

As you can see Java is more verbose :)


You can see that this produces identical results as the Python function:

for (int i = 2; i < 9; i++)
    System.out.println(i + "\t" + cbSeed(i));
2   [1, 2]
3   [1, 0, 2, 3]
4   [1, 4, 2, 3]
5   [1, 0, 4, 5, 2, 0, 3, 0]
6   [1, 0, 4, 5, 2, 0, 3, 6]
7   [1, 0, 4, 5, 2, 7, 3, 6]
8   [1, 8, 4, 5, 2, 7, 3, 6]
Sign up to request clarification or add additional context in comments.

2 Comments

Aw, you beat me to it. Your answer looks nicer so +1. This link may help if anyone wants to understand the for in in Python: docs.python.org/2/tutorial/…
Thank you very much. This is exactly what I was looking for! I should look into python as well!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.