diff --git a/README.md b/README.md index 665bc09c..c1a3374a 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [FizzBuzz](src/_Problems_/fizzbuzz) - [String Permutaions](src/_Problems_/get-string-permutations) - [Get Subsequence](src/_Problems_/get_subsequence) -- [Get Maze Path](src/_Problems_/get_subsequence) +- [Get Maze Path](src/_Problems_/get-mazePath) - [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s) - [Get Max Char](src/_Problems_/maxchar) - [Get Smallest Common Number](src/_Problems_/get-smallest-common-number) diff --git a/src/_Problems_/get-mazePath/get-mazePath.test.js b/src/_Problems_/get-mazePath/get-mazePath.test.js new file mode 100644 index 00000000..b30f50af --- /dev/null +++ b/src/_Problems_/get-mazePath/get-mazePath.test.js @@ -0,0 +1,41 @@ +const { getMazePath } = require('.'); + +describe('Get maze path', () => { + it('returns all possible solutions for a 2x2 grid', () => { + const expectedSolutions = ['HHVV', 'HVHV', 'HVVH', 'VHHV', 'VHVH', 'VVHH']; + + expect(getMazePath(0, 0, 2, 2)).toEqual(expectedSolutions); + }); + + it('returns an even amount of horizontal and vertical movements', () => { + const solutions = getMazePath(0, 0, 3, 3); + + solutions.forEach(solution => { + expect(solution.length).toEqual(6); + + expect(solution.match(/H/g).length).toEqual(3); + expect(solution.match(/V/g).length).toEqual(3); + }); + }); + + it('returns the expected number of solutions based on given grids', () => { + expect(getMazePath(0, 0, 1, 1).length).toEqual(2); + expect(getMazePath(0, 0, 2, 2).length).toEqual(6); + expect(getMazePath(0, 0, 3, 3).length).toEqual(20); + expect(getMazePath(0, 0, 4, 4).length).toEqual(70); + + expect(getMazePath(1, 1, 4, 4).length).toEqual(20); + }); + + it('returns an empty array when the start and end coordinates are equal', () => { + const solutions = getMazePath(2, 2, 2, 2); + + expect(solutions).toEqual(['']); + }); + + it('returns an empty array when the start coordinates are greater than the end coordinates', () => { + const solutions = getMazePath(2, 2, 1, 1); + + expect(solutions).toEqual([]); + }); +}); \ No newline at end of file diff --git a/src/_Problems_/get-mazePath/index.js b/src/_Problems_/get-mazePath/index.js index 99aead53..cff29bee 100644 --- a/src/_Problems_/get-mazePath/index.js +++ b/src/_Problems_/get-mazePath/index.js @@ -7,10 +7,7 @@ // --->> er = end row // --->> ec = end column - - - -let getMazePath = (cr, cc, er, ec) => { +const getMazePath = (cr, cc, er, ec) => { if(cr == er && cc == ec) { //============POSITIVE BASE CASE=========== let br = []; br.push(''); @@ -37,6 +34,4 @@ let getMazePath = (cr, cc, er, ec) => { return myResult; } - -let path = getMazePath(0, 0, 2, 2); -console.log(path); \ No newline at end of file +module.exports = { getMazePath }; \ No newline at end of file