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,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,87 @@
!
! Longest increasing subsequence
! tested with Intel ifx (IFX) 2025.2.1 20250806 on Kubuntu 25.10
! GNU Fortran (Ubuntu 15.2.0-4ubuntu4) 15.2.0 on Kubuntu 25.10
! VSI Fortran x86-64 V8.6-001 on OpenVMS x86_64 V9.2-3
! No Non-standard features used, should compile on any fairly recent Fortran.
! U.B., January 2026
!==============================================================================
program LIS
implicit none
! The two sequences of the task description
!
integer, parameter, dimension( 6) :: S1=[3,2,6,4,5,1]
integer, parameter, dimension(16) :: S2=[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
! dimension(*) is not used only to make VSI Fortran (on openVMS) happy.
! Intel and GNU Fortran work with dimension(*)
! Find and print the Longest Increasing Subsequences
call printLIS (S1, size(S1))
call printLIS (S2, size(S2))
contains
! ====================================================================
! Find and print the longest increasing subsequence (LIS) of array X.
! Algorithm as well as Notation as described in the wikipedia article
! https://en.wikipedia.org/wiki/Longest_increasing_subsequence
! ====================================================================
subroutine printLIS (X, LS)
integer, intent(in) :: LS
integer, intent(in), dimension(LS) :: X
integer, dimension(LS) :: result ! found longest increasing subsequence
integer, dimension(LS) :: P ! list of Predecessors
integer, dimension(LS) :: M ! Note this does not need to be longer because we're 1-based.
integer :: L ! Current length of LIS
integer :: newl ! New estimate of LIS length
integer :: i ! Loop index
integer :: lo, hi, mid ! helpers for binary search
integer :: k ! index for for the chain od the LIS
L=0
do i=1,LS
! Binary search for the smallest positive l ≤ LS
! such that X[M[l]] >= X[i] lo=1
lo = 1
hi = L
do while (lo .le. hi) ! Note on very first time, lo is1 and l is 0 ...
mid = (lo+hi) / 2
if (X (M(mid)) .lt. X(i)) then ! ... hence no problem with uninitialized M
lo = mid+1
else
hi=mid-1
endif
end do
! After this search, lo is 1 greater than the
! length of the longest prefix of X(i)
newL = lo
! The predecessor of X[i] is the last index of
! the subsequence of length newL-1
P(i) = M(newL-1)
M(newl) = i
if (newl .gt. L) then
! If we found a subsequence longer than any we've
! found yet, update L
L = newL
endif
enddo
! Reconstruct the longest increasing subsequence
! It consists of the values of X at the L indices:
! ..., P[P[M[L]]], P[M[L]], M[L]
k = M(L) ! This is the last element,
do i = L, 1, -1 ! now go dotn to the first.
result(i) = X(k)
k = P(k)
end do
! Print in increasing order
write (*,'(10(I0,x))') (result(i),i=1,L)
end subroutine printLIS
end program LIS

View file

@ -0,0 +1,43 @@
require "table2"
local function longest_increasing_subsequence(x)
local n = #x
if n == 0 then return {} end
if n == 1 then return x end
local p = table.rep(n, 0)
local m = table.rep(n, 0)
local len = 0
for i = 1, n do
local lo = 1
local hi = len
while lo <= hi do
local mid = math.ceil((lo + hi) / 2)
if x[m[mid]] < x[i] then
lo = mid + 1
else
hi = mid - 1
end
end
if lo > 1 then p[i] = m[lo - 1] end
m[lo] = i
if lo > len then len = lo end
end
local res = table.rep(len, 0)
if len > 0 then
local k = m[len]
for i = len, 1, -1 do
res[i] = x[k]
k = p[k]
end
end
return res
end
local lists = {
{3, 2, 6, 4, 5, 1},
{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}
}
lists:foreach(|l| -> do
print(longest_increasing_subsequence(l):concat(", "))
end)

View file

@ -0,0 +1,48 @@
function Get-LongestSubsequence ( [int[]]$A )
{
If ( $A.Count -lt 2 ) { return $A }
# Start with an "empty" pile
# (We will only store the top value in each "pile".)
$Pile = @( [int]::MaxValue )
$Last = 0
# Hashtable to hold the back pointers
$BP = @{}
# For each number in the orginal sequence...
ForEach ( $N in $A )
{
# Find the first pile with a value greater than N
$i = 0..$Last | Where { $N -lt $Pile[$_] } | Select -First 1
# Place N on the pile
$Pile[$i] = $N
# Set the back pointer for this value to the value of the previous pile
$BP["$N"] = $Pile[$i-1]
# If this is the previously empty pile, add a new empty pile
If ( $i -eq $Last )
{
$Pile += @( [int]::MaxValue )
$Last++
}
}
# Ignore the empty pile
$Last--
# Start with the value of the last pile
$N = $Pile[$Last]
$S = @( $N )
# Add the remainder of the values by walking through the back pointers
ForEach ( $i in $Last..1 )
{
$S += ( $N = $BP["$N"] )
}
# Return the series (reversed into the correct order)
return $S[$Last..0]
}

View file

@ -0,0 +1,2 @@
( Get-LongestSubsequence 3, 2, 6, 4, 5, 1 ) -join ', '
( Get-LongestSubsequence 0, 8, 4, 12, 2, 10, 6, 16, 14, 1, 9, 5, 13, 3, 11, 7, 15 ) -join ', '

View file

@ -0,0 +1,37 @@
Function LIS(arr)
n = UBound(arr)
Dim p()
ReDim p(n)
Dim m()
ReDim m(n)
l = 0
For i = 0 To n
lo = 1
hi = l
Do While lo <= hi
middle = Int((lo+hi)/2)
If arr(m(middle)) < arr(i) Then
lo = middle + 1
Else
hi = middle - 1
End If
Loop
newl = lo
p(i) = m(newl-1)
m(newl) = i
If newL > l Then
l = newl
End If
Next
Dim s()
ReDim s(l)
k = m(l)
For i = l-1 To 0 Step - 1
s(i) = arr(k)
k = p(k)
Next
LIS = Join(s,",")
End Function
WScript.StdOut.WriteLine LIS(Array(3,2,6,4,5,1))
WScript.StdOut.WriteLine LIS(Array(0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15))