“tanda kurung yang valid” Kode Jawaban

Tanda kurung yang valid

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

tanda kurung yang valid

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

Python Stack Function Count Jumlah braket yang valid mengembalikan jumlah total braket yang valid dalam string

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

Jawaban yang mirip dengan “tanda kurung yang valid”

Pertanyaan yang mirip dengan “tanda kurung yang valid”

Lebih banyak jawaban terkait untuk “tanda kurung yang valid” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya