Last active
September 19, 2020 08:16
-
-
Save onacit/fd0b04b007e395a7fcfc5066898d422d to your computer and use it in GitHub Desktop.
The STEM-AND-LEAF diagram from The Cartoon Guide to Statistics by Larry Gonick & Woollcott Smith
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
package p_fd0b04b007e395a7fcfc5066898d422d; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.SortedMap; | |
import java.util.TreeMap; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
import static java.util.Objects.requireNonNull; | |
public class StemAndLeafDiagram { | |
private static final List<Integer> WEIGHTS_MALE_57 = Arrays.asList( | |
140, 145, 160, 190, 155, 165, 150, 190, 195, 138, 160, 155, 153, 145, 170, 175, 175, 170, 180, | |
135, 170, 157, 130, 185, 190, 155, 170, 155, 215, 150, 145, 155, 155, 150, 155, 150, 180, 160, | |
135, 160, 130, 155, 150, 148, 155, 150, 140, 180, 190, 145, 150, 164, 140, 142, 136, 123, 155 | |
); | |
private static final List<Integer> WEIGHTS_FEMALE_35 = Arrays.asList( | |
140, 120, 130, 138, 121, 125, 116, 145, 150, 112, 125, 130, 120, 130, 131, 120, 118, 125, 135, | |
125, 118, 122, 115, 102, 115, 150, 110, 116, 108, 95, 125, 133, 110, 150, 108 | |
); | |
private static SortedMap<Integer, List<Integer>> collectStemAndLeaf(final Stream<Integer> weights) { | |
return weights.collect(Collectors.groupingBy( | |
w -> w / 10, | |
TreeMap::new, | |
Collectors.mapping( | |
v -> v % 10, | |
Collectors.collectingAndThen( | |
Collectors.toList(), | |
l -> { | |
l.sort(Comparator.naturalOrder()); | |
return l; | |
} | |
) | |
) | |
)); | |
} | |
private static void printStemAndLeafDiagram(final List<Integer> weights) { | |
if (requireNonNull(weights, "weights is null").isEmpty()) { | |
throw new IllegalArgumentException("weights is empty"); | |
} | |
final Map<Integer, List<Integer>> map = collectStemAndLeaf(weights.stream()); | |
{ | |
final int min = weights.stream().mapToInt(Integer::intValue).map(w -> w / 10).min().getAsInt(); | |
final int max = weights.stream().mapToInt(Integer::intValue).map(w -> w / 10).max().getAsInt(); | |
IntStream.rangeClosed(min, max).forEach(v -> { | |
if (!map.containsKey(v)) { | |
map.put(v, null); | |
} | |
}); | |
} | |
map.forEach((k, v) -> { | |
System.out.printf("%1$2d_: ", k); | |
if (v != null) { | |
v.forEach(System.out::print); | |
} | |
System.out.println(); | |
}); | |
} | |
public static void main(final String... args) { | |
System.out.println("------------------------------------------------------------------------------------ MALE"); | |
printStemAndLeafDiagram(WEIGHTS_MALE_57); | |
System.out.println("---------------------------------------------------------------------------------- FEMALE"); | |
printStemAndLeafDiagram(WEIGHTS_FEMALE_35); | |
} | |
} |
Author
onacit
commented
Sep 19, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment