“Gültige Klammern” Code-Antworten

Gültige Klammern

class Solution:
    def isValid(self, s):
        if s == "":
            return True
        if len(s) < 2:
            return False

        pair_brkts = {
            "{" : "}",
            "(" : ")",
            "[" : "]"
            }
        stack = []

        for i in s:
            if i in pair_brkts:
                stack.append(i)                                #stack i(forward facing brackets) that also exists as keys in our pair_brkts dictionary)
                #print("forward facing brackets", stack)       #to see the contents of your stacked list 
            else:
                if len(stack) == 0 or pair_brkts[stack.pop()] != i:   #if stack list is empty or the value pair of last 
                                                                        #list item isnt same, return False, otherwise break out of loop
                    #print("backward facing brackets", stack)        #print to see whats left of your list after 
                                                                    #looping is over for all your i(i.e brackets)
                    return False
        if len(stack) > 0:                                          #if stack list is not empty at this point, return False, else return True
            return False
        return True

 
        

Task = Solution()
print("1. ", Task.isValid("({[()]})"))
print("2. ", Task.isValid("()[]{}"))
print("3. ", Task.isValid("(]"))
Kingsley Atuba

Gültige Klammern

{ { } [ ] [ [ [ ] ] ] } is VALID expression
          [ [ [ ] ] ]    is VALID sub-expression
  { } [ ]                is VALID sub-expression
Hugo Barcelos

Python Stack -Funktion zählen die gültige Anzahl der Klammern gibt die Gesamtzahl der gültigen Klammern in der Zeichenfolge zurück

import sys

def error(c, line_number, column_number):
    print 'Error: unmatched', c, 'line', line_number, 'column', column_number

def check(stack, wanted, c, line_number, column_number):
    if stack[-1] != wanted:
        error(c, line_number, column_number)
    else:
        stack.pop()

def check_parentheses(f):
    stack = list()
    line_number = 0
    for line in f:
        line_number = line_number + 1
        column_number = 0
        for c in line:
            column_number = column_number + 1
            if c == '(' or c == '[' or c == '{':
                stack.append(c)
            elif c == ')':
                check(stack, '(', ')', line_number, column_number)
            elif c == ']':
                check(stack, '[', ']', line_number, column_number)
            elif c == '}':
                check(stack, '{', '}', line_number, column_number)

def main():
    filename = sys.argv[1]
    try:
        f = file(filename)
    except IOError:
        sys.stderr.write('Error: Cannot open file %s' % filename)
        sys.exit(1)
    check_parentheses(f)
    f.close()

main()
Sleepy Sardine

Ähnliche Antworten wie “Gültige Klammern”

Fragen ähnlich wie “Gültige Klammern”

Weitere verwandte Antworten zu “Gültige Klammern” auf Java

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen