Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,8 +0,0 @@
|
|||
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;
|
||||
|
|
@ -1,102 +0,0 @@
|
|||
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;
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
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;
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
getMode: function [arr][
|
||||
freqs: new #[]
|
||||
freqs: #[]
|
||||
loop arr 'i [
|
||||
k: to :string i
|
||||
if not? key? freqs k -> set freqs k 0
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ extension op
|
|||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
public Program()
|
||||
{
|
||||
var array1 := new int[]{1, 1, 2, 4, 4};
|
||||
var array2 := new int[]{1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17};
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
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})
|
||||
|
|
@ -1,41 +1,40 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">mode</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- returns a list of the most common values, each of which occurs the same number of times</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">nxt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nxt</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #000000;">nxt</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">></span><span style="color: #000000;">maxc</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">maxc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">count</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nxt</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nxt</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">maxc</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
with javascript_semantics
|
||||
function mode(sequence s)
|
||||
-- returns a list of the most common values, each of which occurs the same number of times
|
||||
integer nxt = 1, count = 1, maxc = 1
|
||||
sequence res = {}
|
||||
if length(s)!=0 then
|
||||
s = sort(s)
|
||||
object prev = s[1]
|
||||
for i=2 to length(s) do
|
||||
if s[i]!=prev then
|
||||
s[nxt] = {count,prev}
|
||||
nxt += 1
|
||||
prev = s[i]
|
||||
count = 1
|
||||
else
|
||||
count += 1
|
||||
if count>maxc then
|
||||
maxc = count
|
||||
end if
|
||||
end if
|
||||
end for
|
||||
s[nxt] = {count,prev}
|
||||
res = ""
|
||||
for i=1 to nxt do
|
||||
if s[i][1]=maxc then
|
||||
res = append(res,s[i][2])
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">9.5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3.14159</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">({</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"blue"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7.5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"green"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"red"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"blue"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"white"</span> <span style="color: #0000FF;">})</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">({.</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">.</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">.</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">.</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">.</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"two"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"two"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Hello there world"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">({})</span>
|
||||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
?mode({1, 2, 5, -5, -9.5, 3.14159})
|
||||
?mode({ 1, "blue", 2, 7.5, 5, "green", "red", 5, 2, "blue", "white" })
|
||||
?mode({1, 2, 3, 1, 2, 4, 2, 5, 2, 3, 3, 1, 3, 6})
|
||||
?mode({.2, .7, .1, .8, .2})
|
||||
?mode({"two", 7, 1, 8, "two", 8})
|
||||
?mode("Hello there world")
|
||||
?mode({})
|
||||
{} = wait_key()
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
$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}
|
||||
Loading…
Add table
Add a link
Reference in a new issue