# 1.成對的括號字元(PythonDemo1) ###### tags: `PythonDemo` **分類:資料結構** **說明** 有些圖形化判面的程式語言編輯器可以自動標注程式碼中沒有成對的括號字元,如()、[]、{}、<>等 方便程式設計師快速找到語法錯誤的地方,十分便利。 在這個題目中,請寫一個程式來檢查輸入所給的一些字串其中的括號字元是否都成對。 --- ```python= n = eval(input()) sign = {'}':'{',']':'[',')':'(','>':'<'} for i in range(n): try: context = input() l = [] r = [] for j in context: if j == '{'or j=='[' or j=='<' or j=='(': l.append(j) elif j == '}'or j==']'or j=='>' or j==')': if sign[j] == l[-1]: l.reverse() l.remove(sign[j]) l.reverse() else: r.append() ''' elif j == '}': if '{'in l : l.remove('{') else: r.append(j) elif j == ')': if '('in l : l.remove('(') else: r.append(j) elif j == ']': if '['in l : l.remove('[') else: r.append(j) elif j == '>': if '<'in l : l.remove('<') else: r.append(j) ''' if len(l)==0 and len(r)==0: print('Y') else: print('N') except: print('N') continue ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up