7-3 valid bracket judgment (30 points)

Keywords: Python pta

This question comes from Li Kou. I also saw this question in this week's Blue Bridge Cup training. I think the complexity of this question is higher than that of the Blue Bridge Cup training, so I took out the Blue Bridge Cup training. Let's analyze the Blue Bridge Cup training first, and then draw out the differences.

Premise:

The combination of parentheses () is strange, Drizzle   Want to know whether various combinations of parentheses can be legal
Legal requirement: each left bracket of the same type must have the corresponding right bracket of the same type, which is closed in the correct order

requirement:

Input: enter a string in parentheses. Output: whether the output is legal. If yes, it is True; otherwise, it is False

Example:

Input:

(){}[]

No blank lines at the end

Output:

True

No blank lines at the end

Range:

For 100% data: bracket string length ≤ 100

Idea:

First, we need to understand what the test point is, which is very simple, that is, the operation of the stack

Therefore, we only need a list, and then read the parentheses sequentially, and put the left parentheses in turn. If it is a right parenthesis, judge whether it is a parenthesis. If so, remove the left parenthesis. that will do

The code is as follows:

def op(str1: str):
    str2 = []
    for i in str1:
        if i == '(' or i == '[' or i == '{':
            str2.append(i)
        elif i == ')':
            if str2[len(str2) - 1] != '(':
                return 0
            else:
                str2.pop()
        elif i == ']':
            if str2[len(str2) - 1] != '[':
                return 0
            else:
                str2.pop()
        elif i == '}':
            if str2[len(str2) - 1] != '{':
                return 0
            else:
                str2.pop()
    return 1

So the difficulty is not high

Then complete the basic code. Here is the complete answer

answer:

# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/10/16 18:58
def op(str1: str):
    str2 = []
    for i in str1:
        if i == '(' or i == '[' or i == '{':
            str2.append(i)
        elif i == ')':
            if str2[len(str2) - 1] != '(':
                return 0
            else:
                str2.pop()
        elif i == ']':
            if str2[len(str2) - 1] != '[':
                return 0
            else:
                str2.pop()
        elif i == '}':
            if str2[len(str2) - 1] != '{':
                return 0
            else:
                str2.pop()
    return 1


str1 = input()
if op(str1):
    print('True')
else:
    print("False")

complicate:

Alan's eyes are hard to use recently and he often can't distinguish the parentheses clearly, so he wants you to help him. Given a string that only includes' (',' ',' {','} ',' [','] ', he wants you to help write a program to determine whether the parentheses are normally closed.

  1. The left parenthesis must be closed with the same type of right parenthesis.
  2. The left parentheses must be closed in the correct order.
  3. An empty string is assumed to be True

Input format:

Enter an empty string or a line containing only '(', '' ',' {','} ',' [','] '.

Output format:

Output "True" if all parentheses are closed correctly, otherwise output "False".

Input example:

A set of inputs is given here. For example:

()

No blank lines at the end

Output example:

The corresponding output is given here. For example:

True

No blank lines at the end

Input example:

A set of inputs is given here. For example:

{[()]()}

No blank lines at the end

Output example:

The corresponding output is given here. For example:

True

No blank lines at the end

Input example:

A set of inputs is given here. For example:

{{()[]}

No blank lines at the end

Output example:

The corresponding output is given here. For example:

False

No blank lines at the end

Idea:

What's the difference between this one and the one above?

There is no doubt that "empty string is recognized as True"

So we just need to use our very skilled judgment (try:... Except:...):

The code is as follows:

try:
    str1 = input()
    if op(str1):
        print('True')
    else:
        print("False")
except :
    print('True')

In this way, we need to use the idea of the preceding question to complete the function.

Here is the complete answer

answer:

# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/10/18 20:17
# Description: sometimes in life, there must be. Don't force it when there is no time in life
# Autor: Neptune
# Date: 2021/10/16 18:58
def op(str1: str):
    str2 = []
    if str1.count('(')!=str1.count(')') or str1.count('[')!=str1.count(']') or str1.count('{')!=str1.count('}') :
        return 0
    for i in str1:
        if i == '(' or i == '[' or i == '{':
            str2.append(i)
        elif i == ')':
            if str2[len(str2) - 1] != '(':
                return 0
            else:
                str2.pop()
        elif i == ']':
            if str2[len(str2) - 1] != '[':
                return 0
            else:
                str2.pop()
        elif i == '}':
            if str2[len(str2) - 1] != '{':
                return 0
            else:
                str2.pop()
    return 1

try:
    str1 = input()
    if op(str1):
        print('True')
    else:
        print("False")
except :
    print('True')

Posted by may on Mon, 18 Oct 2021 13:56:55 -0700