零基础学习python第20讲的问题求帮忙
本帖最后由 xiao480054 于 2022-8-23 21:04 编辑问题:请编写一个程序,判断给定的字符串 s 中括号的写法是否合法。
限制条件:
1.字符串仅包含 '('、')'、'['、']'、'{'、'}' 这三对括号的组合。
2.左右括号必须成对编写,比如 "()" 是合法的,"(" 则是非法的。
3.左右括号必须以正确的顺序闭合,比如 "{()}" 是合法的,"{(})" 则是非法的。
我按照自己的思路编写了以下代码:
while True:
s = input("请输入测试字符串:")
n = []
for each in s:
n.append(each)
if "{" and "}" and "[" and "]" and "(" and ")" in n:
if n.count("{") == n.count("}")) and (n.count("[") == n.count("]")) and (n.count("(") == n.count(")"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif "[" and "]" and "(" and ")" in n:
if n.count("[") == n.count("]") and n.count("(") == n.count(")"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif "{" and "}" and "[" and "]" in n:
if n.count("{") == n.count("}") and n.count("[") == n.count("]"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif "{" and "}" and "(" and ")" in n:
if n.count("{") == n.count("}") and n.count("(") == n.count(")"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif "(" and ")" in n:
if n.count("(") == n.count(")"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif "[" and "]" in n:
if n.count("[") == n.count("]"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif "{" and "}" in n:
if n.count("{") == n.count("}"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
else:
print("非法的")
运行结果出现BUG:
===
请输入测试字符串:{}[](){}[](){}[]()
['(', '(', '(', ')', ')', ')', '[', '[', '[', ']', ']', ']', '{', '{', '{', '}', '}', '}']
合法的
请输入测试字符串:{}[](){}[](){}[](
非法的
请输入测试字符串:{}[](){}[](
非法的
请输入测试字符串:{}[](
['(', '[', ']', '{', '}']
合法的
===
求大家帮忙查看到底哪里出问题了。 T_T 你这个思路有点问题,具体可以看传送门
s = input("请输入测试字符串:")
t = ""
temp = {'(':')', '[':']', '{': '}',')':'(', ']':'[', '}': '{'}
for i in s:
if len(t) != 0:
if temp == t: #判断能否组成括号对,如果可以就将这一对消去
t = t[:len(t)-1]
t = t+i
#print(t)
if t == '':
print('合法的')
else:
print('非法的')
tommyyu 发表于 2022-8-23 19:51
你这个思路有点问题,具体可以看传送门
我去看了下关于栈的那个算法说明,学会了。
先前我自己用PyCharm的debug单步运行我自己的代码,找到了错误原因。后修改如下,最终实现了题目要求。当然比你的复杂多了,还需努力。感谢回复。
while True:
s = input("请输入测试字符串:")
n = []
for each in s:
n.append(each)
if ("{" in n) and ("}" in n) and ("[" in n) and ("]" in n) and ("(" in n) and (")" in n):
if n.count("{") == n.count("}") and n.count("[") == n.count("]") and n.count("(") == n.count(")"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif ("[" in n) and ("]" in n) and ("(" in n) and (")" in n) and ("{" not in n) and ("}" not in n):
if n.count("[") == n.count("]") and n.count("(") == n.count(")"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif ("{" in n) and ("}" in n) and ("[" in n) and ("]" in n) and ("(" not in n) and (")" not in n):
if n.count("{") == n.count("}") and n.count("[") == n.count("]"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif ("{" in n) and ("}" in n) and ("(" in n) and (")" in n) and ("[" not in n) and ("]" not in n):
if n.count("{") == n.count("}") and n.count("(") == n.count(")"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif ("(" in n) and (")" in n) and ("{" not in n) and ("}" not in n) and ("[" not in n) and ("]" not in n):
if n.count("(") == n.count(")"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif ("[" in n) and ("]" in n) and ("{" not in n) and ("}" not in n) and ("(" not in n) and (")" not in n):
if n.count("[") == n.count("]"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
elif ("{" in n) and ("}" in n) and ("(" not in n) and (")" not in n) and ("[" not in n) and ("]" not in n):
if n.count("{") == n.count("}"):
n.sort()
print(n)
print("合法的")
else:
print("非法的")
else:
print("非法的")
页:
[1]