Skip to content

Commit 93af26e

Browse files
committed
Merge branch 'master' of github.com:knaxus/problem-solving-javascript into problems
2 parents 4a75147 + 19222fb commit 93af26e

File tree

6 files changed

+108
-11
lines changed

6 files changed

+108
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
5151
- [FizzBuzz](src/_Problems_/fizzbuzz)
5252
- [String Permutaions](src/_Problems_/get-string-permutations)
5353
- [Get Subsequence](src/_Problems_/get_subsequence)
54-
- [Get Maze Path](src/_Problems_/get_subsequence)
54+
- [Get Maze Path](src/_Problems_/get-mazePath)
5555
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
5656
- [Get Max Char](src/_Problems_/maxchar)
5757
- [Get Smallest Common Number](src/_Problems_/get-smallest-common-number)

src/_DataStructures_/DoublyLinkedList/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ class DoublyLinkedList {
5555

5656
display() {
5757
let address = this.head.next;
58+
let addresses = []
5859
while (address !== this.tail) {
59-
console.log(address.data);
60+
addresses.push(address.data)
6061
address = address.next;
6162
}
63+
return addresses
6264
}
6365
}
6466

src/_DataStructures_/Stack/postfix-expression-evaluation/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ function evaluatePostfixExpression(expression) {
1515
s.push(Number(char));
1616
} else {
1717
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
18-
//push the result to stack
18+
//push the result to stack
1919
let val1 = s.pop();
20-
let val2 = s.pop()
20+
let val2 = s.pop();
2121
switch (char) {
2222
case '+':
2323
s.push(val2 + val1);
@@ -38,3 +38,7 @@ function evaluatePostfixExpression(expression) {
3838
//pop the value of postfix expression
3939
return s.pop();
4040
}
41+
42+
module.exports = {
43+
evaluatePostfixExpression,
44+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { evaluatePostfixExpression } = require('.');
2+
3+
describe('Postfix expression evaluation', function () {
4+
it('should be a function', function () {
5+
expect(typeof evaluatePostfixExpression).toEqual('function');
6+
});
7+
8+
it('should return a number', function () {
9+
const expression = '11+';
10+
11+
expect(typeof evaluatePostfixExpression(expression)).toEqual('number')
12+
});
13+
14+
it('should handle addition', function () {
15+
const expression = '23+';
16+
const expected = 5;
17+
18+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
19+
});
20+
21+
it('should handle subtraction', function () {
22+
const expression = '54-';
23+
const expected = 1;
24+
25+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
26+
});
27+
28+
it('should handle multiplication', function () {
29+
const expression = '34*';
30+
const expected = 12;
31+
32+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
33+
});
34+
35+
it('should handle division', function () {
36+
const expression = '62/';
37+
const expected = 3;
38+
39+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
40+
});
41+
42+
it('should handle negative numbers', function () {
43+
const expression = '25-';
44+
const expected = -3;
45+
46+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
47+
});
48+
49+
it('should handle multiple operators', function () {
50+
const expression = '123*+';
51+
const expected = 7;
52+
53+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
54+
});
55+
});
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)