Created
April 3, 2019 04:18
-
-
Save miketheinkman/9589e40f48a9620dea00df483f201408 to your computer and use it in GitHub Desktop.
Interview question with irritating (but super simple once the pressure was off) edge case
This file contains 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
def brace_sanity_check(test_string): | |
"""Return True if braces brackets and parens are sane | |
""" | |
open_paren = 0 | |
open_bracket = 0 | |
open_brace = 0 | |
for s in test_string: | |
if s == "(": | |
required_for_close = "paren" | |
open_paren += 1 | |
elif s == ")": | |
open_paren -= 1 | |
if open_paren < 0 or required_for_close != "paren": | |
return False | |
elif s == "{": | |
required_for_close = "brace" | |
open_brace += 1 | |
elif s == "}": | |
open_brace -= 1 | |
if open_brace < 0 or required_for_close != "brace": | |
return False | |
elif s == "[": | |
required_for_close = "bracket" | |
open_bracket += 1 | |
elif s == "]": | |
open_bracket -= 1 | |
if open_bracket < 0 or required_for_close != "bracket": | |
return False | |
if not any([open_brace, open_bracket, open_paren]): | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment