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

@ -0,0 +1,14 @@
REPORT equilibrium_index.
TYPES: y_i TYPE STANDARD TABLE OF i WITH EMPTY KEY.
cl_demo_output=>display( REDUCE y_i( LET sequences = VALUE y_i( ( -7 ) ( 1 ) ( 5 ) ( 2 ) ( -4 ) ( 3 ) ( 0 ) )
total_sum = REDUCE #( INIT sum = 0
FOR sequence IN sequences
NEXT sum = sum + ( sequence ) ) IN
INIT x = VALUE y_i( )
y = 0
FOR i = 1 UNTIL i > lines( sequences )
LET z = sequences[ i ] IN
NEXT x = COND #( WHEN y = ( total_sum - y - z ) THEN VALUE y_i( BASE x ( i - 1 ) ) ELSE x )
y = y + z ) ).

View file

@ -0,0 +1,25 @@
list
eqindex(list l)
{
integer e, i, s, sum;
list x;
s = sum = 0;
l.ucall(add_i, 1, sum);
for (i, e in l) {
if (s * 2 + e == sum) {
x.append(i);
}
s += e;
}
x;
}
integer
main(void)
{
list(-7, 1, 5, 2, -4, 3, 0).eqindex.ucall(o_, 0, "\n");
0;
}

View file

@ -0,0 +1,66 @@
import extensions.
import system'routines.
import system'collections.
import extensions'routines.
class EquilibriumEnumerator :: Enumerator
{
int left.
int right.
int index.
enumerator en.
multi constructor new : object
<= new enumerator:object.
constructor new(Enumerator en)
[
@en := en.
$self reset.
]
constructor new(BaseEnumerable list)
<= new(list enumerator).
bool next
[
index += 1.
while(en next)
[
var element := en get.
right -= element.
bool found := (left == right).
left += element.
if (found)
[
^ true
].
index += 1.
].
^ false
]
reset
[
en reset.
left := 0.
right := en summarize.
index := -1.
en reset.
]
get = index.
}
program =
[
EquilibriumEnumerator new:(-7, 1, 5, 2, -4, 3, 0);
forEach:printingLn.
].

View file

@ -1,93 +1 @@
(() => {
// EQUILIBRIUM INDICES ----------------------------------------------------
// equilibriumIndices :: [Int] -> [Int]
const equilibriumIndices = xs =>
foldr((a, [x, y], i) =>
x === y ? cons(i, a) : a,
[],
zip(
scanl1(plus, xs), // Sums from the left
scanr1(plus, xs) // Sums from the right
)
);
// GENERIC FUNCTIONS ------------------------------------------------------
// cons :: a -> [a] -> [a]
const cons = (x, xs) => [x].concat(xs);
// foldr (a -> b -> a) -> a -> [b] -> a
const foldr = (f, a, xs) => xs.reduceRight(f, a);
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// plus :: Num a => a -> a -> a
const plus = (a, b) => a + b;
// scanl :: (b -> a -> b) -> b -> [a] -> [b]
const scanl = (f, startValue, xs) =>
xs.reduce((a, x) => {
const v = f(a.acc, x);
return {
acc: v,
scan: a.scan.concat(v)
};
}, {
acc: startValue,
scan: [startValue]
})
.scan;
// scanl1 :: (a -> a -> a) -> [a] -> [a]
const scanl1 = (f, xs) =>
xs.length > 0 ? scanl(f, xs[0], xs.slice(1)) : [];
// scanr :: (b -> a -> b) -> b -> [a] -> [b]
const scanr = (f, startValue, xs) =>
xs.reduceRight((a, x) => {
const v = f(a.acc, x);
return {
acc: v,
scan: [v].concat(a.scan)
};
}, {
acc: startValue,
scan: [startValue]
})
.scan;
// scanr1 :: (a -> a -> a) -> [a] -> [a]
const scanr1 = (f, xs) =>
xs.length > 0 ? scanr(f, xs.slice(-1)[0], xs.slice(0, -1)) : [];
// Any value -> optional number of indents -> String
// show :: a -> String
// show :: a -> Int -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [x[0], null, x[1]] : x
);
// tail :: [a] -> [a]
const tail = xs => xs.length ? xs.slice(1) : undefined;
// zip :: [a] -> [b] -> [(a,b)]
const zip = (xs, ys) =>
xs.slice(0, Math.min(xs.length, ys.length))
.map((x, i) => [x, ys[i]]);
// TEST -------------------------------------------------------------------
return show(
map(equilibriumIndices, [
[-7, 1, 5, 2, -4, 3, 0],
[2, 4, 6],
[2, 9, 2],
[1, -1, 1, -1, 1, -1, 1],
[1],
[]
])
);
// -> [[3, 6], [], [1], [0, 1, 2, 3, 4, 5, 6], [0], []]
})();
[[3,6],[],[1],[0,1,2,3,4,5,6],[0],[]]

View file

@ -1 +1,16 @@
[[3,6],[],[1],[0,1,2,3,4,5,6],[0],[]]
function equilibrium(arr) {
let sum = arr.reduce((a, b) => a + b);
let leftSum = 0;
for (let i = 0; i < arr.length; ++i) {
sum -= arr[i];
if (leftSum === sum) {
return i;
}
leftSum += arr[i];
}
return -1;
}

View file

@ -0,0 +1 @@
3, -1, 1, 0, 0

View file

@ -1,7 +1,5 @@
# v0.6.0
function equindex2pass(data::Array)
rst = Array{Int}([])
rst = Vector{Int}(0)
suml, sumr, ddelayed = 0, sum(data), 0
for (i, d) in enumerate(data)
suml += ddelayed
@ -11,7 +9,7 @@ function equindex2pass(data::Array)
push!(rst, i)
end
end
return length(rst) > 0 ? rst : nothing
return rst
end
@show equindex2pass([1, -1, 1, -1, 1, -1, 1])

View file

@ -0,0 +1,24 @@
class Rosetta {
function : Main(args : String[]) ~ Nil {
sequence := [-7, 1, 5, 2, -4, 3, 0];
EqulibriumIndices(sequence);
}
function : EqulibriumIndices(sequence : Int[]) ~ Nil {
# find total sum
totalSum := 0;
each(i : sequence) {
totalSum += sequence[i];
};
# compare running sum to remaining sum to find equlibrium indices
runningSum := 0;
each(i : sequence) {
n := sequence[i];
if (totalSum - runningSum - n = runningSum) {
i->PrintLine();
};
runningSum += n;
};
}
}