Converting simple Python code into a Java method -


i found code snippet gives me want in automated tournament bracket generator: array.

there issue. not read nor write python, proficient (enough) in java. don't know if bad stack overflow etiquette, asking assist in conversion of code java method.

def cbseed( n ):     #returns list of n in standard tournament seed order     #note n need not power of 2 - 'byes' returned 0     ol = [1]     in range( int(ceil( log(n) / log(2) ) )):         l = 2*len(ol) + 1         ol = [e if e <= n else 0 s in [[el, l-el] el in ol] e in s]     return ol 

which returns nice

2 [1, 2] #seed 1 plays seed 2 3 [1, 0, 2, 3] #seed 1 gets 'by' game , 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 on , forth till 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 array kind of increments in twos, every 2 being 1 game.

a direct translation 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));      (int = 0; < max; i++) {         int l = 2 * ol.size() + 1;          list<integer> newol = new arraylist<integer>(ol.size() * 2);         (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 can see java more verbose :)


you can see produces identical results python function:

for (int = 2; < 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] 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -