Skip to content

Instantly share code, notes, and snippets.

@nicolas-cherel
Last active December 6, 2018 14:13
Show Gist options
  • Save nicolas-cherel/0cc5a10b0bf422c72dc53c99d50bbd4b to your computer and use it in GitHub Desktop.
Save nicolas-cherel/0cc5a10b0bf422c72dc53c99d50bbd4b to your computer and use it in GitHub Desktop.
// 1.1
data.split(/\n/g).map(e => parseInt(e, 10)).reduce((m, v) => m+v, 0)
// 1.2
Array.from({
[Symbol.iterator]() {
const list = data.split(/\n/g).map(e => parseInt(e, 10));
let index = 0;
let sum = 0;
const seen = { 0: 0 };
let done = false;
return {
next() {
const current = list[index];
sum += current;
Object.assign(seen, { [sum]: (seen[sum] || 0) + 1 })
try {
return {
value: { sum, count: seen[sum] },
done,
}
} finally {
index = ((index+1) % list.length)
done = seen[sum] > 1;
}
}
}
}
}).slice(-1)[0].sum
// 2.1
Object.values(
data
.split(/\n/g)
.map((e) => Array.from(e).sort().join('').match(/((\w)\2+)/g))
.reduce((memo, t) => (
Object.assign(memo,
Object.keys(
t.map(t => t.length).reduce(
(m, i) => Object.assign(m, { [i]: 1 }), {}
)
).reduce(
(m3, v) => Object.assign(m3, { [v]: (m3[v] || 0) + 1 }), memo
)
)), {})
).reduce((m, v) => m*v, 1)
// 2.2
data
.split(/\n/g)
.map((t1, index, list) => (
[
list.find(
(t2) => (
1 === Array.from(t2).reduce(
(count, c, index) => (
count += c === t1[index] ? 0 : 1
),
0
)
)
),
t1
]
))
.filter(([a, b]) => Boolean(a))
.map(([a, b]) => {
const ary = Array.from(a);
ary.splice(Array.from(a).findIndex((c, index) => c !== b[index]), 1);
return ary.join('');
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment