Created
January 19, 2019 14:38
-
-
Save inesusvet/48547a0fea414fa0c9fc55fcdbd09fde to your computer and use it in GitHub Desktop.
Coding dojo in Minsk session result for 2019-01-19
This file contains hidden or 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
| """ | |
| See https://www.codewars.com/kata/all-that-is-open-must-be-closed-dot-dot-dot | |
| ## Background | |
| We all know about "balancing parentheses" (plus brackets, braces and chevrons) | |
| and even balancing characters that are identical. | |
| Read that last sentence again, I balanced different characters and identical | |
| characters twice and you didn't even notice... :) | |
| ## Kata | |
| Your challenge in this kata is to write a piece of code to validate that a | |
| supplied string is balanced. | |
| You must determine if all that is open is then closed, and nothing is closed | |
| which is not already open! | |
| You will be given a string to validate, and a second string, where each pair | |
| of characters defines an opening and closing sequence that needs balancing. | |
| You may assume that the second string always has an even number of characters. | |
| """ | |
| def filter_brackets(text, brackets): | |
| """ | |
| >>> filter_brackets('yes', '') | |
| '' | |
| >>> filter_brackets('()', '()') | |
| '()' | |
| >>> filter_brackets('((yes', '()') | |
| '((' | |
| """ | |
| result = [] | |
| for i in text: | |
| if i in brackets: | |
| result.append(i) | |
| return ''.join(result) | |
| def is_balanced(text, brackets): | |
| """ | |
| >>> is_balanced('', '()') | |
| True | |
| >>> is_balanced('Sensei says yes!', '()') | |
| True | |
| >>> is_balanced('))((', '{}()') | |
| False | |
| >>> is_balanced('(Sensei says yes!)', '()[]') | |
| True | |
| >>> is_balanced('(Sensei says no!', '()') | |
| False | |
| >>> is_balanced('(Sensei) (says) (yes!)', '()') | |
| True | |
| >>> is_balanced('(Sensei (says) yes!)', '()[]') | |
| True | |
| >>> is_balanced('((Sensei) says) no!)', '()[]{}') | |
| False | |
| >>> is_balanced('(Sensei (says) (yes!))', '()[]{}') | |
| True | |
| >>> is_balanced('(Sensei [says] yes)', '()[]') | |
| True | |
| >>> is_balanced('[Sensei (says] no)', '()[]') | |
| False | |
| >>> is_balanced('', '') | |
| True | |
| >>> is_balanced('([{}])', '()[]{}') | |
| True | |
| >>> is_balanced('([{]})', '()[]{}') | |
| False | |
| >>> is_balanced('!Senei says yes!', '!!') | |
| True | |
| >>> is_balanced('Sensei says no!', '!!') | |
| False | |
| >>> is_balanced('!Sensei (says) yes!', '!!()') | |
| True | |
| """ | |
| text = filter_brackets(text, brackets) | |
| opening = brackets[::2] | |
| closing = brackets[1::2] | |
| brackets = dict(zip(closing, opening)) | |
| state = [] | |
| for char in text: | |
| if char in brackets: | |
| #if len(state) == 0: | |
| # return False | |
| last = state.pop() if state else None | |
| if brackets[char] != last: | |
| return False | |
| if char in opening: | |
| state.append(char) | |
| return state == [] | |
| if __name__ == '__main__': | |
| import doctest | |
| failures, errors = doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment