概述
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
@author: liudaoqiang
@file: studycase
@time: 2018/9/2 10:46
'''
from arraystack import LinkedStack
def branketsBalance(exp):
"""exp is a string that represents the expression"""
stk = LinkedStack()
for ch in exp:
if ch in ['[','(']:
stk.push(ch)
elif ch in [']',')']:
if stk.isEmpty():
return False
chFromStack = stk.pop()
if ch == ']' and chFromStack != ']' or
ch == ')' and chFromStack != '(':
return False
return stk.isEmpty()
def branketsBalanceExtd(exp, startlyst, endlyst):
"""exp is a string that represents the expression"""
stk = LinkedStack()
for index in range(len(startlyst) - 1):
if startlyst[index] not in exp:
return False
stk.push(startlyst[index])
if startlyst[index] == endlyst[index]:
stk.pop()
else:
return False
return stk.isEmpty()
def main():
exp = input("input bracketed expression")
if branketsBalance(exp):
print("OK")
else:
print("not OK")
if __name__ == "__main__":
main()
最后
以上就是知性帆布鞋为你收集整理的练习7.1-2的全部内容,希望文章能够帮你解决练习7.1-2所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复