Skip to content

Instantly share code, notes, and snippets.

@xcheff
Created June 13, 2023 12:55
Show Gist options
  • Save xcheff/d2a60fe280b6eb2d6b9c6392199c684f to your computer and use it in GitHub Desktop.
Save xcheff/d2a60fe280b6eb2d6b9c6392199c684f to your computer and use it in GitHub Desktop.
Bigger Bitwise sequence of array
const bitwise = (arr) => arr.reduce((bitwise, value) => bitwise & value);
const sequences = (array) =>
array.flatMap((_, index, arr) => {
return arr
.slice(index)
.map((_, innerIndex) => arr.slice(index, index + innerIndex + 1));
});
const analyzeSequences = (arr) =>
arr.map((data) => ({ bitwise: bitwise(data), length: data.length }));
const biggerNonZeroSeq = (data) =>
data.reduce((max, item) => {
if (item.bitwise === 0) {
return max;
} else {
return max > item.length ? max : item.length;
}
});
const showData = (data) => {
console.log(data);
return data;
};
const pipe =
(...fns) =>
(x) =>
fns.reduce((y, f) => f(y), x);
const getBiggerLengthNonZeroBitwise = pipe(
sequences,
showData,
analyzeSequences,
showData,
biggerNonZeroSeq
);
console.log(getBiggerLengthNonZeroBitwise([1, 2, 3, 4, 5, 6]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment