Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,38 +0,0 @@
|
|||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Max_Subarray is
|
||||
type Int_Array is array (Positive range <>) of Integer;
|
||||
Empty_Error : Exception;
|
||||
function Max(Item : Int_Array) return Int_Array is
|
||||
Start : Positive;
|
||||
Finis : Positive;
|
||||
Max_Sum : Integer := Integer'First;
|
||||
Sum : Integer;
|
||||
begin
|
||||
if Item'Length = 0 then
|
||||
raise Empty_Error;
|
||||
end if;
|
||||
|
||||
for I in Item'range loop
|
||||
Sum := 0;
|
||||
for J in I..Item'Last loop
|
||||
Sum := Sum + Item(J);
|
||||
if Sum > Max_Sum then
|
||||
Max_Sum := Sum;
|
||||
Start := I;
|
||||
Finis := J;
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
return Item(Start..Finis);
|
||||
end Max;
|
||||
A : Int_Array := (-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1);
|
||||
B : Int_Array := Max(A);
|
||||
begin
|
||||
for I in B'range loop
|
||||
Put_Line(Integer'Image(B(I)));
|
||||
end loop;
|
||||
exception
|
||||
when Empty_Error =>
|
||||
Put_Line("Array being analyzed has no elements.");
|
||||
end Max_Subarray;
|
||||
|
|
@ -17,8 +17,8 @@ subarraySum: function [arr][
|
|||
lst: i
|
||||
]
|
||||
]
|
||||
if? lst > fst -> return @[mx, slice arr fst lst]
|
||||
else -> return [0, []]
|
||||
switch lst > fst -> return @[mx, slice arr fst lst]
|
||||
-> return [0, []]
|
||||
]
|
||||
|
||||
sequences: @[
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
Local $iArray[11] = [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]
|
||||
GREAT_SUB($iArray)
|
||||
Local $iArray[5] = [-1, -2, -3, -4, -5]
|
||||
GREAT_SUB($iArray)
|
||||
Local $iArray[15] = [7, -6, -8, 5, -2, -6, 7, 4, 8, -9, -3, 2, 6, -4, -6]
|
||||
GREAT_SUB($iArray)
|
||||
|
||||
Func GREAT_SUB($iArray)
|
||||
Local $iSUM = 0, $iBEGIN_MAX = 0, $iEND_MAX = -1, $iMAX_SUM = 0
|
||||
For $i = 0 To UBound($iArray) - 1
|
||||
$iSUM = 0
|
||||
For $k = $i To UBound($iArray) - 1
|
||||
$iSUM += $iArray[$k]
|
||||
If $iSUM > $iMAX_SUM Then
|
||||
$iMAX_SUM = $iSUM
|
||||
$iEND_MAX = $k
|
||||
$iBEGIN_MAX = $i
|
||||
EndIf
|
||||
Next
|
||||
Next
|
||||
ConsoleWrite("> Array: [")
|
||||
For $i = 0 To UBound($iArray) - 1
|
||||
If $iArray[$i] > 0 Then ConsoleWrite("+")
|
||||
ConsoleWrite($iArray[$i])
|
||||
If $i <> UBound($iArray) - 1 Then ConsoleWrite(",")
|
||||
Next
|
||||
ConsoleWrite("]" & @CRLF & "+>Maximal subsequence: [")
|
||||
$iSUM = 0
|
||||
For $i = $iBEGIN_MAX To $iEND_MAX
|
||||
$iSUM += $iArray[$i]
|
||||
If $iArray[$i] > 0 Then ConsoleWrite("+")
|
||||
ConsoleWrite($iArray[$i])
|
||||
If $i <> $iEND_MAX Then ConsoleWrite(",")
|
||||
Next
|
||||
ConsoleWrite("]" & @CRLF & "!>SUM of subsequence: " & $iSUM & @CRLF)
|
||||
EndFunc ;==>GREAT_SUB
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
function maxSubseq(sequence s)
|
||||
integer sum, maxsum, first, last
|
||||
maxsum = 0
|
||||
first = 1
|
||||
last = 0
|
||||
for i = 1 to length(s) do
|
||||
sum = 0
|
||||
for j = i to length(s) do
|
||||
sum += s[j]
|
||||
if sum > maxsum then
|
||||
maxsum = sum
|
||||
first = i
|
||||
last = j
|
||||
end if
|
||||
end for
|
||||
end for
|
||||
return s[first..last]
|
||||
end function
|
||||
|
||||
? maxSubseq({-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1})
|
||||
? maxSubseq({})
|
||||
? maxSubseq({-1, -5, -3})
|
||||
|
|
@ -8,7 +8,25 @@ function maxsub(ary, idx)
|
|||
end
|
||||
local v = maxsub(ary, idx + 1)
|
||||
if maxsum < sumt(v, 1, #v) then return v end
|
||||
local ret = {}
|
||||
for i = idx, last do ret[#ret+1] = ary[i] end
|
||||
return ret
|
||||
local ret, allNegative = {}, true
|
||||
for i = idx, last do
|
||||
ret[#ret+1] = ary[i]
|
||||
if ret[#ret] >= 0 then allNegative = false end
|
||||
end
|
||||
return allNegative and {} or ret
|
||||
end
|
||||
|
||||
local function test(s)
|
||||
local msub = maxsub(s)
|
||||
print("["..table.concat(s, " ").."] -> ["..table.concat(msub, " ").."] sum: "..sumt(msub, 1, #msub))
|
||||
end
|
||||
|
||||
-- test cases from 11l
|
||||
test {-1, 2, -1}
|
||||
test {-1, 2, -1, 3, -1}
|
||||
test {-1, 1, 2, -5, -6}
|
||||
test {-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1}
|
||||
|
||||
-- additional test cases
|
||||
test {-1, -2, -1}
|
||||
test {}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,17 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">maxSubseq</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: #004080;">integer</span> <span style="color: #000000;">maxsum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">first</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</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: #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: #004080;">integer</span> <span style="color: #000000;">sumsij</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</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: #000000;">sumsij</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">sumsij</span><span style="color: #0000FF;">></span><span style="color: #000000;">maxsum</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">maxsum</span><span style="color: #0000FF;">,</span><span style="color: #000000;">first</span><span style="color: #0000FF;">,</span><span style="color: #000000;">last</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">sumsij</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</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;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">first</span><span style="color: #0000FF;">..</span><span style="color: #000000;">last</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #0000FF;">?</span> <span style="color: #000000;">maxSubseq</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;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</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;">2</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: #000000;">4</span><span style="color: #0000FF;">,</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: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #0000FF;">?</span> <span style="color: #000000;">maxSubseq</span><span style="color: #0000FF;">({})</span>
|
||||
<span style="color: #0000FF;">?</span> <span style="color: #000000;">maxSubseq</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;">5</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
with javascript_semantics
|
||||
function maxSubseq(sequence s)
|
||||
integer maxsum = 0, first = 1, last = 0
|
||||
for i=1 to length(s) do
|
||||
integer sumsij = 0
|
||||
for j=i to length(s) do
|
||||
sumsij += s[j]
|
||||
if sumsij>maxsum then
|
||||
{maxsum,first,last} = {sumsij,i,j}
|
||||
end if
|
||||
end for
|
||||
end for
|
||||
return s[first..last]
|
||||
end function
|
||||
? maxSubseq({-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1})
|
||||
? maxSubseq({})
|
||||
? maxSubseq({-1, -5, -3})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue