JSFiddle - React, Tailwind, and code Playground
by levrun
HTML
<script src="https://cdn.jsdelivr.net/jasmine/2.5.2/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/2.5.2/jasmine-html.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/2.5.2/boot.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/jasmine/2.5.2/jasmine.css">
TypeScript
/*
https://codility.com/programmers/lessons/2-arrays/cyclic_rotation/
Given a zero-indexed array A consisting of N integers and an integer K,
returns the array A rotated K times.
*/
class ArrayShift {
rotate(A, K) {
if(A.length === 0) {
return A;
}
var normalizedK = K;
while(normalizedK > (A.length - 1)) {
normalizedK = normalizedK - A.length;
}
if (normalizedK <= 0) {
return A;
}
var shiftedArray = [A.length];
var arrayLength = A.length;
for (var i = 0; i < arrayLength; i++) {
if(i + normalizedK >= arrayLength) {
shiftedArray[i + normalizedK - arrayLength] = A[i];
} else {
shiftedArray[i + normalizedK] = A[i];
}
}
return shiftedArray;
}
}
describe("Array shifting suite", function() {
it("Test that we can rotate array by 1", function(done) {
ArrayShift rotator = new ArrayShift();
inputArray = [1, 2, 3, 4];
inputK = 1;
resultArray = [4, 1, 2, 3];
expect(rotator.rotate(inputArray, inputK)).toEqual(resultArray);
setTimeout(done, 1000);
});
it("Test that we don't need to rotate array if K is 0", function(done) {
ArrayShift rotator = new ArrayShift();
inputArray = [1, 2, 3, 4];
inputK = 0;
resultArray = [1, 2, 3, 4];
expect(rotator.rotate(inputArray, inputK)).toEqual(resultArray);
setTimeout(done, 1000);
});
it("Test that we can rotate array by 2", function(done) {
ArrayShift rotator = new ArrayShift();
inputArray = [1, 2, 3, 4];
inputK = 2;
resultArray = [3, 4, 1, 2];
expect(rotator.rotate(inputArray, inputK)).toEqual(resultArray);
setTimeout(done, 1000);
});
it("Test that we empty array shifted to empty array", function(done) {
ArrayShift rotator = new ArrayShift();
inputArray = [];
inputK = 2;
resultArray = [];
...