From 3b519f99863d2afb087ca232040d5151ba0fd501 Mon Sep 17 00:00:00 2001 From: Sergiy Goloviatinski Date: Sun, 6 Oct 2019 16:03:52 +0200 Subject: [PATCH 1/2] Solved #36 --- python/intermediate/valid-parenthesis_sg.py | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 python/intermediate/valid-parenthesis_sg.py diff --git a/python/intermediate/valid-parenthesis_sg.py b/python/intermediate/valid-parenthesis_sg.py new file mode 100755 index 0000000..0024b68 --- /dev/null +++ b/python/intermediate/valid-parenthesis_sg.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +def has_valid_parens(s): + if len(s) == 0: + return True + + # maps closing brackets to their opening equivalent + close_open_parens = { + ')': '(', + ']': '[', + '}': '{' + } + + stack = [] + + for c in s: + if c in close_open_parens.values(): # opening brackets + stack.append(c) + if c in close_open_parens.keys(): # closing brackets + if close_open_parens[c] != stack[-1]: + return False + else: + stack.pop() + + return len(stack) == 0 + + +if __name__ == "__main__": + print(has_valid_parens("()")) + print(has_valid_parens("()[]{}")) + print(has_valid_parens("(]")) + print(has_valid_parens("([)]")) + print(has_valid_parens("{[]}")) + print(has_valid_parens("((((((((())))))))()")) From 66a8188a7b4695bc55a04755f539eb2964e9d7fc Mon Sep 17 00:00:00 2001 From: Sergiy Goloviatinski Date: Sun, 6 Oct 2019 16:17:28 +0200 Subject: [PATCH 2/2] Solved #1 --- python/beginner/sum-of-all-numbers_sg.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 python/beginner/sum-of-all-numbers_sg.py diff --git a/python/beginner/sum-of-all-numbers_sg.py b/python/beginner/sum-of-all-numbers_sg.py new file mode 100755 index 0000000..534776b --- /dev/null +++ b/python/beginner/sum-of-all-numbers_sg.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +def sum_all(l): + low = l[0] + high = l[-1] + + if low > high: + low, high = high, low + + return sum([i for i in range(low, high+1)]) + + +if __name__ == "__main__": + print(sum_all([1, 4])) + print(sum_all([4, 1]))