This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Solved #91 Added : balanced Parenthesis #95
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce20c84
Solved #91 Added : balanced Parenthesis
sameersrivastava13 5a1a5a9
Refactored : balanced_paranthsis.py
sameersrivastava13 59a1f2f
Refactored : balanced_parentheses.py
sameersrivastava13 adb16b5
Solved #91 refactored : balanced Parenthesis
sameersrivastava13 9d69518
Solved #91 Added : balanced Parentheses
sameersrivastava13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
openBracketList = ["[", "{", "("] | ||
closeBracketList = ["]", "}", ")"] | ||
|
||
|
||
def checkParentheses(data: str) -> str: | ||
""" | ||
checkParentheses() : Will take a string as an arguement and each time when an open parentheses is encountered | ||
will push it in the stack, and when closed parenthesis is encountered, | ||
will match it with the top of stack and pop it. | ||
|
||
Parameters: | ||
data (str): takes a string. | ||
|
||
Returns: | ||
str: Returns a string value whether string passed is balanced or Unbalanced. | ||
""" | ||
stack = [] | ||
for index in data: | ||
if index in openBracketList: | ||
stack.append(index) | ||
elif index in closeBracketList: | ||
position = closeBracketList.index(index) | ||
if (len(stack) > 0) and ( | ||
openBracketList[position] == stack[len(stack) - 1] | ||
): | ||
stack.pop() | ||
else: | ||
return "Unbalanced" | ||
if len(stack) == 0: | ||
return "Balanced" | ||
else: | ||
return "Unbalanced" | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
data = input("Enter the string to check:\t") | ||
result = checkParentheses(data) | ||
print(result) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.