Created
August 4, 2023 15:02
-
-
Save bemoty/9c9d510f9b28011284894242005af7ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.bemoty.Test; | |
import java.util.Arrays; | |
import java.util.stream.IntStream; | |
public class TennisTournament { | |
public static void main(String[] args) { | |
// 32 indices: [0, 31, 15, 16, 8, 23, 9, 24] | |
// 16 indices: [0, 15, 7, 8] | |
int[] input16 = IntStream.range(1, 5).toArray(); | |
int[] input32 = IntStream.range(1, 9).toArray(); | |
int[] input64 = IntStream.range(1, 17).toArray(); | |
int[] input128 = IntStream.range(1, 33).toArray(); | |
System.out.println(Arrays.toString(setSeeded(128, input128))); | |
System.out.println(Arrays.toString(setSeeded(64, input64))); | |
System.out.println(Arrays.toString(setSeeded(32, input32))); | |
System.out.println(Arrays.toString(setSeeded(16, input16))); | |
} | |
private static int[] setSeeded(int size, int[] input) { | |
if (input.length != size / 4) throw new IllegalArgumentException(); | |
int[] output = new int[size]; | |
output[0] = input[0]; | |
output[size - 1] = input[1]; | |
int[] rest = Arrays.copyOfRange(input, 2, input.length); | |
setCenter(output, rest); | |
return output; | |
} | |
private static int[] setCenter(int[] output, int[] input) { | |
if (input.length == 0) return output; | |
int index = output.length / 2; | |
output[index - 1] = input[0]; | |
output[index] = input.length > 1 ? input[1] : 0; | |
if (input.length > 2) { | |
int[] rest = Arrays.copyOfRange(input, 2, input.length); | |
set(output, setCenter(Arrays.copyOfRange(output, 0, output.length / 2), | |
IntStream.range(0, rest.length).filter((i) -> i % 2 != 0).map(i -> rest[i]).toArray()), 0); | |
set(output, setCenter(Arrays.copyOfRange(output, output.length / 2 + 1, output.length), | |
IntStream.range(0, rest.length).filter(i -> i % 2 == 0).map(i -> rest[i]).toArray()), index + 1); | |
} | |
return output; | |
} | |
private static void set(int[] output, int[] input, int offset) { | |
for (int i = offset; i < input.length + offset; i++) { | |
if (i >= output.length) break; | |
output[i] = input[i - offset]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment