复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#! /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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复