-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Solution for issue #1 * Create valid-parenthesis_ljsauer.py * Delete sum-of-all-numbers_ljsauer.py
- Loading branch information
1 parent
568e927
commit 6eb5472
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 | ||
|
||
|
||
|