Created
March 5, 2019 02:13
-
-
Save BurhanH/079327740c2ebae4bd769fe65f684647 to your computer and use it in GitHub Desktop.
balanced bracket list
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
# -*- coding: utf-8 -*- | |
# Requirements: | |
# get diff for balanced bracket list | |
# string as input, you can use only '(' or ')' symbols | |
# length is 1 to 1000 | |
# Examples: | |
# str = '((()))'; Answer is : 0 | |
# str = '((()'; Answer is : 2 | |
# str = '())())(('; Answer is : 4 | |
def check(string): | |
if 0 < len(string) < 1000: | |
lst = list(string) | |
balance = 0 | |
counter = 0 | |
for x in lst: | |
if x == '(': | |
balance += 1 | |
else: | |
balance -= 1 | |
if balance < 0: | |
counter += 1 | |
balance = 0 | |
return print(balance + counter) | |
else: | |
return print('The string length should be between 1 and 1000!') | |
check('((()))') | |
# 0 | |
check('((()') | |
# 2 | |
check('())())((') | |
# 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment