Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

check if breckets clossing properly

def valid_paren(input_str):
  # Declaraing a stack.
  stack = []
  # Iterating over the entire string
  for paren in input_str:
    # If the input string contains an opening parenthesis,
    # push in on to the stack.
    if paren == '(' or paren == '[' or paren == '{':
      stack.append(paren)
    else:
      # In the case of valid parentheses, the stack cannot be 
      # be empty if a closing parenthesis is encountered.
      if not stack:
        print(input_str, "contains invalid parentheses.")
        return
      else:
        # If the input string contains a closing bracket,
        # then pop the corresponding opening parenthesis if
        # present.
        top = stack[-1]
        if paren == ')' and top == '(' or 
        paren == ']' and top == '[' or 
        paren == '}' and top == '{':
          stack.pop()
  # Checking the status of the stack to determine the
  # validity of the string.
  if not stack:
    print(input_str, "contains valid parentheses.")
  else:
    print(input_str, "contains invalid parentheses.")

input1 = "{{}}()[()]"
input2 = "{][}"
input3 = ")"
valid_paren(input1)
valid_paren(input2)
valid_paren(input3)
input4 = "([)]"
valid_paren(input4)
input5 = '() [{}]'
valid_paren(input5)
Comment

check if breckets clossing properly

def valid_paren(input_str):
  # Declaraing a stack.
  stack = []
  # Iterating over the entire string
  for paren in input_str:
    # If the input string contains an opening parenthesis,
    # push in on to the stack.
    if paren == '(' or paren == '[' or paren == '{':
      stack.append(paren)
    else:
      # In the case of valid parentheses, the stack cannot be 
      # be empty if a closing parenthesis is encountered.
      if not stack:
        print(input_str, "contains invalid parentheses.")
        return
      else:
        # If the input string contains a closing bracket,
        # then pop the corresponding opening parenthesis if
        # present.
        top = stack[-1]
        if paren == ')' and top == '(' or 
        paren == ']' and top == '[' or 
        paren == '}' and top == '{':
          stack.pop()
  # Checking the status of the stack to determine the
  # validity of the string.
  if not stack:
    print(input_str, "contains valid parentheses.")
  else:
    print(input_str, "contains invalid parentheses.")

input1 = "{{}}()[()]"
input2 = "{][}"
input3 = ")"
valid_paren(input1)
valid_paren(input2)
valid_paren(input3)

Comment

PREVIOUS NEXT
Code Example
Typescript :: bts assurance 
Typescript :: four basic components that information system consists of 
Typescript :: how to destroy the widgets with th name same created in for loop python tkinter 
Typescript :: HTML form inputs to excel spreadsheet [duplicate] 
Typescript :: R barplots ggplot 
Typescript :: What are the components of the environment? Explain it along with the examples class 6 
Typescript :: usually placed in footer 
Typescript :: type for object props 
Typescript :: how can i get 2 inputs in singal line seprated by space 
Typescript :: ngbcollapse error with Reactive Forms 
Typescript :: python fancy way to get arguments from the command line 
Typescript :: bibtex remove brackets in note field 
Typescript :: which electromagnetic radiation is used for heating and night vision equipment 
Typescript :: sap abap check file exists on application server tcode 
Typescript :: get_refreshed_fragments too long to load 
Typescript :: Count pets the types of pets in a columns 
Typescript :: vue router popstate 
Typescript :: routerextensions nativescript 7 import 
Typescript :: typeorm transactions example 
Typescript :: typescript hello world 
Typescript :: yup typescript 
Typescript :: rest api django return value if exists in another table 
Typescript :: google fonts roboto 
Cpp :: move mouse c++ 
Cpp :: conditional cout in c++ 
Cpp :: vhdl integer to std_logic_vector 
Cpp :: leap year c++ 
Cpp :: how to shut down windows in c++ 
Cpp :: c++ edit another processes memory address 
Cpp :: how to create a pair of double quotes in c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =