Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,8 @@
generic
type Element_Type is private;
type Element_Array is array (Positive range <>) of Element_Type;
package Mode is
function Get_Mode (Set : Element_Array) return Element_Array;
end Mode;

View file

@ -0,0 +1,102 @@
with Ada.Containers.Indefinite_Vectors;
package body Mode is
-- map Count to Elements
package Count_Vectors is new Ada.Containers.Indefinite_Vectors
(Element_Type => Element_Array,
Index_Type => Positive);
procedure Add (To : in out Count_Vectors.Vector; Item : Element_Type) is
use type Count_Vectors.Cursor;
Position : Count_Vectors.Cursor := To.First;
Found : Boolean := False;
begin
while not Found and then Position /= Count_Vectors.No_Element loop
declare
Elements : Element_Array := Count_Vectors.Element (Position);
begin
for I in Elements'Range loop
if Elements (I) = Item then
Found := True;
end if;
end loop;
end;
if not Found then
Position := Count_Vectors.Next (Position);
end if;
end loop;
if Position /= Count_Vectors.No_Element then
-- element found, remove it and insert to next count
declare
New_Position : Count_Vectors.Cursor :=
Count_Vectors.Next (Position);
begin
-- remove from old position
declare
Old_Elements : Element_Array :=
Count_Vectors.Element (Position);
New_Elements : Element_Array (1 .. Old_Elements'Length - 1);
New_Index : Positive := New_Elements'First;
begin
for I in Old_Elements'Range loop
if Old_Elements (I) /= Item then
New_Elements (New_Index) := Old_Elements (I);
New_Index := New_Index + 1;
end if;
end loop;
To.Replace_Element (Position, New_Elements);
end;
-- new position not already there?
if New_Position = Count_Vectors.No_Element then
declare
New_Array : Element_Array (1 .. 1) := (1 => Item);
begin
To.Append (New_Array);
end;
else
-- add to new position
declare
Old_Elements : Element_Array :=
Count_Vectors.Element (New_Position);
New_Elements : Element_Array (1 .. Old_Elements'Length + 1);
begin
New_Elements (1 .. Old_Elements'Length) := Old_Elements;
New_Elements (New_Elements'Last) := Item;
To.Replace_Element (New_Position, New_Elements);
end;
end if;
end;
else
-- element not found, add to count 1
Position := To.First;
if Position = Count_Vectors.No_Element then
declare
New_Array : Element_Array (1 .. 1) := (1 => Item);
begin
To.Append (New_Array);
end;
else
declare
Old_Elements : Element_Array :=
Count_Vectors.Element (Position);
New_Elements : Element_Array (1 .. Old_Elements'Length + 1);
begin
New_Elements (1 .. Old_Elements'Length) := Old_Elements;
New_Elements (New_Elements'Last) := Item;
To.Replace_Element (Position, New_Elements);
end;
end if;
end if;
end Add;
function Get_Mode (Set : Element_Array) return Element_Array is
Counts : Count_Vectors.Vector;
begin
for I in Set'Range loop
Add (Counts, Set (I));
end loop;
return Counts.Last_Element;
end Get_Mode;
end Mode;

View file

@ -0,0 +1,26 @@
with Ada.Text_IO;
with Mode;
procedure Main is
type Int_Array is array (Positive range <>) of Integer;
package Int_Mode is new Mode (Integer, Int_Array);
Test_1 : Int_Array := (1, 2, 3, 1, 2, 4, 2, 5, 2, 3, 3, 1, 3, 6);
Result : Int_Array := Int_Mode.Get_Mode (Test_1);
begin
Ada.Text_IO.Put ("Input: ");
for I in Test_1'Range loop
Ada.Text_IO.Put (Integer'Image (Test_1 (I)));
if I /= Test_1'Last then
Ada.Text_IO.Put (",");
end if;
end loop;
Ada.Text_IO.New_Line;
Ada.Text_IO.Put ("Result:");
for I in Result'Range loop
Ada.Text_IO.Put (Integer'Image (Result (I)));
if I /= Result'Last then
Ada.Text_IO.Put (",");
end if;
end loop;
Ada.Text_IO.New_Line;
end Main;

View file

@ -0,0 +1,39 @@
include misc.e
function mode(sequence s)
sequence uniques, counts, modes
integer j,max
uniques = {}
counts = {}
for i = 1 to length(s) do
j = find(s[i], uniques)
if j then
counts[j] += 1
else
uniques = append(uniques, s[i])
counts = append(counts, 1)
end if
end for
max = counts[1]
for i = 2 to length(counts) do
if counts[i] > max then
max = counts[i]
end if
end for
j = 1
modes = {}
while j <= length(s) do
j = find_from(max, counts, j)
if j = 0 then
exit
end if
modes = append(modes, uniques[j])
j += 1
end while
return modes
end function
constant s = { 1, "blue", 2, 7.5, 5, "green", "red", 5, 2, "blue", "white" }
pretty_print(1,mode(s),{3})

View file

@ -0,0 +1,23 @@
local fmt = require "fmt"
local function mode(a)
local t = a:countvalues()
local max = t:values():max()
local res = {}
for k, v in t do
if v == max then res:insert(k) end
end
return res
end
local tests = {
{1, 2, 5, -5, -9.5, 3.14159},
{ 1, "blue", 2, 7.5, 5, "green", "red", 5, 2, "blue", "white" },
{1, 2, 3, 1, 2, 4, 2, 5, 2, 3, 3, 1, 3, 6},
{.2, .7, .1, .8, .2},
{"two", 7, 1, 8, "two", 8},
"Hello there world":split(""),
{}
}
for tests as test do fmt.lprint(mode(test)) end

View file

@ -0,0 +1,3 @@
$data = @(1,1,1,2,3,4,5,5,6,7,7,7)
$groups = $data | group-object | sort-object count -Descending
$groups | ? {$_.Count -eq $groups[0].Count}

View file

@ -0,0 +1,3 @@
Modes ← ▽=⊸/↥⊕⊃⧻⊢⊸⊛
Modes [1 3 6 6 6 6 7 7 12 12 17]
Modes [1 1 2 4 4]