Stem-and-leaf plot: Difference between revisions

Content added Content deleted
m (→‎{{header|Factor}}: remove errant whitespace)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 360: Line 360:
13 | 1 2 3 9
13 | 1 2 3 9
14 | 1 6</lang>
14 | 1 6</lang>

=={{header|C++}}==
<lang cpp>#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

const int dataset[] = {
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
};
const int datasize = sizeof(dataset) / sizeof(dataset[0]);

int main()
{
typedef std::pair<int,int> StemLeaf;
std::vector<StemLeaf> stemplot;

for (int i = 0; i < datasize; ++i)
{
stemplot.push_back(StemLeaf(dataset[i] / 10, dataset[i] % 10));
}

std::sort(stemplot.begin(), stemplot.end()); // order stem/leaf pairs

int lo = stemplot.front().first; // minimum stem value
int hi = stemplot.back().first; // maximum stem value

for (std::vector<StemLeaf>::iterator itr = stemplot.begin(); lo <= hi; ++lo)
{
std::cout << std::setw(2) << lo << " |"; // print stem

// while (there are more stems) and (stem is equal to lo)
for ( ; itr != stemplot.end() && itr->first == lo; ++itr)
{
std::cout << " " << itr->second; // print leaf
}

std::cout << std::endl;
}
}</lang>
Output:
<pre> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
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</pre>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Line 486: Line 424:
13 | 1 2 3 9
13 | 1 2 3 9
14 | 1 6</pre>
14 | 1 6</pre>

=={{header|C++}}==
<lang cpp>#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

const int dataset[] = {
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
};
const int datasize = sizeof(dataset) / sizeof(dataset[0]);

int main()
{
typedef std::pair<int,int> StemLeaf;
std::vector<StemLeaf> stemplot;

for (int i = 0; i < datasize; ++i)
{
stemplot.push_back(StemLeaf(dataset[i] / 10, dataset[i] % 10));
}

std::sort(stemplot.begin(), stemplot.end()); // order stem/leaf pairs

int lo = stemplot.front().first; // minimum stem value
int hi = stemplot.back().first; // maximum stem value

for (std::vector<StemLeaf>::iterator itr = stemplot.begin(); lo <= hi; ++lo)
{
std::cout << std::setw(2) << lo << " |"; // print stem

// while (there are more stems) and (stem is equal to lo)
for ( ; itr != stemplot.end() && itr->first == lo; ++itr)
{
std::cout << " " << itr->second; // print leaf
}

std::cout << std::endl;
}
}</lang>
Output:
<pre> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
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</pre>


=={{header|Ceylon}}==
=={{header|Ceylon}}==
Line 1,372: Line 1,372:
(showStemLeaf -: showStemLeafX) nls NB. both solutions give same result
(showStemLeaf -: showStemLeafX) nls NB. both solutions give same result
1</lang>
1</lang>

=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
Line 2,101: Line 2,102:
13 | 1 2 3 9
13 | 1 2 3 9
14 | 1 6</pre>
14 | 1 6</pre>



=={{header|Maple}}==
=={{header|Maple}}==
Line 2,532: Line 2,532:


and the output will be in <code>plot.pdf</code>. [http://switchb.org/kpreid/2009/12-24-rc-stemplot-perl-latex-output Output.]
and the output will be in <code>plot.pdf</code>. [http://switchb.org/kpreid/2009/12-24-rc-stemplot-perl-latex-output Output.]

=={{header|Perl 6}}==
{{trans|Perl}}

Handles negative stems properly.
<lang perl6>my @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
>».Int.sort;
my Int $stem_unit = 10;
my %h = @data.classify: * div $stem_unit;
my $range = [minmax] %h.keys».Int;
my $stem_format = "%{$range.min.chars max $range.max.chars}d";
for $range.list -> $stem {
my $leafs = %h{$stem} // [];
say $stem.fmt($stem_format), ' | ', ~$leafs.map: * % $stem_unit;
}</lang>

Output:<pre> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
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</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,758: Line 2,713:
13 | 1 2 3 9
13 | 1 2 3 9
14 | 1 6
14 | 1 6



=={{header|Python}}==
=={{header|Python}}==
Line 2,987: Line 2,941:
14| 1 6
14| 1 6
</pre>
</pre>

=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}

Handles negative stems properly.
<lang perl6>my @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
>».Int.sort;
my Int $stem_unit = 10;
my %h = @data.classify: * div $stem_unit;
my $range = [minmax] %h.keys».Int;
my $stem_format = "%{$range.min.chars max $range.max.chars}d";
for $range.list -> $stem {
my $leafs = %h{$stem} // [];
say $stem.fmt($stem_format), ' | ', ~$leafs.map: * % $stem_unit;
}</lang>

Output:<pre> 0 | 7 7
1 | 2 3 8 8
2 | 3 5 7 7 7 7 7 7 8 8 9 9
3 | 0 1 1 1 1 2 3 4 5 6 7 7 7 8 9
4 | 0 0 1 2 2 2 2 3 3 3 4 4 4 5 6 7 8 8
5 | 2 3 7 8 8
6 | 1 3 8
7 | 1
8 |
9 | 6 9
10 | 4 5 5 5 5 6 7 9 9 9
11 | 1 3 3 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 9 9
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</pre>


=={{header|REXX}}==
=={{header|REXX}}==
Line 3,717: Line 3,717:
0 OK, 0:2037
0 OK, 0:2037
</pre>
</pre>

=={{header|Ursala}}==
=={{header|Ursala}}==
<lang Ursala>#import std
<lang Ursala>#import std