MathPHP\Statistics\Distribution::stemAndLeafPlot PHP Метод

stemAndLeafPlot() публичный статический Метод

https://en.wikipedia.org/wiki/Stem-and-leaf_display Returns an array with the keys as the stems, and the values are arrays containing the leaves. Optional parameter to print the stem and leaf plot. Given input array: [ 44 46 47 49 63 64 66 68 68 72 72 75 76 81 84 88 106 ] Prints: 4 | 4 6 7 9 5 | 6 | 3 4 6 8 8 7 | 2 2 5 6 8 | 1 4 8 9 | 10 | 6
public static stemAndLeafPlot ( array $values, boolean $print = false ) : array
$values array
$print boolean Optional setting to print the distribution
Результат array keys are the stems, values are the leaves
    public static function stemAndLeafPlot(array $values, bool $print = false) : array
    {
        // Split each value into stem and leaf
        sort($values);
        $plot = array();
        foreach ($values as $value) {
            $stem = $value / 10;
            $leaf = $value % 10;
            if (!isset($plot[$stem])) {
                $plot[$stem] = array();
            }
            $plot[$stem][] = $leaf;
        }
        // Fill in any empty keys in the distribution we had no stem/leaves for
        $min = min(array_keys($plot));
        $max = max(array_keys($plot));
        for ($stem = $min; $stem <= $max; $stem++) {
            if (!isset($plot[$stem])) {
                $plot[$stem] = array();
            }
        }
        ksort($plot);
        // Optionally print the stem and leaf plot
        if ($print === true) {
            $length = max(array_map(function ($stem) {
                return strlen($stem);
            }, array_keys($plot)));
            foreach ($plot as $stem => $leaves) {
                printf("%{$length}d | %s\n", $stem, implode(' ', $leaves));
            }
        }
        return $plot;
    }