summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authorHaidong Ji2019-01-02 16:09:25 -0600
committerHaidong Ji2019-01-02 16:09:25 -0600
commit374f0d359768aa2891b2a6dc24f195f0ec277ab2 (patch)
treefd09cb125e4e2be2472e1e66b1896aee56e82872 /sources
Check bracket done!
Strict TDD is not possible, at least the type I'm used to through Eclipse PyDev, where I write tests first, and rely on PyDev to generate files/function stubs. Bummer, but I decided to stick with PyCharm just to learn it.
Diffstat (limited to 'sources')
-rw-r--r--sources/__init__.py0
-rw-r--r--sources/check_bracket.py35
2 files changed, 35 insertions, 0 deletions
diff --git a/sources/__init__.py b/sources/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sources/__init__.py
diff --git a/sources/check_bracket.py b/sources/check_bracket.py
new file mode 100644
index 0000000..bd7fa6e
--- /dev/null
+++ b/sources/check_bracket.py
@@ -0,0 +1,35 @@
+# Uses python3
+def check_bracket(exp):
+ char_stack = []
+ unmatched_opening_bracket = 0
+ count = 0
+ brackets = ['[', ']', '(', ')', '{', '}']
+ for i in range(len(exp)):
+ c = exp[i]
+ if c not in brackets:
+ count = count + 1
+ continue
+ if c == '[' or c == '(' or c == '{':
+ char_stack.append(c)
+ count = count + 1
+ unmatched_opening_bracket = count
+ else:
+ if len(char_stack) == 0:
+ return i + 1
+ top = char_stack[-1]
+ del (char_stack[-1])
+ if (top == '[' and c != ']') or (top == '(' and c != ')') or (top == '{' and c != '}'):
+ return i + 1
+ else:
+ unmatched_opening_bracket = unmatched_opening_bracket - 1
+ count = count + 1
+ if len(char_stack) == 0:
+ return "Success"
+ else:
+ if unmatched_opening_bracket > 0:
+ return unmatched_opening_bracket
+ return count
+
+
+exp = input()
+print(check_bracket(exp))