Skip to content

Commit 1b8b556

Browse files
committed
feat: add rubiks cube art solution
1 parent c8d8504 commit 1b8b556

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

codewars/rubiks-cube-art/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Rubik's Cube Art
2+
3+
## Description
4+
5+
To complete this kata you will have to finish a function that returns a string of characters which when printed resemble a Rubik's cube. The function is named cube, and it has one integer parameter (formal argument) n, for the dimensions of the cube.
6+
7+
For example, when I call the function cube(3) it will return a string which when printed appears as so:
8+
9+
```txt
10+
/\_\_\_\
11+
/\/\_\_\_\
12+
/\/\/\_\_\_\
13+
\/\/\/_/_/_/
14+
\/\/_/_/_/
15+
\/_/_/_/
16+
```
17+
18+
There are no spaces to the right of the cube edges (same above and below the cube), and it must work for all dimensions greater than, or equal to one (technically 1 x 1 x 1):
19+
20+
1 x 1 x 1
21+
22+
```txt
23+
/\_\
24+
\/_/
25+
```
26+
27+
2 x 2 x 2
28+
29+
```txt
30+
/\_\_\
31+
/\/\_\_\
32+
\/\/_/_/
33+
\/_/_/
34+
```

codewars/rubiks-cube-art/solution.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function cube(size) {
2+
const topSide = [];
3+
const bottomSide = [];
4+
Array.from({ length: size }, (_, i) => i + 1).forEach((i) => {
5+
topSide.push(' '.repeat(size - i) + '/\\'.repeat(i) + '_\\'.repeat(size));
6+
bottomSide.push(' '.repeat(size - i) + '\\/'.repeat(i) + '_/'.repeat(size));
7+
});
8+
return topSide.concat(bottomSide.reverse()).join('\n');
9+
}
10+
11+
module.exports = cube;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const cube = require('./solution');
2+
3+
describe("Rubik's cube art", () => {
4+
const testCases = [
5+
{
6+
input: 1,
7+
output: '/\\_\\\n\\/_/',
8+
},
9+
{
10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: ' /\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
39+
+ ' /\\/\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
40+
+ ' /\\/\\/\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
41+
+ ' /\\/\\/\\/\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
42+
+ ' /\\/\\/\\/\\/\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
43+
+ ' /\\/\\/\\/\\/\\/\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
44+
+ ' /\\/\\/\\/\\/\\/\\/\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
45+
+ ' /\\/\\/\\/\\/\\/\\/\\/\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
46+
+ '/\\/\\/\\/\\/\\/\\/\\/\\/\\_\\_\\_\\_\\_\\_\\_\\_\\_\\\n'
47+
+ '\\/\\/\\/\\/\\/\\/\\/\\/\\/_/_/_/_/_/_/_/_/_/\n'
48+
+ ' \\/\\/\\/\\/\\/\\/\\/\\/_/_/_/_/_/_/_/_/_/\n'
49+
+ ' \\/\\/\\/\\/\\/\\/\\/_/_/_/_/_/_/_/_/_/\n'
50+
+ ' \\/\\/\\/\\/\\/\\/_/_/_/_/_/_/_/_/_/\n'
51+
+ ' \\/\\/\\/\\/\\/_/_/_/_/_/_/_/_/_/\n'
52+
+ ' \\/\\/\\/\\/_/_/_/_/_/_/_/_/_/\n'
53+
+ ' \\/\\/\\/_/_/_/_/_/_/_/_/_/\n'
54+
+ ' \\/\\/_/_/_/_/_/_/_/_/_/\n'
55+
+ ' \\/_/_/_/_/_/_/_/_/_/',
56+
},
57+
];
58+
59+
it('should return a string type', () => {
60+
expect(typeof cube(1)).toBe('string');
61+
});
62+
63+
it.each(testCases)('should return a cube with size $input', (testCase) => {
64+
expect(cube(testCase.input)).toBe(testCase.output);
65+
});
66+
});

0 commit comments

Comments
 (0)