Skip to content
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

Added postfix expression evaluation problem #33

Merged
merged 20 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Baseball Game](src/_DataStructures_/Stack/baseball-game)
- [Minimum Stack](src/_DataStructures_/Stack/min-stack)
- [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis)
- [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation)
- [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits)
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)


- [Queue](src/_DataStructures_/Queue)
- [Weave](src/_DataStructures_/Queue/weave)
Expand Down
16 changes: 10 additions & 6 deletions src/_Classics_/fibonacci/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
// the algorithm has time complexity of O(n^2), very bad!
function fibonacci(position) {
// if position is 1 or 2, the number in fibonacci sequence will be 1
if (position < 3) {
return 1;
if (position <= 1) {
return position;
}

// else the element in fibonacci sequence will be the sum of
// element at position(p) (p -1) and (p - 2)
return fibonacci(position - 2) + fibonacci(position - 1);
Expand All @@ -24,8 +26,8 @@ function fibonacciMemoized(index, cache) {
if (cache[index]) {
return cache[index];
} else {
if (index < 3) {
return 1;
if (index <=1) {
return index;
} else {
cache[index] =
fibonacciMemoized(index - 1, cache) +
Expand All @@ -41,7 +43,9 @@ function fibonacciMemoized(index, cache) {

function fibonacciTabular(n) {
const table = [0, 1];

if (n <= 1) {
return n;
}
for (let i = 2; i <= n; i += 1) {
table[i] = table[i - 1] + table[i - 2];
}
Expand All @@ -54,4 +58,4 @@ function fibonacciTabular(n) {
// console.log(`Fib normal - ${fibonacci(number)}`);
// console.log('--');
// console.log(`Fib memo - ${fibonacciMemoized(number)}`);
// console.log(`Fib table - ${fibonacciTabular(number)}`);
// console.log(`Fib table - ${fibonacciTabular(number)}`);
40 changes: 40 additions & 0 deletions src/_DataStructures_/Stack/postfix-expression-evaluation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Evaluation of Postfix Expression
* Input:456*+
* Output:34
*/

const Stack = require('../index');

function evaluatePostfixExpression(expression) {
let s = new Stack();
for (let i = 0; i < expression.length; i++) {
const char = expression[i];
if (!isNaN(char)) {
//if number push the char onto stack
s.push(Number(char));
} else {
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
//push the result to stack
let val1 = s.pop();
let val2 = s.pop()
switch (char) {
case '+':
s.push(val2 + val1);
break;
case '-':
s.push(val2 - val1);
break;
case '*':
s.push(val2 * val1);
break;
case '/':
s.push(val2 / val1);
break;

}
}
}
//pop the value of postfix expression
return s.pop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Given an integer N, remove consecutive repeated digits from it.
* Input:133445
* Output:1345
*/

const Stack = require('../index');


function removeConsecutiveDigits(no) {
let s = new Stack();
let newNo = "";
//initally push first digit into stack
newNo += no[0];
s.push(no[0]);
for (let i = 1; i < no.length; i++) {
const digit = no[i];
//if stack top and incoming digit is same ignore it else append to newNo.
if (s.peek() !== digit) {
newNo += digit;
s.push(digit);
}
}
return newNo
}