Created
November 17, 2023 14:02
-
-
Save Querela/d8da1518b68c083d2eebe3c001544d61 to your computer and use it in GitHub Desktop.
Split String on markers and transform to hit ranges
This file contains hidden or 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
String value = "fdkslaöfjs fdskla öfjs{{{{}}}}fjdslkajfs{{{{}}}} fdl söaj {{{{}}}}abc{{{{}}}} fdjksö jk"; | |
String marker = "{{{{}}}}"; | |
String[] parts = value.split(Pattern.quote(marker)); | |
List<Integer> lengths = List.of(parts).stream().map(String::length).collect(Collectors.toList()); | |
int total = 0; | |
List<Integer> lengths_agg = List.of(parts).stream().map(String::length).map(l -> {total = total + l; return total;}).collect(Collectors.toList()); | |
// https://stackoverflow.com/a/30072617/9360161 | |
public static <T> Stream<List<T>> batches(List<T> source, int length) { | |
if (length <= 0) { throw new IllegalArgumentException("length = " + length); } | |
int size = source.size(); | |
if (size <= 0) { return Stream.empty(); } | |
int fullChunks = (size - 1) / length; | |
return IntStream.range(0, fullChunks + 1).mapToObj(n -> source.subList(n * length, n == fullChunks ? size : (n + 1) * length)); | |
} | |
List<List<Integer>> groups = batches(lens, 2).collect(Collectors.toList()); | |
groups.remove(groups.size() - 1); | |
// inclusive indices | |
List<List<Integer>> ranges = groups.stream().map(l -> List.of(l.get(0) + 1, l.get(1))).collect(Collectors.toList()); | |
List<Integer[]> ranges = groups.stream().map(l -> new int[] {l.get(0) + 1, l.get(1)}).collect(Collectors.toList()); | |
// lengths | |
List<Integer[]> ranges = groups.stream().map(l -> new int[] {l.get(0) + 1, l.get(1) - l.get(0)}).collect(Collectors.toList()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment