Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f6d18ed

Browse files
Merge pull request #33 from SumeetHaryani/master
Added postfix expression evaluation problem
2 parents 17eebb1 + c1508a8 commit f6d18ed

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

‎README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
2626
- [Baseball Game](src/_DataStructures_/Stack/baseball-game)
2727
- [Minimum Stack](src/_DataStructures_/Stack/min-stack)
2828
- [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis)
29+
- [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation)
30+
- [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits)
2931
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
32+
3033

3134
- [Queue](src/_DataStructures_/Queue)
3235
- [Weave](src/_DataStructures_/Queue/weave)

‎src/_Classics_/fibonacci/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
//The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
12
// the algorithm has time complexity of O(n^2), very bad!
23
function fibonacci(position) {
34
// if position is 1 or 2, the number in fibonacci sequence will be 1
4-
if (position <3) {
5-
return 1;
5+
if (position <=1) {
6+
return position;
67
}
8+
79
// else the element in fibonacci sequence will be the sum of
810
// element at position(p) (p -1) and (p - 2)
911
return fibonacci(position - 2) + fibonacci(position - 1);
@@ -24,8 +26,8 @@ function fibonacciMemoized(index, cache) {
2426
if (cache[index]) {
2527
return cache[index];
2628
} else {
27-
if (index <3) {
28-
return 1;
29+
if (index <=1) {
30+
return index;
2931
} else {
3032
cache[index] =
3133
fibonacciMemoized(index - 1, cache) +
@@ -41,7 +43,9 @@ function fibonacciMemoized(index, cache) {
4143

4244
function fibonacciTabular(n) {
4345
const table = [0, 1];
44-
46+
if (n <= 1) {
47+
return n;
48+
}
4549
for (let i = 2; i <= n; i += 1) {
4650
table[i] = table[i - 1] + table[i - 2];
4751
}
@@ -54,4 +58,4 @@ function fibonacciTabular(n) {
5458
// console.log(`Fib normal - ${fibonacci(number)}`);
5559
// console.log('--');
5660
// console.log(`Fib memo - ${fibonacciMemoized(number)}`);
57-
// console.log(`Fib table - ${fibonacciTabular(number)}`);
61+
// console.log(`Fib table - ${fibonacciTabular(number)}`);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Evaluation of Postfix Expression
3+
* Input:456*+
4+
* Output:34
5+
*/
6+
7+
const Stack = require('../index');
8+
9+
function evaluatePostfixExpression(expression) {
10+
let s = new Stack();
11+
for (let i = 0; i < expression.length; i++) {
12+
const char = expression[i];
13+
if (!isNaN(char)) {
14+
//if number push the char onto stack
15+
s.push(Number(char));
16+
} else {
17+
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
18+
//push the result to stack
19+
let val1 = s.pop();
20+
let val2 = s.pop()
21+
switch (char) {
22+
case '+':
23+
s.push(val2 + val1);
24+
break;
25+
case '-':
26+
s.push(val2 - val1);
27+
break;
28+
case '*':
29+
s.push(val2 * val1);
30+
break;
31+
case '/':
32+
s.push(val2 / val1);
33+
break;
34+
35+
}
36+
}
37+
}
38+
//pop the value of postfix expression
39+
return s.pop();
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Given an integer N, remove consecutive repeated digits from it.
3+
* Input:133445
4+
* Output:1345
5+
*/
6+
7+
const Stack = require('../index');
8+
9+
10+
function removeConsecutiveDigits(no) {
11+
let s = new Stack();
12+
let newNo = "";
13+
//initally push first digit into stack
14+
newNo += no[0];
15+
s.push(no[0]);
16+
for (let i = 1; i < no.length; i++) {
17+
const digit = no[i];
18+
//if stack top and incoming digit is same ignore it else append to newNo.
19+
if (s.peek() !== digit) {
20+
newNo += digit;
21+
s.push(digit);
22+
}
23+
}
24+
return newNo
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /