Skip to content

Commit

Permalink
Solution for issue #36 (#148)
Browse files Browse the repository at this point in the history
* Solution for issue #1

* Create valid-parenthesis_ljsauer.py

* Delete sum-of-all-numbers_ljsauer.py
  • Loading branch information
ljsauer authored and the-vampiire committed Oct 21, 2019
1 parent 568e927 commit 6eb5472
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions python/intermediate/valid-parenthesis_ljsauer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Challenge: Given a string containing only one or more from "()[]{}"
as input, determine if the input string is valid.
Input is valid if:
> Open brackets are closed by the same type of bracket
> Open brackets are closed in the correct order
> Input is empty
"""
def is_valid():
string = input("Enter some parentheses: ")

numOpen_p = 0
numClosed_p = 0
numOpen_k = 0
numClosed_k = 0
numOpen_c = 0
numClosed_c = 0

for char in string:
if char == '(':
numOpen_p +=1
if numClosed_p > numOpen_p:
return False
elif char == '[':
numOpen_k +=1
if numClosed_k > numOpen_k:
return False
elif char == '{':
numOpen_c +=1
if numClosed_c > numOpen_c:
return False
elif char == ')':
numClosed_p +=1
if numClosed_p >= numOpen_p:
return False
elif char == ']':
numClosed_k +=1
if numClosed_k >= numOpen_k:
return False
elif char == '}':
numClosed_c +=1
if numClosed_c >= numOpen_c:
return False
elif string == '':
return True

if numOpen_p > numClosed_p:
return False
elif numOpen_k > numClosed_k:
return False
elif numOpen_c > numClosed_c:
return False
else:
return True



0 comments on commit 6eb5472

Please sign in to comment.