June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,14 +1,17 @@
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]
function gss(arr::Vector{<:Number})
smax = hmax = tmax = 0
for head in eachindex(arr), tail in head:length(arr)
s = sum(arr[head:tail])
if s > smax
smax = s
hmax, tmax = head, tail
end
end
return arr[hmax:tmax]
end
println(maxseq)
println(maxval)
arr = [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]
subseq = gss(arr)
s = sum(subseq)
println("Greatest subsequential sum of $arr:\n → $subseq with sum $s")

View file

@ -0,0 +1,51 @@
# Project : Greatest subsequential sum
# Date : 2017/09/26
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
aList1 = [0, 1, 2, -3, 3, -1, 0, -4, 0, -1, -4, 2]
see "[0, 1, 2, -3, 3, -1, 0, -4, 0, -1, -4, 2] -> " + sum(aList1) + nl
aList2 = [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]
see "[-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1] -> " + sum(aList2) + nl
aList3 = [-1, -2, -3, -4, -5]
see "[-1, -2, -3, -4, -5] -> " + sum(aList3) + nl
aList4 = []
see "[] - > " + sum(aList4) + nl
func sum aList
sumold = []
sumnew = []
snew = 0
flag = 0
if len(aList) = 0
return 0
ok
for s=1 to len(aList)
if aList[s] > -1
flag = 1
ok
next
if flag = 0
return "[]"
ok
for n=1 to len(aList)
sumold = []
sold = 0
for m=n to len(aList)
add(sumold, aList[m])
sold = sold + aList[m]
if sold > snew
snew = sold
sumnew = sumold
ok
next
next
return showarray(sumnew)
func showarray(a)
conv = "["
for i = 1 to len(a)
conv = conv + string(a[i]) + ", "
next
conv = left(conv, len(conv) - 2) + "]"
return conv