“Vérifiez si les brèches claquent correctement” Réponses codées

Vérifiez si les brèches claquent correctement

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

Vérifiez si les brèches claquent correctement

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

Réponses similaires à “Vérifiez si les brèches claquent correctement”

Questions similaires à “Vérifiez si les brèches claquent correctement”

Plus de réponses similaires à “Vérifiez si les brèches claquent correctement” dans TypeScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code