From 3eda3649a33d88a17fb8ebc358c1e2091a163f12 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 20 Jan 2026 04:16:34 -0600 Subject: [PATCH] adding algo --- .../ex_54_basic_calculator.py | 50 +++++++++++++++++++ .../test_54_basic_calculator_round_22.ts | 43 ++++++++++++++++ .../test_54_basic_calculator_round_22.py | 23 +++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_54_basic_calculator.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/test_54_basic_calculator_round_22.ts create mode 100644 tests/test_150_questions_round_22/test_54_basic_calculator_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_54_basic_calculator.py b/src/my_project/interviews/top_150_questions_round_22/ex_54_basic_calculator.py new file mode 100644 index 00000000..3553ae64 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_54_basic_calculator.py @@ -0,0 +1,50 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + + def calculate(self, s: str) -> int: + """ + Evaluate a basic calculator expression with +, -, and parentheses. + Uses a stack to handle nested parentheses. + + Time: O(n), Space: O(n) + """ + stack = [] + result = 0 + current_num = 0 + sign = 1 # 1 for positive, -1 for negative + + for char in s: + if char.isdigit(): + # Build multi-digit number + current_num = current_num * 10 + int(char) + elif char == '+': + # Add previous number with its sign + result += sign * current_num + current_num = 0 + sign = 1 + elif char == '-': + # Add previous number with its sign + result += sign * current_num + current_num = 0 + sign = -1 + elif char == '(': + # Save current result and sign to stack + stack.append(result) + stack.append(sign) + # Reset for new sub-expression + result = 0 + sign = 1 + elif char == ')': + # Complete current number + result += sign * current_num + current_num = 0 + # Pop sign and previous result from stack + result *= stack.pop() # Apply sign before parenthesis + result += stack.pop() # Add previous result + + # Add the last number + result += sign * current_num + + return result \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/test_54_basic_calculator_round_22.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/test_54_basic_calculator_round_22.ts new file mode 100644 index 00000000..6243e01a --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/test_54_basic_calculator_round_22.ts @@ -0,0 +1,43 @@ +function calculate(s: string): number { + const stack: number[] = []; + let result = 0; + let currentNum = 0; + let sign = 1; // 1 for positive, -1 for negative + + for (const char of s) { + if (char >= '0' && char <= '9') { + // Build multi-digit number + currentNum = currentNum * 10 + parseInt(char); + } else if (char === '+') { + // Add previous number with its sign + result += sign * currentNum; + currentNum = 0; + sign = 1; + } else if (char === '-') { + // Add previous number with its sign + result += sign * currentNum; + currentNum = 0; + sign = -1; + } else if (char === '(') { + // Save current result and sign to stack + stack.push(result); + stack.push(sign); + // Reset for new sub-expression + result = 0; + sign = 1; + } else if (char === ')') { + // Complete current number + result += sign * currentNum; + currentNum = 0; + // Pop sign and previous result from stack + result *= stack.pop()!; // Apply sign before parenthesis + result += stack.pop()!; // Add previous result + } + // Skip spaces + } + + // Add the last number + result += sign * currentNum; + + return result; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_54_basic_calculator_round_22.py b/tests/test_150_questions_round_22/test_54_basic_calculator_round_22.py new file mode 100644 index 00000000..35ddc52f --- /dev/null +++ b/tests/test_150_questions_round_22/test_54_basic_calculator_round_22.py @@ -0,0 +1,23 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_54_basic_calculator import Solution + +class BasicCalculatorTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.calculate(s = "1 + 1") + target = 2 + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.calculate(s = " 2-1 + 2 ") + target = 3 + self.assertEqual(output, target) + + def test_third_pattern(self): + solution = Solution() + output = solution.calculate(s = "(1+(4+5+2)-3)+(6+8)") + target = 23 + self.assertEqual(output, target) \ No newline at end of file