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

Remove_Invalid_Parenthesis.py #12340

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 52 additions & 0 deletions backtracking/remove_invalid_parenthesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
def isParenthesis(c):

Check failure on line 1 in backtracking/remove_invalid_parenthesis.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/remove_invalid_parenthesis.py:1:5: N802 Function name `isParenthesis` should be lowercase
return (c == "(") or (c == ")")

Check failure on line 2 in backtracking/remove_invalid_parenthesis.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1714)

backtracking/remove_invalid_parenthesis.py:2:12: PLR1714 Consider merging multiple comparisons: `c in ("(", ")")`. Use a `set` if the elements are hashable.


def isValidString(str):

Check failure on line 5 in backtracking/remove_invalid_parenthesis.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/remove_invalid_parenthesis.py:5:5: N802 Function name `isValidString` should be lowercase

Check failure on line 5 in backtracking/remove_invalid_parenthesis.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (A002)

backtracking/remove_invalid_parenthesis.py:5:19: A002 Argument `str` is shadowing a Python builtin
cnt = 0
for i in range(len(str)):
if str[i] == "(":
cnt += 1
elif str[i] == ")":
cnt -= 1
if cnt < 0:
return False
return cnt == 0


# method to remove invalid parenthesis
def removeInvalidParenthesis(str):

Check failure on line 18 in backtracking/remove_invalid_parenthesis.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/remove_invalid_parenthesis.py:18:5: N802 Function name `removeInvalidParenthesis` should be lowercase

Check failure on line 18 in backtracking/remove_invalid_parenthesis.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (A002)

backtracking/remove_invalid_parenthesis.py:18:30: A002 Argument `str` is shadowing a Python builtin
if len(str) == 0:
return

visit = set()

q = []
temp = 0
level = 0

q.append(str)
visit.add(str)
while len(q):
str = q[0]

Check failure on line 31 in backtracking/remove_invalid_parenthesis.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (A001)

backtracking/remove_invalid_parenthesis.py:31:9: A001 Variable `str` is shadowing a Python builtin
Comment on lines +19 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try running pre --commit [ More on Contribution.md ]

q.pop(0)
if isValidString(str):
print(str)

level = True
if level:
continue
for i in range(len(str)):
if not isParenthesis(str[i]):
continue

temp = str[0:i] + str[i + 1 :]
if temp not in visit:
q.append(temp)
visit.add(temp)


expression = "()())()"
removeInvalidParenthesis(expression)
expression = "()v)"
removeInvalidParenthesis(expression)
Loading