summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.idea/vcs.xml6
-rw-r--r--sources/__init__.py0
-rw-r--r--sources/check_bracket.py35
-rw-r--r--tests/__init__.py0
-rw-r--r--tests/check_bracketTest.py58
5 files changed, 99 insertions, 0 deletions
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="VcsDirectoryMappings">
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
+ </component>
+</project> \ No newline at end of file
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))
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/check_bracketTest.py b/tests/check_bracketTest.py
new file mode 100644
index 0000000..40f97d0
--- /dev/null
+++ b/tests/check_bracketTest.py
@@ -0,0 +1,58 @@
+import unittest
+
+from sources.check_bracket import check_bracket
+
+
+class MyTestCase(unittest.TestCase):
+
+ def test1(self):
+ exp = "[]"
+ self.assertEqual("Success", check_bracket(exp))
+
+ def test2(self):
+ exp = "{}[]"
+ self.assertEqual("Success", check_bracket(exp))
+
+ def test3(self):
+ exp = "[()]"
+ self.assertEqual("Success", check_bracket(exp))
+
+ def test4(self):
+ exp = "(())"
+ self.assertEqual("Success", check_bracket(exp))
+
+ def test5(self):
+ exp = "{[]}()"
+ self.assertEqual("Success", check_bracket(exp))
+
+ def test6(self):
+ exp = "{"
+ self.assertEqual(1, check_bracket(exp))
+
+ def test7(self):
+ exp = "{[}"
+ self.assertEqual(3, check_bracket(exp))
+
+ def test8(self):
+ exp = "foo(bar)"
+ self.assertEqual("Success", check_bracket(exp))
+
+ def test9(self):
+ exp = "foo(bar[i);"
+ self.assertEqual(10, check_bracket(exp))
+
+ def test10(self):
+ exp = "[](()"
+ self.assertEqual(3, check_bracket(exp))
+
+ def test11(self):
+ exp = "{[}haha"
+ self.assertEqual(3, check_bracket(exp))
+
+ def test12(self):
+ exp = "haha{[}"
+ self.assertEqual(7, check_bracket(exp))
+
+
+if __name__ == '__main__':
+ unittest.main()