Skip to content

chore: format code #1515

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 2 commits into from
Oct 12, 2023
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
9 changes: 7 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [SumOfSubset](Backtracking/SumOfSubset.js)
* **Bit-Manipulation**
* [BinaryCountSetBits](Bit-Manipulation/BinaryCountSetBits.js)
* [GrayCodes](Bit-Manipulation/GrayCodes.js)
* [IsPowerofFour](Bit-Manipulation/IsPowerofFour.js)
* [IsPowerOfTwo](Bit-Manipulation/IsPowerOfTwo.js)
* [LogTwo](Bit-Manipulation/LogTwo.js)
Expand Down Expand Up @@ -55,6 +56,7 @@
* [OctToDecimal](Conversions/OctToDecimal.js)
* [OuncesToKilograms](Conversions/OuncesToKilograms.js)
* [RailwayTimeConversion](Conversions/RailwayTimeConversion.js)
* [RgbHslConversion](Conversions/RgbHslConversion.js)
* [RgbHsvConversion](Conversions/RgbHsvConversion.js)
* [RGBToHex](Conversions/RGBToHex.js)
* [RomanToDecimal](Conversions/RomanToDecimal.js)
Expand All @@ -72,14 +74,14 @@
* [Graph2](Data-Structures/Graph/Graph2.js)
* [Graph3](Data-Structures/Graph/Graph3.js)
* **Heap**
* [BinaryHeap](Data-Structures/Heap/BinaryHeap.js)
* [KeyPriorityQueue](Data-Structures/Heap/KeyPriorityQueue.js)
* [MaxHeap](Data-Structures/Heap/MaxHeap.js)
* [MinHeap](Data-Structures/Heap/MinHeap.js)
* [MinPriorityQueue](Data-Structures/Heap/MinPriorityQueue.js)
* **Linked-List**
* [AddTwoNumbers](Data-Structures/Linked-List/AddTwoNumbers.js)
* [CycleDetection](Data-Structures/Linked-List/CycleDetection.js)
* [DoublyLinkedList](Data-Structures/Linked-List/DoublyLinkedList.js)
* [MergeTwoSortedLinkedLists](Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js)
* [ReverseSinglyLinkedList](Data-Structures/Linked-List/ReverseSinglyLinkedList.js)
* [SinglyCircularLinkedList](Data-Structures/Linked-List/SinglyCircularLinkedList.js)
* [SinglyLinkedList](Data-Structures/Linked-List/SinglyLinkedList.js)
Expand All @@ -88,6 +90,7 @@
* [Queue](Data-Structures/Queue/Queue.js)
* [QueueUsing2Stacks](Data-Structures/Queue/QueueUsing2Stacks.js)
* **Stack**
* [EvaluateExpression](Data-Structures/Stack/EvaluateExpression.js)
* [Stack](Data-Structures/Stack/Stack.js)
* [StackES6](Data-Structures/Stack/StackES6.js)
* **Tree**
Expand Down Expand Up @@ -179,6 +182,7 @@
* [DecimalExpansion](Maths/DecimalExpansion.js)
* [DecimalIsolate](Maths/DecimalIsolate.js)
* [DegreeToRadian](Maths/DegreeToRadian.js)
* [Determinant](Maths/Determinant.js)
* [EuclideanDistance](Maths/EuclideanDistance.js)
* [EulerMethod](Maths/EulerMethod.js)
* [EulersTotient](Maths/EulersTotient.js)
Expand Down Expand Up @@ -239,6 +243,7 @@
* [RadianToDegree](Maths/RadianToDegree.js)
* [ReverseNumber](Maths/ReverseNumber.js)
* [ReversePolishNotation](Maths/ReversePolishNotation.js)
* [RowEchelon](Maths/RowEchelon.js)
* [ShorsAlgorithm](Maths/ShorsAlgorithm.js)
* [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js)
* [SieveOfEratosthenesIntArray](Maths/SieveOfEratosthenesIntArray.js)
Expand Down
2 changes: 1 addition & 1 deletion Data-Structures/Heap/test/BinaryHeap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('BinaryHeap', () => {
it('should handle insertion of duplicate values', () => {
// Check if the heap handles duplicate values correctly
minHeap.insert(2)
console.log(minHeap.heap);
console.log(minHeap.heap)
expect(minHeap.heap).toEqual([1, 3, 2, 4, 8, 6, 2])
})

Expand Down
40 changes: 20 additions & 20 deletions Data-Structures/Stack/EvaluateExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,53 @@
* @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid.
*/
function evaluatePostfixExpression(expression) {
const stack = [];
const stack = []

// Helper function to perform an operation and push the result to the stack. Returns success.
function performOperation(operator) {
const rightOp = stack.pop(); // Right operand is the top of the stack
const leftOp = stack.pop(); // Left operand is the next item on the stack
const rightOp = stack.pop() // Right operand is the top of the stack
const leftOp = stack.pop() // Left operand is the next item on the stack

if (leftOp === undefined || rightOp === undefined) {
return false; // Invalid expression
return false // Invalid expression
}
switch (operator) {
case '+':
stack.push(leftOp + rightOp);
break;
stack.push(leftOp + rightOp)
break
case '-':
stack.push(leftOp - rightOp);
break;
stack.push(leftOp - rightOp)
break
case '*':
stack.push(leftOp * rightOp);
break;
stack.push(leftOp * rightOp)
break
case '/':
if (rightOp === 0) {
return false;
return false
}
stack.push(leftOp / rightOp);
break;
stack.push(leftOp / rightOp)
break
default:
return false; // Unknown operator
return false // Unknown operator
}
return true;
return true
}

const tokens = expression.split(/\s+/);
const tokens = expression.split(/\s+/)

for (const token of tokens) {
if (!isNaN(parseFloat(token))) {
// If the token is a number, push it to the stack
stack.push(parseFloat(token));
stack.push(parseFloat(token))
} else {
// If the token is an operator, perform the operation
if (!performOperation(token)) {
return null; // Invalid expression
return null // Invalid expression
}
}
}

return (stack.length === 1) ? stack[0] : null;
return stack.length === 1 ? stack[0] : null
}

export { evaluatePostfixExpression };
export { evaluatePostfixExpression }
29 changes: 14 additions & 15 deletions Data-Structures/Stack/test/EvaluateExpression.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { evaluatePostfixExpression } from '../EvaluateExpression.js';
import { evaluatePostfixExpression } from '../EvaluateExpression.js'

describe('evaluatePostfixExpression', () => {
it('should evaluate a valid expression', () => {
const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 11
const result = evaluatePostfixExpression(expression);
expect(result).toBe(11);
});
const expression = '3 4 * 2 / 5 +' // (3 * 4) / 2 + 5 = 11
const result = evaluatePostfixExpression(expression)
expect(result).toBe(11)
})

it('should handle division by zero', () => {
const expression = '3 0 /'; // Division by zero
const result = evaluatePostfixExpression(expression);
expect(result).toBe(null);
});
const expression = '3 0 /' // Division by zero
const result = evaluatePostfixExpression(expression)
expect(result).toBe(null)
})

it('should handle an invalid expression', () => {
const expression = '3 * 4 2 / +'; // Invalid expression
const result = evaluatePostfixExpression(expression);
expect(result).toBe(null);
});

});
const expression = '3 * 4 2 / +' // Invalid expression
const result = evaluatePostfixExpression(expression)
expect(result).toBe(null)
})
})
9 changes: 3 additions & 6 deletions Maths/test/Determinant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ describe('Determinant', () => {
'Square matrix is required.'
],
[[1, 3, 2, [5, 8, 6], 3], 'Input is not a valid 2D matrix.']
])(
'Should return the error message.',
(matrix, expected) => {
expect(() => determinant(matrix)).toThrowError(expected)
}
)
])('Should return the error message.', (matrix, expected) => {
expect(() => determinant(matrix)).toThrowError(expected)
})
})