练习7.1-2

#! /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()

猜你喜欢

转载自blog.csdn.net/liudaoqiang_tj/article/details/82469829
7.1