Skip to content

Adds testing for the get mazePath problem #48 #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions src/_Problems_/get-mazePath/get-mazePath.test.js
Original file line number Diff line number Diff line change
@@ -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([]);
});
});
9 changes: 2 additions & 7 deletions src/_Problems_/get-mazePath/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand All @@ -37,6 +34,4 @@ let getMazePath = (cr, cc, er, ec) => {
return myResult;
}


let path = getMazePath(0, 0, 2, 2);
console.log(path);
module.exports = { getMazePath };