Skip to content

Latest commit

 

History

History
119 lines (97 loc) · 4.47 KB

README.md

File metadata and controls

119 lines (97 loc) · 4.47 KB

Balanced parentheses

Collab Py Download Sphinx Unix Unix REMOVED

Version 1.

Cheks if input file has balanced parentheses. Returns 'Well parenthesed' or 'Bad parenthesed'.

'Well parenthesed' condition:

  • there is as many open par. as closing par.;
  • any prefix text contains at least as many opening par. as closing;
  • no closing parenthesis of one kind followed an opening parenthesis of another kind.
def parentheses_checker1(file):
    """
    Returns the phrase 'Well parenthesed' if each open parenthesis has
    the matching close one.
    In the opposite case, returns the phrase 'Bad parenthesed'.
    :param file: file ( .txt, .py, etc.)
    :return: 'Well parenthesed'/'Bad parenthesed'
    :rtype: str
    :UC: file is an external file
    :example:
    >>> parentheses_checker1('stack.py')
    'Well parenthesed'
    >>> parentheses_checker1('bad_stack1.py')
    'Bad parenthesed'
    >>> parentheses_checker1('bad_stack3.py')
    'Bad parenthesed'
    """
    with open(file) as f:
        strInput = f.read()
        if strInput:
            brackets = [ ('(',')'), ('[',']'), ('{','}')]
            kStart = 0
            kEnd = 1

            stck=stack.create()

            for char in strInput:
                for bracketPair in brackets:
                    if char == bracketPair[kStart]:
                        stck.append(char)
                    elif char == bracketPair[kEnd] and len(stck) > 0 and stck.pop() != bracketPair[kStart]:
                        return 'Bad parenthesed'

            if len(stck) == 0:
                return 'Well parenthesed'

        return 'Bad parenthesed'

Version 2.

Returns line, column and reason of unbalanced parenthesis.

              
class ParenthesisError(Exception):
    def __init__(self, parenthesis, error):
        what_is_wrong = '%s at line %s, char number %s, the problem is >>> %s'
        parenthesis.line = parenthesis.line.replace('\n', '\\n')
        self.what_is_wrong = what_is_wrong % (error ,parenthesis.i_line-1, parenthesis.i_char-1, parenthesis.line)
 
    def __str__(self):
        return self.what_is_wrong
 
 
class Parenthesis:
    def __init__(self, i_line, line, i_char):
        self.i_line = i_line
        self.line = line
        self.i_char = i_char
 
 
def parentheses_checker2(filename):
    """
    Returns the line, char number and phase containing unbalanced
    parentheses is there's parenthesis problem.
    In the opposite case, returns nothing.
    :param file: file ( .txt, .py, etc.)
    :return: None or line, char number and phase
    :rtype: str or None
    :UC: file is an external file
    :example:
    >>> parentheses_checker2('stack.py')
    >>> parentheses_checker2('bad_stack2.py')
    Wrong open parenthesis at line 80, char number 13, the problem is: >>> def is_empty (s:\n
    >>> parentheses_checker2('bad_stack3.py')
    Wrong open parenthesis at line 10, char number 2, the problem is: >>> A [ module for stack data structure.\n
    """
    par_open = {'(':[], '[':[], '{':[]}
    par_close = {')':'(', ']':'[', '}':'{'}
    
    with open(filename) as f:
        for i_line, line in enumerate(f, start=1):
            for i_char, char in enumerate(line, start=1):
                if char in par_open:
                    par_open[char].append(Parenthesis(i_line=i_line, line=line, i_char=i_char))
                elif char in par_close:
                    try:
                        par_open[par_close[char]].pop()
                    except IndexError:
                        print(ParenthesisError(Parenthesis(i_line=i_line, line=line, i_char=i_char), 'Wrong closed parenthesis '))
    
    for _, l in par_open.items():
        for parenthesis in l:
            print(ParenthesisError(parenthesis, 'Wrong open parenthesis'))
            
Those functions don't process exceptions as commented parentheses.