Skip to content

Instantly share code, notes, and snippets.

@ConnorBaker
Created November 3, 2019 01:41
Show Gist options
  • Select an option

  • Save ConnorBaker/455e10121a35a25ae7e3500ad312fa30 to your computer and use it in GitHub Desktop.

Select an option

Save ConnorBaker/455e10121a35a25ae7e3500ad312fa30 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import static java.util.stream.Collectors.toList;
public class Main {
public static List<Long> binaryNialpdromes(int n) {
final var ret = new ArrayList<Long>(n * (n + 1) / 2);
for (int i = 0; i <= n; i++) {
for (int j = 0; j < i; j++) {
ret.add((long) (Math.pow(2.0, i) - Math.pow(2.0, j)));
}
}
return ret;
}
public static List<List<Integer>> contiguousSubsequences(List<Integer> list) {
return binaryNialpdromes(list.size())
.stream()
.map(n -> BitSet.valueOf(new long[]{n}))
.map(BitSet::stream)
.map(s -> s.boxed().map(list::get).collect(toList()))
.collect(toList());
}
public static List<List<Integer>> powerSet(List<Integer> list) {
return LongStream
.range(0, (int) Math.pow(2.0, list.size()))
.boxed()
.map(n -> BitSet.valueOf(new long[]{n}))
.map(BitSet::stream)
.map(s -> s.boxed().map(list::get).collect(toList()))
.collect(toList());
}
public static void main(String[] args) {
System.out.println("Hello world!");
final var list = IntStream.range(0, 7).boxed().collect(toList());
System.out.println("Length of power set of " + list + ":");
System.out.println(powerSet(list));
System.out.println("Length of set of all subsequences of " + list + ":");
System.out.println(contiguousSubsequences(list));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment