Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,11 @@
defmodule Greatest do
def subseq_sum(list) do
limit = length(list) - 1
ij = for i <- 0..limit, j <- i..limit, do: {i,j}
Enum.reduce(ij, {0, []}, fn {i,j},{max, subseq} ->
slice = Enum.slice(list, i..j)
sum = Enum.sum(slice)
if sum > max, do: {sum, slice}, else: {max, subseq}
end)
end
end

View file

@ -0,0 +1,9 @@
data = [ [1, 2, 3, 4, 5, -8, -9, -20, 40, 25, -5],
[-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1],
[-1, -2, -3, -4, -5],
[] ]
Enum.each(data, fn input ->
IO.puts "\nInput seq: #{inspect input}"
{max, subseq} = Greatest.subseq_sum(input)
IO.puts " Max sum: #{max}\n Subseq: #{inspect subseq}"
end)

View file

@ -0,0 +1,17 @@
defmodule Greatest do
def subseq_sum(list) do
list_i = Enum.with_index(list)
acc = {0, 0, length(list), 0, 0}
{_,max,first,last,_} = Enum.reduce(list_i, acc, fn {elm,i},{curr,max,first,last,curr_first} ->
if curr < 0 do
if elm > max, do: {elm, elm, i, i, curr_first},
else: {elm, max, first, last, curr_first}
else
cur2 = curr + elm
if cur2 > max, do: {cur2, cur2, curr_first, i, curr_first},
else: {cur2, max, first, last, curr_first}
end
end)
{max, Enum.slice(list, first..last)}
end
end

View file

@ -7,7 +7,7 @@ variable best-sum
: max-sub ( array len -- sub len )
over 0 best 2! 0 best-sum !
dup 1 do \ foreach length
2dup i - cells over + swap do \ foreach start
2dup i - 1+ cells over + swap do \ foreach start
i j sum
dup best-sum @ > if
best-sum !
@ -19,6 +19,5 @@ variable best-sum
: .array ." [" dup 0 ?do over i cells + @ . loop ." ] = " sum . ;
create test -1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1 ,
test 11 max-sub .array \ [3 5 6 -2 -1 4 ] = 15
create test -1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1 ,
create test2 -1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , 99 ,

View file

@ -0,0 +1,2 @@
test 11 max-sub .array [3 5 6 -2 -1 4 ] = 15 ok
test2 11 max-sub .array [3 5 6 -2 -1 4 -4 2 99 ] = 112 ok

View file

@ -0,0 +1,14 @@
const A = [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]
maxval = 0
maxseq = Int[]
for head=1:length(A), tail=head:length(A)
val = sum(A[head:tail])
if val > maxval
maxval = val
maxseq = A[head:tail]
end
end
println(maxseq)
println(maxval)

View file

@ -1,14 +1,14 @@
/*REXX program finds the shortest greatest continuous subsequence sum.*/
parse arg @; w=words(@) /*get arg list; # words in list.*/
say 'words='w " list="@ /*show #words & LIST to console.*/
sum=0; L=0; at=w+1 /*default sum, length, starts at.*/
/* [↓] process the list of nums.*/
do j=1 for w; f=word(@,j) /*select one number at a time. */
do k=j to w /* [↓] process a sub─list of #s.*/
s=f; do m=j+1 to k; s=s+word(@,m); end /*m*/
/*REXX program finds the shortest greatest continuous subsequence sum. */
parse arg @; w=words(@) /*get arg list; number words in list. */
say 'words='w " list="@ /*show number words & LIST to terminal,*/
sum=0; L=0; at=w+1 /*default sum, length, and "starts at".*/
/* [↓] process the list of numbers. */
do j=1 for w; f=word(@,j) /*select one number at a time from list*/
do k=j to w; s=f /* [↓] process a sub─list of numbers. */
do m=j+1 to k; s=s+word(@,m); end /*m*/
if s>sum then do; sum=s; at=j; L=k-j+1; end
end /*k*/ /* [↑] chose greatest sum of #s.*/
end /*k*/ /* [↑] chose greatest sum of numbers. */
end /*j*/
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize it.*/
say; say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're done.*/
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize the null. */
say; say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're all done. */

View file

@ -1,14 +1,14 @@
/*REXX program finds the longest greatest continuous subsequence sum.*/
parse arg @; w=words(@) /*get arg list; # words in list.*/
say 'words='w " list="@ /*show #words & LIST to console.*/
sum=0; L=0; at=w+1 /*default sum, length, starts at.*/
/* [↓] process the list of nums.*/
do j=1 for w; f=word(@,j) /*select one number at a time. */
do k=j to w; _=k-j+1 /* [↓] process a sub─list of #s.*/
s=f; do m=j+1 to k; s=s+word(@,m); end /*m*/
/*REXX program finds the longest greatest continuous subsequence sum. */
parse arg @; w=words(@) /*get arg list; number words in list. */
say 'words='w " list="@ /*show number words & LIST to terminal,*/
sum=0; L=0; at=w+1 /*default sum, length, and "starts at".*/
/* [↓] process the list of numbers. */
do j=1 for w; f=word(@,j) /*select one number at a time from list*/
do k=j to w; _=k-j+1; s=f /* [↓] process a sub─list of numbers. */
do m=j+1 to k; s=s+word(@,m); end /*m*/
if (s==sum & _>L) | s>sum then do; sum=s; at=j; L=_; end
end /*k*/ /* [↑] chose longest greatest sum*/
end /*k*/ /* [↑] chose the longest greatest sum.*/
end /*j*/
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize it.*/
say; say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're done.*/
$=subword(@,at,L); if $=='' then $="[NULL]" /*Englishize the null. */
say; say 'sum='sum/1 " sequence="$ /*stick a fork in it, we're all done. */

View file

@ -0,0 +1,19 @@
fn main() {
let nums = [1,2,39,34,20, -20, -16, 35, 0];
let mut max = 0;
let mut boundaries = 0..0;
for length in 0..nums.len() {
for start in 0..nums.len()-length {
let sum = (&nums[start..start+length]).iter()
.fold(0, |sum, elem| sum+elem);
if sum > max {
max = sum;
boundaries = start..start+length;
}
}
}
println!("Max subsequence sum: {} for {:?}", max, &nums[boundaries]);;
}