Last active
December 14, 2016 19:44
-
-
Save nakaly/7d2d9de7c9a61788260a6c5dbdf6993b to your computer and use it in GitHub Desktop.
MakePairProgramingGroup.java
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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
public class MakePairProgramingGroup { | |
public static void main(String... args) { | |
List<String> allMembers = Arrays.asList("1", "2", "3", "4", "5", "6"); | |
List<List<String>> allPairs = makePairs(allMembers); | |
allPairs.stream().forEach(pair -> System.out.println("pair:" + pair.stream().collect(Collectors.joining("&")))); | |
List<List<List<String>>> allGroups = new ArrayList<>(); | |
makeGroup(allPairs, new ArrayList<>(), allGroups, allMembers.size() / 2); | |
List<List<List<String>>> selectedGroups = selectGroup(allGroups); | |
selectedGroups.stream().forEach( | |
group -> System.out.println("selectedGroup:" + group.stream().map(pair -> pair.stream().collect(Collectors.joining("&"))).collect(Collectors.joining(",")))); | |
} | |
static private <T> List<List<T>> makePairs(List<T> memberNames) { | |
List<List<T>> allPairs = new ArrayList<>(); | |
for (int i = 0; i < memberNames.size() - 1; i++) { | |
for (int j = i + 1; j < memberNames.size(); j++) { | |
List<T> pair = new ArrayList<>(); | |
pair.add(memberNames.get(i)); | |
pair.add(memberNames.get(j)); | |
allPairs.add(pair); | |
} | |
} | |
return allPairs; | |
} | |
static private <T> void makeGroup(List<List<T>> pairs, List<List<T>> currentGroup, List<List<List<T>>> groups, int maxGroupSize) { | |
if (currentGroup.size() + 1 == maxGroupSize) { | |
pairs.forEach(pair -> { | |
List<List<T>> group = new ArrayList<>(currentGroup); | |
group.add(pair); | |
List<T> groupMembers = group.stream().flatMap(Collection::stream).collect(Collectors.toList()); | |
Set<T> distinctMembers = new HashSet<>(groupMembers); | |
if (groupMembers.size() == distinctMembers.size()) { | |
groups.add(group); | |
} | |
}); | |
} else { | |
for (int i = 0; i < pairs.size() - (maxGroupSize - currentGroup.size()); i++) { | |
List<List<T>> group = new ArrayList<>(currentGroup); | |
group.add(pairs.get(i)); | |
makeGroup(pairs.subList(1, pairs.size()), group, groups, maxGroupSize); | |
} | |
} | |
} | |
static private <T> List<List<List<T>>> selectGroup(List<List<List<T>>> groups) { | |
List<List<List<T>>> selectedGroups = new ArrayList<>(); | |
groups.forEach(group -> { | |
if (selectedGroups.stream().flatMap(g -> g.stream()).allMatch(pair -> !group.contains(pair))) { | |
selectedGroups.add(group); | |
} | |
}); | |
return selectedGroups; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result