Stem-and-leaf plot: Difference between revisions

m
syntax highlighting fixup automation
(Added solution for Action!)
m (syntax highlighting fixup automation)
Line 16:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="11l">F leaf_plot(&x)
x.sort()
V i = x[0] I/ 10 - 1
Line 39:
]
 
leaf_plot(&data)</langsyntaxhighlight>
 
{{out}}
Line 61:
 
=={{header|ACL2}}==
<langsyntaxhighlight Lisplang="lisp">(defun insert (x xs)
(cond ((endp xs) (list x))
((> x (first xs))
Line 101:
(reverse (stem-and-leaf-bins (reverse (isort xs))
0
nil))))</langsyntaxhighlight>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
 
PROC Main()
Line 137:
PutE()
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Stem-and-leaf_plot.png Screenshot from Atari 8-bit computer]
Line 161:
[[GNAT]] used for sorting, could use any other sorting method.
Does not handle negative stems properly.
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 202:
end loop;
end stemleaf;
</syntaxhighlight>
</lang>
Output:
<pre>
Line 223:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">SetWorkingDir %A_ScriptDir%
#NoEnv
Data := "12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146"
Line 262:
Return ToReturn
}
</syntaxhighlight>
</lang>
Output:
<pre>
Line 282:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STEM-AND-LEAF_PLOT.AWK
#
Line 330:
function max(x,y) { return((x > y) ? x : y) }
function min(x,y) { return((x < y) ? x : y) }
</syntaxhighlight>
</lang>
<p>output:</p>
<pre>
Line 352:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(0, 0)
Line 388:
NEXT
PRINT
ENDPROC</langsyntaxhighlight>
Output:
<pre>
Line 409:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 447:
 
return 0;
}</langsyntaxhighlight>output<syntaxhighlight lang="text"> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
Line 461:
12 | 0 0 1 1 2 2 3 4 4 4 5 5 5 6 7 7 7 7 8 8
13 | 1 2 3 9
14 | 1 6</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 510:
}
}
}</langsyntaxhighlight>
<pre> 0 | 7 7
1 | 2 3 8 8
Line 528:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
Line 571:
std::cout << std::endl;
}
}</langsyntaxhighlight>
Output:
<pre> 0 | 7 7
Line 590:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">"Run the module `thestemandleafplot`."
shared void run() {
Line 613:
print("``formatInteger(i).padLeading(2)``| ``" ".join(stemsToLeaves[i] else [])``");
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(def data
[12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125
139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27
Line 658:
 
(stem-and-leaf data)
</syntaxhighlight>
</lang>
 
{{out}}
Line 680:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm;
 
void main() {
Line 697:
foreach (i; loHi[0]/10 .. loHi[1]/10 + 1)
writefln("%2d | %(%d %) ", i, histo.get(i, []).sort());
}</langsyntaxhighlight>
Output:
<pre> 0 | 7 7
Line 718:
{{trans|Ruby}}
{{works with|Elixir|1.3}}
<langsyntaxhighlight lang="elixir">defmodule Stem_and_leaf do
def plot(data, leaf_digits\\1) do
multiplier = Enum.reduce(1..leaf_digits, 1, fn _,acc -> acc*10 end)
Line 741:
data = ~w(12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146)
|> Enum.map(&String.to_integer(&1))
Stem_and_leaf.plot(data)</langsyntaxhighlight>
 
{{out}}
Line 763:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include sort.e
 
procedure leaf_plot(sequence s)
Line 790:
114, 34, 133, 45, 120, 30, 127, 31, 116, 146 }
 
leaf_plot(data)</langsyntaxhighlight>
 
Output:
Line 811:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
let data =
Line 837:
printfn "")
 
plotStemAndLeafs data</langsyntaxhighlight>
Output:
<pre>
Line 857:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs formatting grouping.extras io kernel math
prettyprint sequences sorting ;
 
Line 874:
120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31
116
} leaf-plot</langsyntaxhighlight>
{{out}}
<pre>
Line 895:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">create data
12 , 127 , 28 , 42 , 39 , 113 , 42 , 18 , 44 , 118 , 44 ,
37 , 113 , 124 , 37 , 48 , 127 , 36 , 29 , 31 , 125 , 139 ,
Line 929:
drop ;
 
plot</langsyntaxhighlight>
Output:
<pre>
Line 956:
Note that the MOD function can produce unexpected values for negative numbers, and, different computer/compiler/language combinations may produce different surprises. In this case, negative values produce negative remainder values, but the ABS function suppresses the surprise.
<syntaxhighlight lang="fortran">
<lang Fortran>
SUBROUTINE COMBSORT(A,N)
INTEGER A(*) !The array.
Line 1,017:
CALL TOPIARY(VALUES,121)
END
</syntaxhighlight>
</lang>
 
Output: (If additional spacing is desired, I2 format could be used, etc.)
Line 1,039:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">' version 22-06-2015
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Line 1,117:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 0 | 7 7
Line 1,136:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,176:
}
}
}</langsyntaxhighlight>
Output:
<pre>
Line 1,197:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List
import Control.Arrow
import Control.Monad
Line 1,219:
stems = map (flip(,)[]) $ uncurry enumFromTo $ minimum &&& maximum $ fst $ unzip stemLeaf
showStemLeaves f w (a,b) = f w (show a) ++ " |" ++ concatMap (f w. show) b
fb = length $ show $ maximum $ map abs ds</langsyntaxhighlight>
Output:
<pre>*Main> task nls
Line 1,240:
Or alternatively – aiming more for legibility than for economy or concision:
 
<langsyntaxhighlight lang="haskell">import Data.List (groupBy, intersperse, mapAccumL, sortBy)
import Data.Ord (comparing)
import Data.Function (on)
Line 1,312:
main :: IO ()
main = putStrLn $ unlines plotLines
</syntaxhighlight>
</lang>
{{Out}}
<pre> 0 | 7 7
Line 1,332:
=={{header|HicEst}}==
The dialog prompts for bitmap or a text image, and for the stem base. Data are read in from clipboard.
<langsyntaxhighlight HicEstlang="hicest">REAL :: workspace(1000), base=16
 
DLG(CHeckbox=bitmap, NameEdit=base, DNum, MIn=1, MAx=16) ! 1 <= stem base <= 16
Line 1,359:
WRITE(Format="i3, ':'") stem
ENDIF
ENDDO</langsyntaxhighlight>
Shown is the given example for bitmap=0 and base 16
<pre> 0 : 7 7 C D
Line 1,373:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang="unicon">procedure main(A)
prune := integer(\A[1]) | 10 # Boundary between leaf and stem
every put(data := [], integer(!&input))
Line 1,384:
}
write()
end</langsyntaxhighlight>
Sample output from data.
<pre>->stem <stem.data
Line 1,411:
=={{header|J}}==
'''Solution: (Tacit)'''
<langsyntaxhighlight lang="j">stem =: <.@(%&10)
leaf =: 10&|
stemleaf =: (stem@{. ; leaf)/.~ stem
Line 1,417:
expandLeaves=: (expandStems e. ])@[ #inv ]
 
showStemLeaf=: (":@,.@expandStems@[ ; ":&>@expandLeaves)&>/@(>@{. ; <@{:)@|:@stemleaf@/:~</langsyntaxhighlight>
 
'''Solution: (Explicit)'''
<langsyntaxhighlight lang="j">stemleafX=: monad define
leaves=. 10 | y
stems=. y <.@:% 10
Line 1,432:
xleaves=. (xstems e. stems) #inv leaves NB. expand leaves to match xstems
(": ,.xstems) ; ":&> xleaves
)</langsyntaxhighlight>
 
'''Example:'''
<langsyntaxhighlight lang="j"> nls =: ; <@(_&".);._2 noun define
12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125
139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105
Line 1,473:
 
(showStemLeaf -: showStemLeafX) nls NB. both solutions give same result
1</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Line 1,533:
printPlot(plot);
}
}</langsyntaxhighlight>
{{works with|Java|1.8+}}
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Line 1,619:
;
}
}</langsyntaxhighlight>
Output:
<pre>0 | [7, 7]
Line 1,641:
It turns out that HTML+CSS renders the plot quite attractively.
 
<langsyntaxhighlight lang="html4strict"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
Line 1,717:
 
</body>
</html></langsyntaxhighlight>
 
The output looks like:
Line 1,724:
 
===JavaScript ES6===
<langsyntaxhighlight lang="javascript">(() => {
// main :: IO String
const main = () => {
Line 1,934:
// MAIN ------------------------------------------------------------------
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre> 0 | 7 7
Line 1,953:
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def stem_and_leaf:
 
# align-right:
Line 1,970:
else [ $stem + 1, (.[1] + "\n\($stem+1|right) | \($d % 10)" )]
end )
| .[1] ;</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">def data:
[ 12,127,28,42,39,113, 42,18,44,118,44,37,113,124,37,48,127,36,29,31,
125,139,131,115,105,132,104,123,35,113,122,42,117,119,58,109,23,105,
Line 1,983:
data | stem_and_leaf
</syntaxhighlight>
</lang>
{{Out}}
<syntaxhighlight lang="sh">
<lang sh>
$ jq -n -r -f stem-and-leaf_plot.jq
0 | 77
Line 2,001:
12 | 00112234445556777788
13 | 1239
14 | 16</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 2,007:
 
This is a rather elaborate function that creates a string depicting a stem and leaf plot. Much of the elaboration is to handle the case of negative numbers that have a stem of 0. There is also a bit of work to allow for leaf sizes other than 1 (some power of 10).
<syntaxhighlight lang="julia">
<lang Julia>
function stemleaf{T<:Real}(a::Array{T,1}, leafsize=1)
ls = 10^int(log10(leafsize))
Line 2,033:
return slp
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
println("Using the Task's Test Data")
test = """12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29
Line 2,056:
println("Using: ", test)
println(stemleaf(test, 10))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,116:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun leafPlot(x: IntArray) {
Line 2,142:
)
leafPlot(data)
}</langsyntaxhighlight>
 
{{out}}
Line 2,164:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">data = { 12,127,28,42,39,113, 42,18,44,118,44,37,113,124,37,48,127,36,29,31,
125,139,131,115,105,132,104,123,35,113,122,42,117,119,58,109,23,105,
63,27,44,105,99,41,128,121,116,125,32,61,37,127,29,113,121,58,114,126,
Line 2,187:
print ""
end</langsyntaxhighlight>
Output:
<pre> 0 | 7 7
Line 2,206:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">StemPlot := proc( datatable::{rtable,list,algebraic} )
local i, j, k, tf, LeafStemTable, LeafStemIndices;
k:=0;
Line 2,254:
Y := [ 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123, 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105, 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58, 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43, 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118, 117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122, 109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114, 34, 133, 45, 120, 30, 127, 31, 116, 146];
 
StemPlot(Y);</langsyntaxhighlight>
 
<pre>0 | 7 7
Line 2,273:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">len[n_] := RealDigits[n][[2]]; padding = len[Max@ Quotient[inputdata, 10]];
For[i = Min@ Quotient[inputdata, 10],i <= Max@ Quotient[inputdata, 10], i++,
(Print[i, If[(padding - len[i]) > 0, (padding - len[i])*" " <> " |", " |"] ,
StringJoin[(" " <> #) & /@ Map[ToString, #]]])&@
Select[{Quotient[#, 10], Mod[#, 10]} & /@ Sort[inputdata],Part[#, 1] == i &][[;; , 2]]]</langsyntaxhighlight>
{{out}}
<pre>0 | 7 7
Line 2,297:
=={{header|MATLAB}} / {{header|Octave}}==
 
<langsyntaxhighlight Matlablang="matlab">function stem_and_leaf_plot(x,stem_unit,leaf_unit)
if nargin < 2, stem_unit = 10; end;
if nargin < 3,
Line 2,319:
x = [12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146];
 
stem_and_leaf_plot(x); </langsyntaxhighlight>
Output:
<pre>
Line 2,342:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">load(descrptive)$
 
data: [12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37, 48, 127,
Line 2,366:
12|00112234445556777788
13|1239
14|16</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import tables
import math
import strutils
Line 2,431:
var negativePlot = StemLeafPlot()
negativePlot.init(1, negativeData)
echo negativePlot</langsyntaxhighlight>
 
{{out}}
Line 2,465:
The definition of the function <code>unique</code> below can be omited if one uses the [http://code.google.com/p/ocaml-extlib/ extlib].
 
<langsyntaxhighlight lang="ocaml">let unique li =
let rec aux acc = function
| [] -> (List.rev acc)
Line 2,473:
else aux (x::acc) xs
in
aux [] li</langsyntaxhighlight>
 
<langsyntaxhighlight lang="ocaml">let data =
[ 12; 127; 28; 42; 39; 113; 42; 18; 44; 118; 44; 37; 113; 124; 37; 48;
127; 36; 29; 31; 125; 139; 131; 115; 105; 132; 104; 123; 35; 113; 122;
Line 2,498:
List.iter (Printf.printf " %d") vs;
print_newline()
) keys</langsyntaxhighlight>
 
we can output the same latex code than the Perl example replacing the main function as follow:
 
<langsyntaxhighlight lang="ocaml">let () =
print_endline "\
\\documentclass{report}
Line 2,519:
print_endline "\
\\end{tabular}
\\end{document}"</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my @data = sort {$a <=> $b} qw( 12 127 28 42 39 113 42 18 44 118 44
37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113
122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32
Line 2,547:
print " $leaf";
}
</syntaxhighlight>
</lang>
{{out}}
<pre> 0 | 7 7
Line 2,567:
=== LaTeX output ===
generating {{header|LaTeX}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl -w
 
my @data = sort {$a <=> $b} qw( 12 127 28 42 39 113 42 18 44 118 44
Line 2,609:
\end{tabular}
\end{document}
EOT</langsyntaxhighlight>
 
LaTeX output of the Perl program:
 
<langsyntaxhighlight lang="latex">\documentclass{report}
\usepackage{fullpage}
\begin{document}
Line 2,624:
14 & 1
\end{tabular}
\end{document}</langsyntaxhighlight>
 
The parameter to the <code>tabular</code> environment defines the columns of the table. “r” and “c” are right- and center-aligned columns, “|” is a vertical rule, and “<code>*{''count''}{''cols''}”</code> repeats a column definition ''count'' times.
Line 2,636:
=={{header|Phix}}==
{{trans|Euphoria}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">leaf_plot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 2,664:
<span style="color: #000000;">leaf_plot</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre style="font-size: 8px">
Line 2,685:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de *Data
12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36
29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119
Line 2,703:
(sort *Data) ) )
(for I (range (caar L) (car (last L)))
(prinl (align 3 I) " | " (glue " " (cdr (assoc I L)))) ) )</langsyntaxhighlight>
Output:
<pre> 0 | 7 7
Line 2,722:
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">
$Set = -split '12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146'
Line 2,733:
@( $Stem.ToString().PadLeft( 2, " " ), '|' ) + ( ( $Data | Where Stem -eq $Stem ).Leaf | Sort ) -join " "
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,757:
 
'''PureBasic Code'''
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole()
Dim MyList(120)
Define i, j, StemMax, StemMin
Line 2,799:
Data.i 27, 7, 68, 40, 31,115,124, 42,128, 52, 71,118,117, 38, 27,106, 33,117,116,111, 40,119, 47,105
Data.i 57,122,109,124,115, 43,120, 43, 27, 27, 18, 28, 48,125,107,114, 34,133, 45,120, 30,127, 31,116,146
EndDataSection</langsyntaxhighlight>
 
'''Output'''
Line 2,821:
 
Adjusting <code>Stem.leafdigits</code> allows you to modify how many digits of a value are used in the leaf, with the stem intervals adjusted accordingly.
<langsyntaxhighlight lang="python">from collections import namedtuple
from pprint import pprint as pp
from math import floor
Line 2,859:
 
if __name__ == '__main__':
print( stemplot(data0) )</langsyntaxhighlight>
 
'''Sample Output'''
Line 2,884:
 
Here is an another example using an OrderedDict and Counter
<langsyntaxhighlight lang="python">from collections import OrderedDict, Counter
 
x= [12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37, 48,
Line 2,903:
 
stemleaf(x)
</syntaxhighlight>
</lang>
 
Output :
Line 2,926:
 
Or, generalising a little to write a purely declarative function (in terms of '''groupby''' and '''reduce''') which takes stem and leaf accessor functions as its first arguments:
<langsyntaxhighlight lang="python">from itertools import (groupby)
from functools import (reduce)
 
Line 2,962:
 
 
main()</langsyntaxhighlight>
<pre> 0 | 7 7
1 | 2 3 8 8
Line 2,980:
=={{header|R}}==
 
<syntaxhighlight lang="r">
<lang R>
x <- c(12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37, 48, 127, 36,
29, 31, 125, 139, 131, 115, 105, 132, 104, 123, 35, 113, 122, 42, 117, 119, 58, 109,
Line 2,990:
 
stem(x)
</syntaxhighlight>
</lang>
 
Output :
Line 3,013:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
(define (show-stem+leaf data)
Line 3,024:
(newline)))
(show-stem+leaf (sequence->list (in-producer read eof)))
</syntaxhighlight>
</lang>
 
Sample run:
Line 3,051:
 
Handles negative stems properly.
<syntaxhighlight lang="raku" perl6line>my @data = <
12 127 28 42 39 113 42 18 44 118 44
37 113 124 37 48 127 36 29 31 125 139
Line 3,074:
my $leafs = %h{$stem} // [];
say $stem.fmt($stem_format), ' | ', ~$leafs.map: * % $stem_unit;
}</langsyntaxhighlight>
 
Output:<pre> 0 | 7 7
Line 3,099:
 
Also, all numbers that are processed are normalized. &nbsp; Using a &nbsp; ''sparse array'' &nbsp; bypasses the need for sorting.
<langsyntaxhighlight lang="rexx">/*REXX program displays a stem and leaf plot of any non-negative numbers [can include 0]*/
parse arg @ /* [↓] Not specified? Then use default*/
if @='' then @=12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 125 139,
Line 3,124:
end /*m*/
say right(k, w) '║' space($) /*display a line of stem─and─leaf plot.*/
end /*k*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; when using the (internal) defaults as input:
<pre>
Line 3,146:
===negative, zero, and positive numbers===
This REXX version also handles negative numbers.
<langsyntaxhighlight lang="rexx">/*REXX program displays a stem─and─leaf plot of any real numbers [can be: neg, 0, pos].*/
parse arg @ /*obtain optional arguments from the CL*/
if @='' then @='15 14 3 2 1 0 -1 -2 -3 -14 -15' /*Not specified? Then use the default.*/
Line 3,176:
end /*m*/
say right(k, w) '║' space($) /*display a line of stem─and─leaf plot.*/
end /*k*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; when using the (internal) defaults as input:
<pre>
Line 3,186:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Stem-and-leaf plot
 
Line 3,218:
next
see nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,241:
=={{header|Ruby}}==
This implementation will handle negative values.
<langsyntaxhighlight lang="ruby">class StemLeafPlot
def initialize(data, options = {})
opts = {:leaf_digits => 1}.merge(options)
Line 3,354:
27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116
111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34
133 45 120 30 127 31 116 146</langsyntaxhighlight>
 
{{out}}
Line 3,378:
 
'''Simple version'''
<langsyntaxhighlight lang="ruby">class StemLeafPlot
def initialize(data, leaf_digits=1)
@leaf_digits = leaf_digits
Line 3,406:
27 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116
111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34
133 45 120 30 127 31 116 146</langsyntaxhighlight>
{{out}}
<pre>
Line 3,428:
=={{header|Scala}}==
{{works with|Scala|2.8}}
<langsyntaxhighlight lang="scala">def stemAndLeaf(numbers: List[Int]) = {
val lineFormat = "%" + (numbers map (_.toString.length) max) + "d | %s"
val map = numbers groupBy (_ / 10)
Line 3,434:
println(lineFormat format (stem, map.getOrElse(stem, Nil) map (_ % 10) sortBy identity mkString " "))
}
}</langsyntaxhighlight>
 
Example:
Line 3,470:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: leafPlot (in var array integer: x) is func
Line 3,506:
begin
leafPlot(data);
end func;</langsyntaxhighlight>
 
Output:
Line 3,529:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var data = %i(
12 127 28 42 39 113 42 18 44 118 44
37 113 124 37 48 127 36 29 31 125 139
Line 3,552:
var leafs = (h{stem} \\ [])
say(stem_format % stem, ' | ', leafs.map { _ % stem_unit }.join(' '))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,574:
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">. clear all
. input x
12
Line 3,603:
12* | 00112234445556777788
13* | 1239
14* | 16</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
# How to process a single value, adding it to the table mapping stems to
Line 3,666:
48 125 107 114 34 133 45 120 30 127 31 116 146
}
printStemLeaf $data</langsyntaxhighlight>
Output:
<pre>
Line 3,687:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
digits=*
Line 3,717:
ENDLOOP
DO format
</syntaxhighlight>
</lang>
Output:
<pre style='height:30ex;overflow:scroll'>
Line 3,738:
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">Push 12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124
Push 0, 13 : Gosub _Read ' read 1st line of data
 
Line 3,801:
@(x) = Pop() ' get element from stack
Next
Return</langsyntaxhighlight>
Output:
<pre> 0 |7 7
Line 3,823:
 
=={{header|Ursala}}==
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 3,844:
#show+
 
main = stemleaf_plot data</langsyntaxhighlight>
Reading from right to left on the bottom line of the <code>stemleaf_plot</code> function, we
obtain the quotient and remainder of every datum divided by ten, partition by
Line 3,880:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var leafPlot = Fn.new { |x|
Line 3,907:
34, 133, 45, 120, 30, 127, 31, 116, 146
]
leafPlot.call(data)</langsyntaxhighlight>
 
{{out}}
Line 3,930:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">fcn leaf_plot(xs){
xs=xs.sort();
i := xs[0] / 10 - 1;
Line 3,952:
34, 133, 45, 120, 30, 127, 31, 116, 146 );
leaf_plot(data);</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits