现在要自己来实现这些数据结构和常用算法了。

把基础再打牢一些。

栈的作用很大,无须多言。

我尽量看了题目要求,自己来实现代码的。

# coding = utf-8

class Stack:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[len(self.items) - 1]

    def size(self):
        return len(self.items)

def par_checker(chk_par):
    s = Stack()
    balanced = True
    for par in chk_par:
        if par in "[({":
            # 入栈
            s.push(par)
        else:
            if s.is_empty():
                balanced = False
                break
            else:
                # 出栈并判断
                open_par = s.pop()
                close_par = par
                if not check_match(open_par, close_par):
                    balanced = False
                    break
    if s.is_empty() and balanced:
        return True
    else:
        return False

def check_match(open_par, close_par):
    opener = "[({"
    closer = "])}"
    # 使用index返回对应位置
    return opener.index(open_par) == closer.index(close_par)

print(par_checker("([{}])"))
print(par_checker(")))(())"))
print(par_checker("(())))))))"))
print(par_checker("(((((((((())))))))"))
print(par_checker("(()({(([[]]))})()()()(()))"))
print(par_checker("(()()(()()()(()))"))

  

输出:

C:\Users\Sahara\.virtualenvs\untitled\Scripts\python.exe D:/test/python_stack.py
True
False
False
False
True
False

Process finished with exit code 0

  

使用python解决算法和数据结构--使用栈实现符号匹配的更多相关文章

  1. 使用python解决算法和数据结构--使用栈实现进制转换

    可以将10进制数据转换成2进制,8进制,16进制等. 晚上练练算法和数据结构哈. # coding = utf-8 class Stack: def __init__(self): self.item ...

  2. problem-solving-with-algorithms-and-data-structure-usingpython(使用python解决算法和数据结构) -- 算法分析

    1. 计算前n个整数的和 def sumOfN(n): theSum = 0 for i in range(1,n+1): theSum += i return theSum print(sumOfN ...

  3. problem-solving-with-algorithms-and-data-structure-usingpython(使用python解决算法和数据结构) -- 基本数据结构 -- 队列

    1. 什么是队列? 队列是项的有序结合,其中添加新项的一端称为队尾,移除项的一端称为队首. FIFO:先进先出 2. 队列抽象数据类型 队列操作如下: Queue() 创建一个空的新队列. 它不需要参 ...

  4. python之 算法和数据结构

    什么是计算机科学? --首先明确的一点就是计算机科学不仅仅是对计算机的研究,虽然计算机在科学发展的过程中发挥了重大的作用,但是它只是一个工具,一个没有灵魂的工具而已,所谓的计算机科学实际上是对问题,解 ...

  5. 基于visual Studio2013解决算法导论之019栈实现(基于数组)

     题目 用数组实现栈 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> #in ...

  6. 基于visual Studio2013解决算法导论之018栈实现(基于链表)

     题目 用链表实现栈 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> #in ...

  7. 算法与数据结构(二) 栈与队列的线性和链式表示(Swift版)

    数据结构中的栈与队列还是经常使用的,栈与队列其实就是线性表的一种应用.因为线性队列分为顺序存储和链式存储,所以栈可以分为链栈和顺序栈,队列也可分为顺序队列和链队列.本篇博客其实就是<数据结构之线 ...

  8. C语言数据结构之栈:括号匹配

    括号匹配这是个很简单的题目,如果只有小括号,就模拟进栈和出栈的过程就行了: 注:输入时'@'作为结束标志 #include <stdio.h> int main() { freopen(& ...

  9. 《用Python解决数据结构与算法问题》在线阅读

    源于经典 数据结构作为计算机从业人员的必备基础,Java, c 之类的语言有很多这方面的书籍,Python 相对较少, 其中比较著名的一本 problem-solving-with-algorithm ...

随机推荐

  1. Log4PHP日志库使用

    库下载地址: http://logging.apache.org/log4php/download.html 当前测试使用的版本为2.3.0 1.解压缩下载的压缩文件apache-log4php-2. ...

  2. 题解-bzoj3569 DZY Loves Chinese II

    Problem bzoj 题意概要:给定\(n\)点\(m\)边无向连通图,\(Q\)次询问删除\(k\)条边后是否仍然连通,强制在线 Solution 半年前考到过这类题目(询问删除任意两条边使得图 ...

  3. libunistring-0.9.9

    Introduction to libunistring Text files are nowadays usually encoded in Unicode, and may consist of ...

  4. 029_shell编写工作常用工具类总结

    一.检查命令的执行结果 function check_result() { result=$? flag=$1 if [[ "$result"x == "0"x ...

  5. localtime函数和strftime函数

    localtime函数 功能: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtime函数转换后的时间没有经过时区变换,是UTC时间 . 用法: #include & ...

  6. centos7 docker使用https_proxy 代理配置

    centos7 docker使用https_proxy 代理配置 背景: 内网的centos主机不能上网,通过同网段的windows设置代理上网,yum.conf配置http代理是可以的,但是dock ...

  7. centos6.5环境wget报错Unable to establish SSL connection

    centos6.5环境wget报错Unable to establish SSL connection [root@centossz008 src]# wget --no-check-certific ...

  8. MySQL - 查看慢SQL

    查看MySQL是否启用了查看慢SQL的日志文件 (1) 查看慢SQL日志是否启用 mysql> show variables like 'log_slow_queries'; +-------- ...

  9. Python-爬虫-猫眼T100

    目标站点需求分析 涉及的库 from multiprocessing import Poolfrom requests.exceptions import RequestExceptionimport ...

  10. 开发了5年android,我开始了go学习之旅

    前言 做了近5年的android开发,最近项目也是不怎么忙,空闲的时候总会思考一些事情,不过作为移动开发,我个人觉得很有必要学习后台开发,由于公司是Go语言开发的,了解go语言一段时间后,我发现go语 ...