From 6eb5472e846650b73e75e04f717bdf500d171052 Mon Sep 17 00:00:00 2001 From: Lillie Sauer <48888021+ljsauer@users.noreply.github.com> Date: Sun, 20 Oct 2019 21:37:01 -0500 Subject: [PATCH] Solution for issue #36 (#148) * Solution for issue #1 * Create valid-parenthesis_ljsauer.py * Delete sum-of-all-numbers_ljsauer.py --- .../intermediate/valid-parenthesis_ljsauer.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 python/intermediate/valid-parenthesis_ljsauer.py diff --git a/python/intermediate/valid-parenthesis_ljsauer.py b/python/intermediate/valid-parenthesis_ljsauer.py new file mode 100644 index 0000000..e6ffa5e --- /dev/null +++ b/python/intermediate/valid-parenthesis_ljsauer.py @@ -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 + + +