Skip to content

Instantly share code, notes, and snippets.

@BurhanH
Created March 5, 2019 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurhanH/079327740c2ebae4bd769fe65f684647 to your computer and use it in GitHub Desktop.
Save BurhanH/079327740c2ebae4bd769fe65f684647 to your computer and use it in GitHub Desktop.
balanced bracket list
# -*- 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