Skip to content

Commit 7cfb06f

Browse files
committed
Adds testing for the get mazePath problem knaxus#48
1 parent 1cd6613 commit 7cfb06f

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
5050
- [FizzBuzz](src/_Problems_/fizzbuzz)
5151
- [String Permutaions](src/_Problems_/get-string-permutations)
5252
- [Get Subsequence](src/_Problems_/get_subsequence)
53-
- [Get Maze Path](src/_Problems_/get_subsequence)
53+
- [Get Maze Path](src/_Problems_/get-mazePath)
5454
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
5555
- [Get Max Char](src/_Problems_/maxchar)
5656
- [Get Smallest Common Number](src/_Problems_/get-smallest-common-number)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { getMazePath } = require('.');
2+
3+
describe('Get maze path', () => {
4+
it('returns all possible solutions for a 2x2 grid', () => {
5+
const expectedSolutions = ['HHVV', 'HVHV', 'HVVH', 'VHHV', 'VHVH', 'VVHH'];
6+
7+
expect(getMazePath(0, 0, 2, 2)).toEqual(expectedSolutions);
8+
});
9+
10+
it('returns an even amount of horizontal and vertical movements', () => {
11+
const solutions = getMazePath(0, 0, 3, 3);
12+
13+
solutions.forEach(solution => {
14+
expect(solution.length).toEqual(6);
15+
16+
expect(solution.match(/H/g).length).toEqual(3);
17+
expect(solution.match(/V/g).length).toEqual(3);
18+
});
19+
});
20+
21+
it('returns the expected number of solutions based on given grids', () => {
22+
expect(getMazePath(0, 0, 1, 1).length).toEqual(2);
23+
expect(getMazePath(0, 0, 2, 2).length).toEqual(6);
24+
expect(getMazePath(0, 0, 3, 3).length).toEqual(20);
25+
expect(getMazePath(0, 0, 4, 4).length).toEqual(70);
26+
27+
expect(getMazePath(1, 1, 4, 4).length).toEqual(20);
28+
});
29+
30+
it('returns an empty array when the start and end coordinates are equal', () => {
31+
const solutions = getMazePath(2, 2, 2, 2);
32+
33+
expect(solutions).toEqual(['']);
34+
});
35+
36+
it('returns an empty array when the start coordinates are greater than the end coordinates', () => {
37+
const solutions = getMazePath(2, 2, 1, 1);
38+
39+
expect(solutions).toEqual([]);
40+
});
41+
});

src/_Problems_/get-mazePath/index.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
// --->> er = end row
88
// --->> ec = end column
99

10-
11-
12-
13-
let getMazePath = (cr, cc, er, ec) => {
10+
const getMazePath = (cr, cc, er, ec) => {
1411
if(cr == er && cc == ec) { //============POSITIVE BASE CASE===========
1512
let br = [];
1613
br.push('');
@@ -37,6 +34,4 @@ let getMazePath = (cr, cc, er, ec) => {
3734
return myResult;
3835
}
3936

40-
41-
let path = getMazePath(0, 0, 2, 2);
42-
console.log(path);
37+
module.exports = { getMazePath };

0 commit comments

Comments
 (0)