Python implements a method for solving parentheses matching problems

Source: Internet
Author: User
This article mainly introduces Python to solve the problem of matching parentheses, involving Python stack-based string traversal, judgment, operation to solve the parentheses matching related operation skills, the need for friends can refer to the next

The example in this paper describes how Python implements the problem of solving parentheses. Share to everyone for your reference, as follows:

This in the undergraduate study data structure time has been exposed to a lot of, the mainstream idea is the use of the stack to press, pop up to match, as for Python can be used to complete the operation of the list, because the list of methods append equivalent to the stack of methods push , the list of pop methods equivalent to the stack popmethod.

The main idea:

The first set of two lists is the opening and closing parentheses of the various parentheses, and then traversing the given string, in the following cases:

1. String first character appears in closed parenthesis list, ends directly, output error

2. String length is not even, end directly, output error

3. To the original string list de-weight, if the list length of the go-back is not even direct end, output error

4. Iterate through the string, adding parentheses to the list of open brackets, and when a closing parenthesis is encountered, the index of the closing parenthesis in the closed parenthesis list is the same as the index of the last open parenthesis in the open bracket list in the current list, or the output error

Mainly in the length of a large time can be judged as soon as possible some of the more obvious error patterns, save time, according to this simple idea, the following is the specific implementation:

#!usr/bin/env python#encoding:utf-8 "" __author__: Yishui Cold City function: parentheses matching related issues ' Def bracket_mathch (one_str): ' ' ' ' ' Tmp_list=[] open_bracket_list=[' (', ' [', ' {', ' < ', ' '] close_bracket_list=[') ', ' ' ' ' ', '} ', ' > ', ' ' "one_str_list= List (ONE_STR) Length=len (one_str_list) set_list=list (set (one_str_list)) Num_list=[one_str_list.count (one) for one in Set_list] If one_str[0] in Close_bracket_list:return false elif Length%2!=0:return false elif len (set_list)%2!=0:ret Urn False else:for i in range (length): If One_str[i] in Open_bracket_list:tmp_list.append (One_str[i]) elif one_s Tr[i] in Close_bracket_list:if close_bracket_list.index (One_str[i]) ==open_bracket_list.index (tmp_list[-1]): Tmp_li St.pop () Else:return False break return trueif __name__ = = ' __main__ ': one_str_list=[' ({}) ', ' ({[< ";]}) ' , ' [(]} ' {{{{{} ', ' {{{}] ', '}{[()] ' One_str in One_str_list:if bracket_mathch (ONE_STR): Print one_str, ' correct ' els E:print one_str, ' error ' tmp= ' {}[{() () []<{{[[[[(()] () () () () {}[]{}[] () <>]]}} }] ' Print BRACKET_MATHCH (TMP)

The results are as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.