Skip to content

Commit 4229b2d

Browse files
committed
Snail algorithm
1 parent d7d049c commit 4229b2d

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ There might still be some personal comments added for my learning purposes.
3838
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/sorting/sorting.test.js)
3939

4040
- **All**
41+
* Snail
42+
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/snail/snail.js)
43+
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/snail/snail.test.js)
4144
* Knapsack
4245
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/kanpsack/knapsack.js)
4346
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/kanpsack/knapsack.test.js)

src/snail/snail.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Problem: SnailSort is an algorithm that takes an array of equal-length sub-arrays, and then merges them into a single array in a clockwise spiral, starting from the upper-left hand corner.
2+
// Time complexity:
3+
const arrayMatrix = [
4+
[1, 2, 3, 4, 5],
5+
[6, 7, 8, 9, 10],
6+
[11, 12, 13, 14, 15],
7+
[16, 17, 18, 19, 20],
8+
[21, 22, 23, 24, 25],
9+
];
10+
11+
// const arrayMatrix = [
12+
// [1, 2, 3],
13+
// [6, 7, 8],
14+
// [11, 12, 13],
15+
// ];
16+
const snail = (arrayMatrix) => {
17+
const finalArray = [];
18+
while (arrayMatrix.length > 0) {
19+
arrayMatrix[0].forEach((el) => finalArray.push(el));
20+
arrayMatrix.splice(0, 1);
21+
arrayMatrix.forEach((arr) => {
22+
if (arrayMatrix.indexOf(arr) !== arrayMatrix.length - 1) {
23+
finalArray.push(arr[arr.length - 1]);
24+
arr.pop();
25+
}
26+
});
27+
if (arrayMatrix[arrayMatrix.length - 1]) {
28+
arrayMatrix[arrayMatrix.length - 1]
29+
.reverse()
30+
.forEach((el) => finalArray.push(el));
31+
arrayMatrix.pop();
32+
}
33+
arrayMatrix.reverse();
34+
arrayMatrix.forEach((arr) => {
35+
if (arrayMatrix.indexOf(arr) !== arrayMatrix.length - 1) {
36+
finalArray.push(arr[0]);
37+
arr.shift();
38+
}
39+
});
40+
arrayMatrix.reverse();
41+
}
42+
return finalArray;
43+
};
44+
45+
export default snail

src/snail/snail.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import snail from "./snail";
2+
3+
describe("Snail", () => {
4+
test("Snail", () => {
5+
expect(
6+
snail([
7+
[1, 2, 3],
8+
[6, 7, 8],
9+
[11, 12, 13],
10+
])
11+
).toStrictEqual([1, 2, 3, 8, 13, 12, 11, 6, 7]);
12+
});
13+
test("Snail", () => {
14+
expect(
15+
snail([
16+
[1, 2, 3, 4],
17+
[5, 6, 7, 8],
18+
[9, 10, 11, 12],
19+
[13, 14, 15, 16],
20+
])
21+
).toStrictEqual([
22+
1,
23+
2,
24+
3,
25+
4,
26+
8,
27+
12,
28+
16,
29+
15,
30+
14,
31+
13,
32+
9,
33+
5,
34+
6,
35+
7,
36+
11,
37+
10,
38+
]);
39+
});
40+
test("Snail", () => {
41+
expect(
42+
snail([
43+
[1, 2, 3, 4, 5],
44+
[6, 7, 8, 9, 10],
45+
[11, 12, 13, 14, 15],
46+
[16, 17, 18, 19, 20],
47+
[21, 22, 23, 24, 25],
48+
])
49+
).toStrictEqual([
50+
1,
51+
2,
52+
3,
53+
4,
54+
5,
55+
10,
56+
15,
57+
20,
58+
25,
59+
24,
60+
23,
61+
22,
62+
21,
63+
16,
64+
11,
65+
6,
66+
7,
67+
8,
68+
9,
69+
14,
70+
19,
71+
18,
72+
17,
73+
12,
74+
13,
75+
]);
76+
});
77+
});

0 commit comments

Comments
 (0)