Skip to content

Commit ce24cd4

Browse files
committed
feat: add array rotation algorithms
1 parent 08d8c6b commit ce24cd4

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Rotates an array to the right by k positions
3+
* @param {number[]} array - The array to rotate
4+
* @param {number} k - Number of positions to rotate
5+
* @returns {number[]} - New array rotated to the right by k positions
6+
* @throws {TypeError} - If input is not an array
7+
*/
8+
export function rotateRight(array, k) {
9+
if (!Array.isArray(array)) {
10+
throw new TypeError('Input must be an array')
11+
}
12+
13+
if (!Number.isInteger(k) || k < 0) {
14+
throw new TypeError('Rotation count must be a non-negative integer')
15+
}
16+
17+
const length = array.length
18+
if (length === 0) return []
19+
20+
const normalizedK = k % length
21+
const rotated = new Array(length)
22+
23+
for (let i = 0; i < length; i++) {
24+
rotated[(i + normalizedK) % length] = array[i]
25+
}
26+
27+
return rotated
28+
}
29+
30+
/**
31+
* Rotates an array to the left by k positions
32+
* @param {number[]} array - The array to rotate
33+
* @param {number} k - Number of positions to rotate
34+
* @returns {number[]} - New array rotated to the left by k positions
35+
* @throws {TypeError} - If input is not an array
36+
*/
37+
export function rotateLeft(array, k) {
38+
if (!Array.isArray(array)) {
39+
throw new TypeError('Input must be an array')
40+
}
41+
42+
if (!Number.isInteger(k) || k < 0) {
43+
throw new TypeError('Rotation count must be a non-negative integer')
44+
}
45+
46+
const length = array.length
47+
if (length === 0) return []
48+
49+
const normalizedK = k % length
50+
const rotated = new Array(length)
51+
52+
for (let i = 0; i < length; i++) {
53+
rotated[i] = array[(i + normalizedK) % length]
54+
}
55+
56+
return rotated
57+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { rotateRight, rotateLeft } from '../RotateArray.js'
2+
3+
describe('rotateRight', () => {
4+
it('should rotate array to the right by k positions', () => {
5+
expect(rotateRight([1, 2, 3, 4, 5], 2)).toEqual([4, 5, 1, 2, 3])
6+
expect(rotateRight([1, 2, 3], 1)).toEqual([3, 1, 2])
7+
})
8+
9+
it('should handle rotation count larger than array length', () => {
10+
expect(rotateRight([1, 2, 3], 5)).toEqual([2, 3, 1])
11+
expect(rotateRight([1], 10)).toEqual([1])
12+
})
13+
14+
it('should return empty array for empty input', () => {
15+
expect(rotateRight([], 3)).toEqual([])
16+
})
17+
18+
it('should handle zero rotation', () => {
19+
expect(rotateRight([1, 2, 3], 0)).toEqual([1, 2, 3])
20+
})
21+
22+
it('should throw TypeError for invalid inputs', () => {
23+
expect(() => rotateRight('not an array', 1)).toThrow(TypeError)
24+
expect(() => rotateRight([1, 2, 3], -1)).toThrow(TypeError)
25+
expect(() => rotateRight([1, 2, 3], 1.5)).toThrow(TypeError)
26+
})
27+
})
28+
29+
describe('rotateLeft', () => {
30+
it('should rotate array to the left by k positions', () => {
31+
expect(rotateLeft([1, 2, 3, 4, 5], 2)).toEqual([3, 4, 5, 1, 2])
32+
expect(rotateLeft([1, 2, 3], 1)).toEqual([2, 3, 1])
33+
})
34+
35+
it('should handle rotation count larger than array length', () => {
36+
expect(rotateLeft([1, 2, 3], 5)).toEqual([3, 1, 2])
37+
expect(rotateLeft([1], 10)).toEqual([1])
38+
})
39+
40+
it('should return empty array for empty input', () => {
41+
expect(rotateLeft([], 3)).toEqual([])
42+
})
43+
44+
it('should handle zero rotation', () => {
45+
expect(rotateLeft([1, 2, 3], 0)).toEqual([1, 2, 3])
46+
})
47+
48+
it('should throw TypeError for invalid inputs', () => {
49+
expect(() => rotateLeft('not an array', 1)).toThrow(TypeError)
50+
expect(() => rotateLeft([1, 2, 3], -1)).toThrow(TypeError)
51+
expect(() => rotateLeft([1, 2, 3], 1.5)).toThrow(TypeError)
52+
})
53+
})

0 commit comments

Comments
 (0)