From 0192a9ac62c12e535b68f2c8e8af13260c191dc8 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 24 Nov 2024 04:33:06 -0600 Subject: [PATCH] adding valid parentheses --- .../valid_parentheses.py | 16 +++++++++++++++ .../test_valid_parentheses_round_11.py | 20 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_11/valid_parentheses.py create mode 100644 tests/test_150_questions_round_11/test_valid_parentheses_round_11.py diff --git a/src/my_project/interviews/top_150_questions_round_11/valid_parentheses.py b/src/my_project/interviews/top_150_questions_round_11/valid_parentheses.py new file mode 100644 index 00000000..0f29d052 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_11/valid_parentheses.py @@ -0,0 +1,16 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def isValid(self, s): + + dic_parentheses = {'(':')','[':']','{':'}'} + check_lst = list() + + for p in s: + if p in dic_parentheses: + check_lst.append(p) + elif len(check_lst) == 0 \ + or p != dic_parentheses[check_lst.pop()]: + return False + return len(check_lst) == 0 \ No newline at end of file diff --git a/tests/test_150_questions_round_11/test_valid_parentheses_round_11.py b/tests/test_150_questions_round_11/test_valid_parentheses_round_11.py new file mode 100644 index 00000000..85416b4a --- /dev/null +++ b/tests/test_150_questions_round_11/test_valid_parentheses_round_11.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_11\ +.valid_parentheses import Solution + +class ValidParenthesesTestCase(unittest.TestCase): + + def test_is_valid_parentheses(self): + solution = Solution() + output = solution.isValid(s='()') + self.assertTrue(output) + + def test_is_no_valid_parentheses_pattern_1(self): + solution = Solution() + output = solution.isValid(s='(]') + self.assertFalse(output) + + def test_is_no_valid_parentheses_pattern_2(self): + solution = Solution() + output = solution.isValid(s='((') + self.assertFalse(output) \ No newline at end of file