Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Equilibrium-index/JavaScript/equilibrium-index-1.js
Normal file
19
Task/Equilibrium-index/JavaScript/equilibrium-index-1.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
function equilibrium(a) {
|
||||
var N = a.length, i, l = [], r = [], e = []
|
||||
for (l[0] = a[0], r[N - 1] = a[N - 1], i = 1; i<N; i++)
|
||||
l[i] = l[i - 1] + a[i], r[N - i - 1] = r[N - i] + a[N - i - 1]
|
||||
for (i = 0; i < N; i++)
|
||||
if (l[i] === r[i]) e.push(i)
|
||||
return e
|
||||
}
|
||||
|
||||
// test & output
|
||||
[ [-7, 1, 5, 2, -4, 3, 0], // 3, 6
|
||||
[2, 4, 6], // empty
|
||||
[2, 9, 2], // 1
|
||||
[1, -1, 1, -1, 1, -1, 1], // 0,1,2,3,4,5,6
|
||||
[1], // 0
|
||||
[] // empty
|
||||
].forEach(function(x) {
|
||||
console.log(equilibrium(x))
|
||||
});
|
||||
1
Task/Equilibrium-index/JavaScript/equilibrium-index-2.js
Normal file
1
Task/Equilibrium-index/JavaScript/equilibrium-index-2.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
[[3,6],[],[1],[0,1,2,3,4,5,6],[0],[]]
|
||||
16
Task/Equilibrium-index/JavaScript/equilibrium-index-3.js
Normal file
16
Task/Equilibrium-index/JavaScript/equilibrium-index-3.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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;
|
||||
}
|
||||
1
Task/Equilibrium-index/JavaScript/equilibrium-index-4.js
Normal file
1
Task/Equilibrium-index/JavaScript/equilibrium-index-4.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
3, -1, 1, 0, 0
|
||||
109
Task/Equilibrium-index/JavaScript/equilibrium-index-5.js
Normal file
109
Task/Equilibrium-index/JavaScript/equilibrium-index-5.js
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// ------------- ALL EQUILIBRIUM INDICES -------------
|
||||
|
||||
// equilibriumIndices :: [Int] -> [Int]
|
||||
const equilibriumIndices = xs =>
|
||||
zip(
|
||||
scanl1(add)(xs)
|
||||
)(
|
||||
scanr1(add)(xs)
|
||||
)
|
||||
.reduceRight(
|
||||
(a, xy, i) => xy[0] === xy[1] ? (
|
||||
[i, ...a]
|
||||
) : a, []
|
||||
);
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () => [
|
||||
[-7, 1, 5, 2, -4, 3, 0],
|
||||
[2, 4, 6],
|
||||
[2, 9, 2],
|
||||
[1, -1, 1, -1, 1, -1, 1],
|
||||
[1],
|
||||
[]
|
||||
].map(compose(
|
||||
JSON.stringify,
|
||||
equilibriumIndices
|
||||
))
|
||||
.join("\n");
|
||||
// -> [[3, 6], [], [1], [0, 1, 2, 3, 4, 5, 6], [0], []]
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// add (+) :: Num a => a -> a -> a
|
||||
const add = a =>
|
||||
// Curried addition.
|
||||
b => a + b;
|
||||
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
// A function defined by the right-to-left
|
||||
// composition of all the functions in fs.
|
||||
fs.reduce(
|
||||
(f, g) => x => f(g(x)),
|
||||
x => x
|
||||
);
|
||||
|
||||
|
||||
// scanl :: (b -> a -> b) -> b -> [a] -> [b]
|
||||
const scanl = f => startValue => xs =>
|
||||
// The series of interim values arising
|
||||
// from a catamorphism. Parallel to foldl.
|
||||
xs.reduce((a, x) => {
|
||||
const v = f(a[0])(x);
|
||||
|
||||
return [v, a[1].concat(v)];
|
||||
}, [startValue, [startValue]])[1];
|
||||
|
||||
|
||||
// scanl1 :: (a -> a -> a) -> [a] -> [a]
|
||||
const scanl1 = f =>
|
||||
// scanl1 is a variant of scanl that has no
|
||||
// starting value argument.
|
||||
xs => xs.length > 0 ? (
|
||||
scanl(f)(
|
||||
xs[0]
|
||||
)(xs.slice(1))
|
||||
) : [];
|
||||
|
||||
|
||||
// scanr :: (a -> b -> b) -> b -> [a] -> [b]
|
||||
const scanr = f =>
|
||||
startValue => xs => xs.reduceRight(
|
||||
(a, x) => {
|
||||
const v = f(x)(a[0]);
|
||||
|
||||
return [v, [v].concat(a[1])];
|
||||
}, [startValue, [startValue]]
|
||||
)[1];
|
||||
|
||||
|
||||
// scanr1 :: (a -> a -> a) -> [a] -> [a]
|
||||
const scanr1 = f =>
|
||||
// scanr1 is a variant of scanr that has no
|
||||
// seed-value argument, and assumes that
|
||||
// xs is not empty.
|
||||
xs => xs.length > 0 ? (
|
||||
scanr(f)(
|
||||
xs.slice(-1)[0]
|
||||
)(xs.slice(0, -1))
|
||||
) : [];
|
||||
|
||||
|
||||
// zip :: [a] -> [b] -> [(a, b)]
|
||||
const zip = xs =>
|
||||
// The paired members of xs and ys, up to
|
||||
// the length of the shorter of the two lists.
|
||||
ys => Array.from({
|
||||
length: Math.min(xs.length, ys.length)
|
||||
}, (_, i) => [xs[i], ys[i]]);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue