Published
Edited
Jun 5, 2022
Importers
2 stars
Insert cell
Insert cell
Insert cell
circularShift([...Array(10).keys()], k)
Insert cell
Insert cell
circularShift = {
const reverseSlice = function reverseSlice(A, start, end) {
for (let i = 0; i < (end - start) >>> 1; ++i) {
const x = A[start+i];
A[start+i] = A[end-i-1];
A[end-i-1] = x;
}
return A;
}
return function circularShift(A, k) {
const n = A.length;
k = (k % n + n) % n; // wrap k if it is negative or bigger than n
reverseSlice(A, 0, n);
reverseSlice(A, 0, k);
reverseSlice(A, k, n);
return A;
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more