Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -0,0 +1,51 @@
with Ada.Text_IO;
procedure Longest_Increasing_Subsequence is
type Sequence is array (Positive range <>) of Integer;
Seq1 : constant Sequence :=
(3, 2, 6, 4, 5, 1);
Seq2 : constant Sequence :=
(0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15);
function Lis (Seq : Sequence) return Sequence is
Best : Sequence (Seq'Range) := (others => 1);
Pred : Sequence (Seq'Range) := (others => 0);
Best_Idx, Max_Len : Natural := 0;
begin
-- Calculate LIS length for every end point
for I in Seq'Range loop
for J in 1 .. I - 1 loop
if Seq (J) < Seq (I) and then
Best (I) < 1 + Best (J) then
Best (I) := 1 + Best (J);
Pred (I) := J;
end if;
end loop;
end loop;
-- Find tail of global LIS
for I in Best'Range loop
if Best (I) > Max_Len then
Best_Idx := I;
Max_Len := Best (I);
end if;
end loop;
-- Trace sequence
declare
Lis : Sequence (1 .. Max_Len);
begin
for I in reverse 1 .. Max_Len loop
Lis (I) := Seq (Best_Idx);
Best_Idx := Pred (Best_Idx);
end loop;
return Lis;
end;
end Lis;
begin
Ada.Text_IO.Put_Line (Lis (Seq1)'Image);
Ada.Text_IO.Put_Line (Lis (Seq2)'Image);
end Longest_Increasing_Subsequence;

View file

@ -0,0 +1,31 @@
# Return a list - just one in the case of a tie
create or replace function longest_increasing_subsequence(lst) as (
-- Add a flag to signal the start of a new subsequence
with flagged as (
select *, if(id = 1, 0, if(c > (lag(c) over ()), 0, 1)) as flag
from (select generate_subscripts(lst,1) as id,
unnest(lst) as c)
),
partitions as (
select *, sum(flag) over (order by id) as partition_number
from (select *, if(id=1 or (c > (lag(c) over ())), 1, 0) as flag
from flagged)
order by id asc
),
subseq_number as (
select max_by(partition_number, ncount) as n
from (select partition_number, count(*) as ncount
from partitions
group by partition_number)
)
select array_agg(c order by id)
from partitions, subseq_number
where partition_number = subseq_number.n
);
# Examples;
select l as list, longest_increasing_subsequence(l) as lis
from (select [3,2,6,4,5,1] as l union all
select [0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15] as l union all
select [0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15,20] as l);