Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
40
Task/Stem-and-leaf-plot/Ada/stem-and-leaf-plot.adb
Normal file
40
Task/Stem-and-leaf-plot/Ada/stem-and-leaf-plot.adb
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
||||
with Gnat.Heap_Sort_G;
|
||||
procedure stemleaf is
|
||||
data : array(Natural Range <>) of Integer := (
|
||||
0,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); -- Position 0 is used for storage during sorting, initialized as 0
|
||||
|
||||
procedure Move (from, to : in Natural) is
|
||||
begin data(to) := data(from);
|
||||
end Move;
|
||||
|
||||
function Cmp (p1, p2 : Natural) return Boolean is
|
||||
begin return data(p1)<data(p2);
|
||||
end Cmp;
|
||||
|
||||
package Sorty is new GNAT.Heap_Sort_G(Move,Cmp);
|
||||
min,max,p,stemw: Integer;
|
||||
begin
|
||||
Sorty.Sort(data'Last);
|
||||
min := data(1);
|
||||
max := data(data'Last);
|
||||
stemw := Integer'Image(max)'Length;
|
||||
p := 1;
|
||||
for stem in min/10..max/10 loop
|
||||
put(stem,Width=>stemw); put(" |");
|
||||
Leaf_Loop:
|
||||
while data(p)/10=stem loop
|
||||
put(" "); put(data(p) mod 10,Width=>1);
|
||||
exit Leaf_loop when p=data'Last;
|
||||
p := p+1;
|
||||
end loop Leaf_Loop;
|
||||
new_line;
|
||||
end loop;
|
||||
end stemleaf;
|
||||
131
Task/Stem-and-leaf-plot/Erlang/stem-and-leaf-plot.erl
Normal file
131
Task/Stem-and-leaf-plot/Erlang/stem-and-leaf-plot.erl
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
%%% ============================================================
|
||||
%%% Stem-and-Leaf Plot in Erlang
|
||||
%%% ============================================================
|
||||
%%% - Pure functions
|
||||
%%% - No mutation
|
||||
%%% - Deterministic transformations
|
||||
%%% - Explicit functional pipeline
|
||||
%%% ============================================================
|
||||
|
||||
-module(stem_leaf).
|
||||
-export([main/0]).
|
||||
|
||||
%%% ============================================================
|
||||
%%% 1. Mathematical Helpers (Pure Functions)
|
||||
%%% ============================================================
|
||||
|
||||
%% expand_number/1
|
||||
%% Conceptually expands single digit numbers to two-digit
|
||||
%% (e.g., 7 -> 07). Numerically unchanged, but included
|
||||
%% for semantic clarity.
|
||||
expand_number(N) ->
|
||||
N.
|
||||
|
||||
%% stem/1
|
||||
%% Returns the stem (all but the last digit).
|
||||
%% Example: 127 -> 12
|
||||
stem(N) ->
|
||||
N div 10.
|
||||
|
||||
%% leaf/1
|
||||
%% Returns the leaf (last digit).
|
||||
%% Example: 127 -> 7
|
||||
leaf(N) ->
|
||||
N rem 10.
|
||||
|
||||
%% range/2
|
||||
%% Returns a list of integers from From to To (inclusive).
|
||||
%% Used to guarantee stems 0..14 are always present.
|
||||
range(From, To) when From =< To ->
|
||||
lists:seq(From, To).
|
||||
|
||||
%%% ============================================================
|
||||
%%% 2. Data Transformation Pipeline
|
||||
%%% ============================================================
|
||||
|
||||
%% build_stem_leaf/1
|
||||
%% Takes raw data and returns a list of:
|
||||
%% [{Stem, [SortedLeaves]}, ...]
|
||||
%%
|
||||
%% Steps:
|
||||
%% 1. Expand numbers
|
||||
%% 2. Sort entire dataset
|
||||
%% 3. Generate all stems 0..14
|
||||
%% 4. Collect and sort leaves for each stem
|
||||
build_stem_leaf(Data) ->
|
||||
Expanded = [expand_number(N) || N <- Data],
|
||||
Sorted = lists:sort(Expanded),
|
||||
Stems = range(0, 14),
|
||||
[{S, sorted_leaves(S, Sorted)} || S <- Stems].
|
||||
|
||||
%% sorted_leaves/2
|
||||
%% Filters all numbers matching a given stem
|
||||
%% Extracts leaves
|
||||
%% Sorts leaves
|
||||
sorted_leaves(StemValue, Data) ->
|
||||
Leaves =
|
||||
[leaf(N) || N <- Data, stem(N) =:= StemValue],
|
||||
lists:sort(Leaves).
|
||||
|
||||
%%% ============================================================
|
||||
%%% 3. Formatting Functions (Pure Rendering Layer)
|
||||
%%% ============================================================
|
||||
|
||||
%% format_stem/1
|
||||
%% Formats stem as two digits (leading zero if needed).
|
||||
%% Example: 7 -> "07"
|
||||
format_stem(S) ->
|
||||
io_lib:format("~2..0B", [S]).
|
||||
|
||||
%% format_leaves/1
|
||||
%% Converts list of integers into space-separated string.
|
||||
%% Example: [1,3,7] -> "1 3 7"
|
||||
format_leaves([]) ->
|
||||
"";
|
||||
format_leaves(Leaves) ->
|
||||
string:join([integer_to_list(L) || L <- Leaves], " ").
|
||||
|
||||
%% render_line/1
|
||||
%% Converts {Stem, Leaves} into formatted line.
|
||||
%% Example:
|
||||
%% {12, [3,4,7]} -> "12 | 3 4 7"
|
||||
render_line({StemValue, Leaves}) ->
|
||||
lists:flatten(
|
||||
io_lib:format("~s | ~s",
|
||||
[format_stem(StemValue), format_leaves(Leaves)])
|
||||
).
|
||||
|
||||
%% render_plot/1
|
||||
%% Converts full stem-leaf structure into multi-line string.
|
||||
render_plot(StemLeafPairs) ->
|
||||
Lines = [render_line(Pair) || Pair <- StemLeafPairs],
|
||||
string:join(Lines, "\n").
|
||||
|
||||
%%% ============================================================
|
||||
%%% 4. Program Entry Point
|
||||
%%% ============================================================
|
||||
|
||||
main() ->
|
||||
%% --------------------------------------------------------
|
||||
%% Raw Dataset
|
||||
%% --------------------------------------------------------
|
||||
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
|
||||
],
|
||||
|
||||
%% --------------------------------------------------------
|
||||
%% Functional Processing Pipeline
|
||||
%% --------------------------------------------------------
|
||||
StemLeafStructure = build_stem_leaf(Data),
|
||||
Plot = render_plot(StemLeafStructure),
|
||||
|
||||
%% --------------------------------------------------------
|
||||
%% Output
|
||||
%% --------------------------------------------------------
|
||||
io:format("~s~n", [Plot]).
|
||||
28
Task/Stem-and-leaf-plot/Euphoria/stem-and-leaf-plot.eu
Normal file
28
Task/Stem-and-leaf-plot/Euphoria/stem-and-leaf-plot.eu
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
include sort.e
|
||||
|
||||
procedure leaf_plot(sequence s)
|
||||
sequence stem
|
||||
s = sort(s)
|
||||
stem = repeat({},floor(s[$]/10)+1)
|
||||
for i = 1 to length(s) do
|
||||
stem[floor(s[i]/10)+1] &= remainder(s[i],10)
|
||||
end for
|
||||
for i = 1 to length(stem) do
|
||||
printf(1, "%3d | ", i-1)
|
||||
for j = 1 to length(stem[i]) do
|
||||
printf(1, "%d ", stem[i][j])
|
||||
end for
|
||||
puts(1,'\n')
|
||||
end for
|
||||
end procedure
|
||||
|
||||
constant 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 }
|
||||
|
||||
leaf_plot(data)
|
||||
17
Task/Stem-and-leaf-plot/Perl/stem-and-leaf-plot-4.pl
Normal file
17
Task/Stem-and-leaf-plot/Perl/stem-and-leaf-plot-4.pl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use strict; # https://rosettacode.org/wiki/Stem-and-leaf_plot
|
||||
use warnings;
|
||||
|
||||
my @plot;
|
||||
/(.*)(.)/, $plot[$1 || 0] .= " $2" for sort {$a <=> $b} map split, <DATA>;
|
||||
printf "%3d |%s\n", $_, $plot[$_] // '' for 0 .. $#plot;
|
||||
|
||||
__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
|
||||
28
Task/Stem-and-leaf-plot/Pluto/stem-and-leaf-plot.pluto
Normal file
28
Task/Stem-and-leaf-plot/Pluto/stem-and-leaf-plot.pluto
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
local fmt = require "fmt"
|
||||
|
||||
local function leaf_plot(x)
|
||||
x:sort()
|
||||
local i = x[1] // 10 - 1
|
||||
for j = 1, #x do
|
||||
local d = x[j] // 10
|
||||
while (d > i) do
|
||||
i += 1
|
||||
fmt.write("%s%3d |", (j != 1) ? "\n" : "", i)
|
||||
end
|
||||
io.write($" {x[j] % 10}")
|
||||
end
|
||||
print()
|
||||
end
|
||||
|
||||
local 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
|
||||
}
|
||||
leaf_plot(data)
|
||||
10
Task/Stem-and-leaf-plot/PowerShell/stem-and-leaf-plot.ps1
Normal file
10
Task/Stem-and-leaf-plot/PowerShell/stem-and-leaf-plot.ps1
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
$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'
|
||||
|
||||
$Data = $Set | Select @{ Label = 'Stem'; Expression = { [string][int]$_.Substring( 0, $_.Length - 1 ) } }, @{ Label = 'Leaf'; Expression = { [string]$_[-1] } }
|
||||
|
||||
$StemStats = $Data | Measure-Object -Property Stem -Minimum -Maximum
|
||||
|
||||
ForEach ( $Stem in $StemStats.Minimum..$StemStats.Maximum )
|
||||
{
|
||||
@( $Stem.ToString().PadLeft( 2, " " ), '|' ) + ( ( $Data | Where Stem -eq $Stem ).Leaf | Sort ) -join " "
|
||||
}
|
||||
120
Task/Stem-and-leaf-plot/Rust/stem-and-leaf-plot.rs
Normal file
120
Task/Stem-and-leaf-plot/Rust/stem-and-leaf-plot.rs
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
// ================================================================ :
|
||||
// Advice: Rust with a functional approach
|
||||
// : Treats single-digit numbers as having a leading zero (stem 0)
|
||||
// : Uses the last digit as the leaf
|
||||
// : Includes all stems from 0 to 14
|
||||
// : Uses a functional approach with pure functions
|
||||
// : Produces monospaced plain text output
|
||||
// ================================================================
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
// ================================================================
|
||||
// Mathematical Helper Functions (Pure)
|
||||
// ================================================================
|
||||
|
||||
// Conceptually expands single digit numbers.
|
||||
// Numerically unchanged, included for semantic clarity.
|
||||
fn expand_number(n: u32) -> u32 {
|
||||
n
|
||||
}
|
||||
|
||||
// Returns the stem (all but the last digit).
|
||||
// Example: 127 -> 12
|
||||
fn stem(n: u32) -> u32 {
|
||||
n / 10
|
||||
}
|
||||
|
||||
// Returns the leaf (last digit).
|
||||
// Example: 127 -> 7
|
||||
fn leaf(n: u32) -> u32 {
|
||||
n % 10
|
||||
}
|
||||
|
||||
// ================================================================
|
||||
// Data Transformation Layer (Pure Functional Pipeline)
|
||||
// ================================================================
|
||||
|
||||
// Builds the stem-leaf structure.
|
||||
// Returns a BTreeMap so stems are automatically sorted.
|
||||
// Ensures stems 0..14 always exist.
|
||||
fn build_stem_leaf(data: &[u32]) -> BTreeMap<u32, Vec<u32>> {
|
||||
// Step 1: Expand and sort the data
|
||||
let mut sorted: Vec<u32> = data.iter().map(|&n| expand_number(n)).collect();
|
||||
|
||||
sorted.sort();
|
||||
|
||||
// Step 2: Create all stems 0..14
|
||||
(0..=14)
|
||||
.map(|s| {
|
||||
// Collect and sort leaves belonging to this stem
|
||||
let mut leaves: Vec<u32> = sorted
|
||||
.iter()
|
||||
.filter(|&&n| stem(n) == s)
|
||||
.map(|&n| leaf(n))
|
||||
.collect();
|
||||
|
||||
leaves.sort();
|
||||
|
||||
(s, leaves)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
// ================================================================
|
||||
// Formatting Layer (Pure Rendering)
|
||||
// ================================================================
|
||||
|
||||
// Formats a stem as two digits (leading zero if necessary).
|
||||
// Example: 7 -> "07"
|
||||
fn format_stem(s: u32) -> String {
|
||||
format!("{:02}", s)
|
||||
}
|
||||
|
||||
// Formats leaves as space-separated digits.
|
||||
// Example: [1,3,7] -> "1 3 7"
|
||||
fn format_leaves(leaves: &[u32]) -> String {
|
||||
leaves
|
||||
.iter()
|
||||
.map(|l| l.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
// Renders the complete stem-and-leaf plot as multi-line string.
|
||||
fn render_plot(stem_leaf: &BTreeMap<u32, Vec<u32>>) -> String {
|
||||
stem_leaf
|
||||
.iter()
|
||||
.map(|(s, leaves)| format!("{} | {}", format_stem(*s), format_leaves(leaves)))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
// ================================================================
|
||||
// Program Entry Point
|
||||
// ================================================================
|
||||
|
||||
fn main() {
|
||||
// ------------------------------------------------------------
|
||||
// Raw Dataset
|
||||
// ------------------------------------------------------------
|
||||
let data: Vec<u32> = vec![
|
||||
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,
|
||||
];
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Functional Processing Pipeline
|
||||
// ------------------------------------------------------------
|
||||
let stem_leaf_structure = build_stem_leaf(&data);
|
||||
let plot = render_plot(&stem_leaf_structure);
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Output (Monospaced Plain Text)
|
||||
// ------------------------------------------------------------
|
||||
println!("{}", plot);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue