From ce24cd4fd2dece2e7af31afd5e961e1616b7a786 Mon Sep 17 00:00:00 2001 From: Zahir-R Date: Fri, 3 Oct 2025 13:10:39 -0400 Subject: [PATCH 1/3] feat: add array rotation algorithms --- Data-Structures/Array/RotateArray.js | 57 +++++++++++++++++++ .../Array/test/RotateArray.test.js | 53 +++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 Data-Structures/Array/RotateArray.js create mode 100644 Data-Structures/Array/test/RotateArray.test.js diff --git a/Data-Structures/Array/RotateArray.js b/Data-Structures/Array/RotateArray.js new file mode 100644 index 0000000000..35e885d97b --- /dev/null +++ b/Data-Structures/Array/RotateArray.js @@ -0,0 +1,57 @@ +/** + * Rotates an array to the right by k positions + * @param {number[]} array - The array to rotate + * @param {number} k - Number of positions to rotate + * @returns {number[]} - New array rotated to the right by k positions + * @throws {TypeError} - If input is not an array + */ +export function rotateRight(array, k) { + if (!Array.isArray(array)) { + throw new TypeError('Input must be an array') + } + + if (!Number.isInteger(k) || k < 0) { + throw new TypeError('Rotation count must be a non-negative integer') + } + + const length = array.length + if (length === 0) return [] + + const normalizedK = k % length + const rotated = new Array(length) + + for (let i = 0; i < length; i++) { + rotated[(i + normalizedK) % length] = array[i] + } + + return rotated +} + +/** + * Rotates an array to the left by k positions + * @param {number[]} array - The array to rotate + * @param {number} k - Number of positions to rotate + * @returns {number[]} - New array rotated to the left by k positions + * @throws {TypeError} - If input is not an array + */ +export function rotateLeft(array, k) { + if (!Array.isArray(array)) { + throw new TypeError('Input must be an array') + } + + if (!Number.isInteger(k) || k < 0) { + throw new TypeError('Rotation count must be a non-negative integer') + } + + const length = array.length + if (length === 0) return [] + + const normalizedK = k % length + const rotated = new Array(length) + + for (let i = 0; i < length; i++) { + rotated[i] = array[(i + normalizedK) % length] + } + + return rotated +} diff --git a/Data-Structures/Array/test/RotateArray.test.js b/Data-Structures/Array/test/RotateArray.test.js new file mode 100644 index 0000000000..5f3b4930fa --- /dev/null +++ b/Data-Structures/Array/test/RotateArray.test.js @@ -0,0 +1,53 @@ +import { rotateRight, rotateLeft } from '../RotateArray.js' + +describe('rotateRight', () => { + it('should rotate array to the right by k positions', () => { + expect(rotateRight([1, 2, 3, 4, 5], 2)).toEqual([4, 5, 1, 2, 3]) + expect(rotateRight([1, 2, 3], 1)).toEqual([3, 1, 2]) + }) + + it('should handle rotation count larger than array length', () => { + expect(rotateRight([1, 2, 3], 5)).toEqual([2, 3, 1]) + expect(rotateRight([1], 10)).toEqual([1]) + }) + + it('should return empty array for empty input', () => { + expect(rotateRight([], 3)).toEqual([]) + }) + + it('should handle zero rotation', () => { + expect(rotateRight([1, 2, 3], 0)).toEqual([1, 2, 3]) + }) + + it('should throw TypeError for invalid inputs', () => { + expect(() => rotateRight('not an array', 1)).toThrow(TypeError) + expect(() => rotateRight([1, 2, 3], -1)).toThrow(TypeError) + expect(() => rotateRight([1, 2, 3], 1.5)).toThrow(TypeError) + }) +}) + +describe('rotateLeft', () => { + it('should rotate array to the left by k positions', () => { + expect(rotateLeft([1, 2, 3, 4, 5], 2)).toEqual([3, 4, 5, 1, 2]) + expect(rotateLeft([1, 2, 3], 1)).toEqual([2, 3, 1]) + }) + + it('should handle rotation count larger than array length', () => { + expect(rotateLeft([1, 2, 3], 5)).toEqual([3, 1, 2]) + expect(rotateLeft([1], 10)).toEqual([1]) + }) + + it('should return empty array for empty input', () => { + expect(rotateLeft([], 3)).toEqual([]) + }) + + it('should handle zero rotation', () => { + expect(rotateLeft([1, 2, 3], 0)).toEqual([1, 2, 3]) + }) + + it('should throw TypeError for invalid inputs', () => { + expect(() => rotateLeft('not an array', 1)).toThrow(TypeError) + expect(() => rotateLeft([1, 2, 3], -1)).toThrow(TypeError) + expect(() => rotateLeft([1, 2, 3], 1.5)).toThrow(TypeError) + }) +}) From 1d07bc76576c561dc0b5abd0110aa420ff24e908 Mon Sep 17 00:00:00 2001 From: Zahir-R Date: Fri, 3 Oct 2025 13:23:15 -0400 Subject: [PATCH 2/3] feat: add array rotation algorithms --- Data-Structures/Array/RotateArray.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Data-Structures/Array/RotateArray.js b/Data-Structures/Array/RotateArray.js index 35e885d97b..d8248f0ac2 100644 --- a/Data-Structures/Array/RotateArray.js +++ b/Data-Structures/Array/RotateArray.js @@ -1,5 +1,6 @@ /** * Rotates an array to the right by k positions + * @see https://en.wikipedia.org/wiki/Circular_shift * @param {number[]} array - The array to rotate * @param {number} k - Number of positions to rotate * @returns {number[]} - New array rotated to the right by k positions @@ -29,6 +30,7 @@ export function rotateRight(array, k) { /** * Rotates an array to the left by k positions + * @see https://en.wikipedia.org/wiki/Circular_shift * @param {number[]} array - The array to rotate * @param {number} k - Number of positions to rotate * @returns {number[]} - New array rotated to the left by k positions From f7e91ec467b6c2b3071e18af797f1ac0048cd936 Mon Sep 17 00:00:00 2001 From: Zahir-R Date: Fri, 3 Oct 2025 13:58:40 -0400 Subject: [PATCH 3/3] feat: add array rotation algorithms --- Data-Structures/Array/RotateArray.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Data-Structures/Array/RotateArray.js b/Data-Structures/Array/RotateArray.js index d8248f0ac2..7228829041 100644 --- a/Data-Structures/Array/RotateArray.js +++ b/Data-Structures/Array/RotateArray.js @@ -6,7 +6,7 @@ * @returns {number[]} - New array rotated to the right by k positions * @throws {TypeError} - If input is not an array */ -export function rotateRight(array, k) { +const rotateRight = (array, k) => { if (!Array.isArray(array)) { throw new TypeError('Input must be an array') } @@ -36,7 +36,7 @@ export function rotateRight(array, k) { * @returns {number[]} - New array rotated to the left by k positions * @throws {TypeError} - If input is not an array */ -export function rotateLeft(array, k) { +const rotateLeft = (array, k) => { if (!Array.isArray(array)) { throw new TypeError('Input must be an array') } @@ -57,3 +57,5 @@ export function rotateLeft(array, k) { return rotated } + +export { rotateRight, rotateLeft }