“Compruebe si los breckets se aclaran correctamente” Código de respuesta

Compruebe si los breckets se aclaran correctamente

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)
SHAM3R

Compruebe si los breckets se aclaran correctamente

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)

SHAM3R

Respuestas similares a “Compruebe si los breckets se aclaran correctamente”

Preguntas similares a “Compruebe si los breckets se aclaran correctamente”

Más respuestas relacionadas con “Compruebe si los breckets se aclaran correctamente” en TypeScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código