Exemple #1
0
def main(fichier):

     s=stack.create()
     ind_char=stack.create()
     ind_ligne=stack.create()
     nb_ligne=0
     in_channel = open(fichier, 'r')
    
     chaine="debut"

     while(chaine != ''):
           chaine=in_channel.readline()
           nb_ligne=nb_ligne+1

           for i in range(len(chaine)):

               if(chaine[i]=="(" or chaine[i]=="[" or chaine[i]=="{") :
                   stack.push(chaine[i],s)
                   stack.push(i,ind_char)
                   stack.push(nb_ligne,ind_ligne)

               elif(chaine[i]==")" or chaine[i]=="]" or chaine[i]=="}"):
                    if(not (stack.is_empty(s))):
                          if (stack.top(s)==getOpening(chaine[i])):
                                 stack.pop(s)
                          else:
                                 print("Closed parenthese",chaine[i]," at line ",nb_ligne," char ",i," don't match the open parenthese ",stack.top(s)," at line ",stack.top(ind_ligne)," char ",stack.top(ind_char))
                    else:
                                 print("No open parenthese matching parenthese ",chaine[i]," at line ",nb_ligne," char ",i)
           
           chaine=in_channel.readline()
     if(not stack.is_empty(s)):
          print("Parenthese ",stack.top(s)," at line ",stack.top(nb_ligne)," char ",stack.top(nb_char)," has no matching closed parenthese.")
def bracket(str):
    dict = {'(':')','{':'}','[':']'}
    ouv = stack.create()
    fer = stack.create()
    for i in range(len(str)):
        if(str[i]=='(' or str[i]=='[' or str[i]=='{'):
                ouv+=[str[i]]
        elif(str[i]==")" or str[i]=="]" or str[i]=='}'):
            fer+=[str[i]]
            if(dict[stack.pop(ouv)]!=stack.pop(fer)):
                return False
    if(len(ouv)!=0 or len(fer)!=0):
        return False
    return True
Exemple #3
0
def verif_parentheses(chaine):
    s=stack.create()

    for i in range(len(chaine)):
        """        
        print(s)
        """        
        if(chaine[i]=="(" or chaine[i]=="[" or chaine[i]=="{") :
              stack.push(chaine[i],s)
        elif(chaine[i]==")" or chaine[i]=="]" or chaine[i]=="}"):
              if(not (stack.is_empty(s)) and stack.top(s)==getOpening(chaine[i])):
                   stack.pop(s)
              else:
                  return -1
    if(stack.is_empty(s)):
        return 1
    else:
        return 0
def bracket(str):
    dict = {'(':')','{':'}','[':']'}
    ouv = stack.create()
    fer = stack.create()
    ouvLines = stack.create()
    ferLines = stack.create()
    ouvChars = stack.create()
    ferChars = stack.create()
    line = 1
    char = 0
    inComment = False
    inString = False
    for i in range(len(str)):
        if(inComment and str[i]!="\n"):
            continue
        elif(inComment and str[i]=="\n"):
            inComment = False
        if(str[i]=="\n"):
            line += 1
            char = 0
        if(str[i]=="#"):
            inComment = True
        char += 1
        if(str[i]=='(' or str[i]=='[' or str[i]=='{'):
            ouv+=[str[i]]
            ouvLines+=[line]
            ouvChars+=[char]
        elif(str[i]==")" or str[i]=="]" or str[i]=='}'):
            fer+=[str[i]]
            ferLines+=[line]
            ferChars+=[char]
            if(len(ouv)!=0):
                o = stack.pop(ouv)
                f = stack.pop(fer)
                ol = stack.pop(ouvLines)
                fl = stack.pop(ferLines)
                oc = stack.pop(ouvChars)
                fc = stack.pop(ferChars)
            else:
                f = stack.pop(fer)
                fl = stack.pop(ferLines)
                fc = stack.pop(ferChars)
                return "No open parenthese matching parenthese "+f+" at line "+builtins.str(fl)+" char "+builtins.str(fc)+"."
            if(dict[o]!=f):
                return "Closed parenthese "+f+" at line "+builtins.str(fl)+" char "+builtins.str(fc)+" don't match the open parenthese "+o+" at line "+builtins.str(ol)+" char "+builtins.str(oc)+"."
    if(len(ouv)!=0 and len(fer)==0):
        o = stack.pop(ouv)
        ol = stack.pop(ouvLines)
        oc = stack.pop(ouvChars)
        return "Parenthese "+o+" at line "+builtins.str(ol)+" char "+builtins.str(oc)+" has no matching closed parenthese."
    return "Well parenthesed."
Exemple #5
0
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'
Exemple #6
0
def bracket(str):
    dict = {"(": ")", "{": "}", "[": "]"}
    ouv = stack.create()
    fer = stack.create()
    ouvLines = stack.create()
    ferLines = stack.create()
    ouvChars = stack.create()
    ferChars = stack.create()
    line = 1
    char = 0
    for i in range(len(str)):
        if str[i] == "\n":
            line += 1
            char = 0
        char += 1
        if str[i] == "(" or str[i] == "[" or str[i] == "{":
            ouv += [str[i]]
            ouvLines += [line]
            ouvChars += [char]
        elif str[i] == ")" or str[i] == "]" or str[i] == "}":
            fer += [str[i]]
            ferLines += [line]
            ferChars += [char]
            if len(ouv) != 0:
                o = stack.pop(ouv)
                f = stack.pop(fer)
                ol = stack.pop(ouvLines)
                fl = stack.pop(ferLines)
                oc = stack.pop(ouvChars)
                fc = stack.pop(ferChars)
            else:
                f = stack.pop(fer)
                fl = stack.pop(ferLines)
                fc = stack.pop(ferChars)
                return (
                    "No open parenthese matching parenthese "
                    + f
                    + " at line "
                    + builtins.str(fl)
                    + " char "
                    + builtins.str(fc)
                    + "."
                )
            if dict[o] != f:
                return (
                    "Closed parenthese "
                    + f
                    + " at line "
                    + builtins.str(fl)
                    + " char "
                    + builtins.str(fc)
                    + " don't match the open parenthese "
                    + o
                    + " at line "
                    + builtins.str(ol)
                    + " char "
                    + builtins.str(oc)
                    + "."
                )
    if len(ouv) != 0 and len(fer) == 0:
        o = stack.pop(ouv)
        ol = stack.pop(ouvLines)
        oc = stack.pop(ouvChars)
        return (
            "Parenthese "
            + o
            + " at line "
            + builtins.str(ol)
            + " char "
            + builtins.str(oc)
            + " has no matching closed parenthese."
        )
    return "Well parenthesed."
Exemple #7
0
def verif_parentheses(chaine):
    s=stack.create()
    indice=stack.create()
Exemple #8
0
##################################################################################
#
# A queue that enqueues stacks!  #################################################
#
##################################################################################

print("\nA queue that enqueues stacks!\n")

# create new queue
queue = q.create()

# enqueue a bunch of stacks!
for i in range(0, 10):

    # make new stack
    new_stack = s.create()

    for j in range(0, 3):

        val = i * 3 + j

        print("Pushing value : " + str(val))
        s.push(new_stack, val)

    print(
        "Enqueue-ing the stack containing those past 3 values onto the queue!!\n"
    )
    q.enqueue(queue, new_stack)

print("\nDe-queue-ing the queue!\n")
Exemple #9
0
def create(x):
    s = stack.create(x)
    return [s, 1, x, False, 0]