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

Updated the logic for early exit. Added missing condition for input #2

Merged
merged 1 commit into from
Dec 14, 2023
Merged
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
34 changes: 20 additions & 14 deletions identify_parenthesis_pattern.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
def check_pattern(input_pattern):
## First check if the count of '(' and ')' are same. If not, return False.
count_opening_braces = 0
count_closing_braces = 0
for char in input_pattern:
if char == '(':
count_opening_braces += 1
if char == ')':
count_closing_braces += 1
# count_opening_braces = 0
# count_closing_braces = 0
# for char in input_pattern:
# if char == '(':
# count_opening_braces += 1
# if char == ')':
# count_closing_braces += 1

if count_opening_braces != count_closing_braces:
# if count_opening_braces != count_closing_braces:
# print("unequal number of open and closing braces in the pattern detected")
# return False
if len(input_pattern) % 2 != 0:
print("unequal number of open and closing braces in the pattern detected")
return False
else:
Expand Down Expand Up @@ -50,11 +53,14 @@ def check_pattern(input_pattern):
# Inorder for the Github action to succed, Hardcoding the input with an example value.
# Code verified with user prompted input already.
#input_pattern = input("Enter the string pattern: ").strip()
input_pattern = "()()()()"
for char in input_pattern:
if char not in ('(', ')'):
raise ValueError("Only '(' and ')' allowed in the input pattern")
checked_result = check_pattern(input_pattern)
print(checked_result)
input_pattern = "()()()"
if input_pattern:
for char in input_pattern:
if char not in ('(', ')'):
raise ValueError("Only '(' and ')' allowed in the input pattern")
checked_result = check_pattern(input_pattern)
print(checked_result)
else:
raise ValueError("Only '(' and ')' allowed in the input pattern")
except ValueError as e:
print(f"{e.args[0]}")