Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,67 @@
PROGRAM MAX_SUM
DIM A%[11],B%[10],C%[4]
!$DYNAMIC
DIM P%[0]
PROCEDURE MAX_SUBSEQUENCE(P%[],N%->A$)
LOCAL A%,B%,I%,J%,M%,S%
A%=1
FOR I%=0 TO N% DO
S%=0
FOR J%=I% TO N% DO
S%+=P%[J%]
IF S%>M% THEN
M%=S%
A%=I%
B%=J%
END IF
END FOR
END FOR
IF A%>B% THEN A$="[]" EXIT PROCEDURE END IF
A$="["
FOR I%=A% TO B% DO
A$+=STR$(P%[I%])+","
END FOR
A$=LEFT$(A$,LEN(A$)-1)+"]"
END PROCEDURE
PROCEDURE SHOW_ARRAY(P%[],N%->A$)
LOCAL I%
A$="["
FOR I%=0 TO N% DO
A$+=STR$(P%[I%])+","
END FOR
A$=LEFT$(A$,LEN(A$)-1)+"]"
END PROCEDURE
BEGIN
A%[]=(0,1,2,-3,3,-1,0,-4,0,-1,-4,2)
N%=UBOUND(A%,1)
!$DIM P%[N%]
SHOW_ARRAY(A%[],N%->A$)
PRINT(A$;" -> ";)
MAX_SUBSEQUENCE(A%[],N%->A$)
PRINT(A$)
!$ERASE P%
B%[]=(-1,-2,3,5,6,-2,-1,4,-4,2,-1)
N%=UBOUND(B%,1)
!$DIM P%[N%]
SHOW_ARRAY(B%[],N%->A$)
PRINT(A$;" -> ";)
MAX_SUBSEQUENCE(B%[],N%->A$)
PRINT(A$)
!$ERASE P%
C%[]=(-1,-2,-3,-4,-5)
N%=UBOUND(C%,1)
!$DIM P%[N%]
SHOW_ARRAY(C%[],N%->A$)
PRINT(A$;" -> ";)
MAX_SUBSEQUENCE(C%[],N%->A$)
PRINT(A$)
!$ERASE P%
END PROGRAM

View file

@ -0,0 +1,35 @@
(lib 'struct)
(struct result (score starter))
;; the score of i in sequence ( .. i j ...) is max (i , i + score (j))
;; to compute score of (a b .. x y z) :
;; start with score(z) and compute scores of y , z , ..c, b , a.
;; this is O(n)
;; return value of sub-sequence
(define (max-max L into: result)
(define value
(if
(empty? L) -Infinity
(max (first L) (+ (first L) (max-max (cdr L) result )))))
(when (> value (result-score result))
(set-result-score! result value) ;; remember best score
(set-result-starter! result L)) ;; and its location
value)
;; return (best-score (best sequence))
(define (max-seq L)
(define best (result -Infinity null))
(max-max L into: best)
(define score (result-score best))
(list score
(for/list (( n (result-starter best)))
#:break (zero? score)
(set! score (- score n))
n)))
(define L '(-1 -2 3 5 6 -2 -1 4 -4 2 -1))
(max-seq L)
→ (15 (3 5 6 -2 -1 4))

View file

@ -0,0 +1,36 @@
' FB 1.05.0 Win64
Dim As Integer seq(10) = {-1 , -2 , 3 , 5 , 6 , -2 , -1 , 4 , -4 , 2 , -1}
Dim As Integer i, j, sum, maxSum, first, last
maxSum = 0
For i = LBound(seq) To UBound(seq)
sum = 0
For j = i To UBound(seq)
' only proper sub-sequences are considered
If i = LBound(seq) AndAlso j = UBound(seq) Then Exit For
sum += seq(j)
If sum > maxSum Then
maxSum = sum
first = i
last = j
End If
Next j
Next i
If maxSum > 0 Then
Print "Maximum subsequence is from indices"; first; " to"; last
Print "Elements are : ";
For i = first To last
Print seq(i); " ";
Next
Print
Print "Sum is"; maxSum
Else
Print "Maximum subsequence is the empty sequence which has a sum of 0"
End If
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,7 @@
proc maxsum(s): int =
var maxendinghere = 0
for x in s:
maxendinghere = max(maxendinghere + x, 0)
result = max(result, maxendinghere)
echo maxsum(@[-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1])

View file

@ -0,0 +1,16 @@
function maxSubseq(sequence s)
integer this, maxsum = 0, first = 1, last = 0
for i=1 to length(s) do
this = 0
for j=i to length(s) do
this += s[j]
if this>maxsum then
{maxsum,first,last} = {this,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})

View file

@ -0,0 +1,30 @@
gss = (lst) :
# Find discrete integral
integral = (0)
accum = 0
lst each (n): accum = accum + n, integral append(accum).
# Check integral[b + 1] - integral[a] for all 0 <= a <= b < N
max = -1
max_a = 0
max_b = 0
lst length times (b) :
b times (a) :
if (integral(b + 1) - integral(a) > max) :
max = integral(b + 1) - integral(a)
max_a = a
max_b = b
.
.
.
# Print the results
if (max >= 0) :
(lst slice(max_a, max_b) join(" + "), " = ", max, "\n") join print
.
else :
"No subsequence larger than 0\n" print
.
.
gss((-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1))
gss((-1, -2, -3, -4, -5))
gss((7,-6, -8, 5, -2, -6, 7, 4, 8, -9, -3, 2, 6, -4, -6))

View file

@ -0,0 +1,19 @@
func maxsubseq(*a) {
var (start, end, sum, maxsum) = (-1, -1, 0, 0);
a.each_kv { |i, x|
sum += x;
if (maxsum < sum) {
maxsum = sum;
end = i;
}
elsif (sum < 0) {
sum = 0;
start = i;
}
};
a.ft(start+1, end);
}
say maxsubseq(-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1);
say maxsubseq(-2, -2, -1, 3, 5, 6, -1, 4, -4, 2, -1);
say maxsubseq(-2, -2, -1, -3, -5, -6, -1, -4, -4, -2, -1);

View file

@ -0,0 +1,11 @@
def subarray_sum:
. as $arr
| reduce range(0; length) as $i
( {"first": length, "last": 0, "curr": 0, "curr_first": 0, "max": 0};
$arr[$i] as $e
| (.curr + $e) as $curr
| . + (if $e > $curr then {"curr": $e, "curr_first": $i} else {"curr": $curr} end)
| if .curr > .max then . + {"max": $curr, "first": .curr_first, "last": $i}
else .
end)
| [ .max, $arr[ .first : (1 + .last)] ];

View file

@ -0,0 +1 @@
[1, 2, 3, 4, 5, -8, -9, -20, 40, 25, -5] | subarray_sum

View file

@ -0,0 +1,2 @@
$ jq -c -n -f Greatest_subsequential_sum.jq
[65,[40,25]]