PythonでBrackets
#coding utf-8 import string def checkio(expression): s = expression s = s.encode("utf-8") s = s.translate(string.maketrans("",""),"+-*/0123456789") if s == "": return True t = 0 while t < len(expression): s = s.replace("()","").replace("{}","").replace("[]","") t+=1 if s == "": return True return False if __name__ == '__main__': # checkio(u"((5+3)*2+1)")# == True, "Simple" # checkio(u"{[(3+1)+2]+}")# == True, "Different types" # checkio(u"(3+{1-1)}")# == False, ") is alone inside {}" # checkio(u"[1+1]+(2*2)-{3/3}")# == True, "Different operators" # checkio(u"(({[(((1)-2)+3)-3]/3}-3)") #== False, "One is redundant" # checkio(u"2+3") #== True, "No brackets, no problem" # checkio("({[3]})-[4/(3*{1001-1000}*3)/4]") # checkio("((5+3)*2+1)") # checkio("({[3]})-[4/(3*{1001-1000}*3)/4]") checkio("2+3")
参考にしたサイト
PythonにおけるUnicodeとUTF-8の相互変換 - CtrlShift.net
Pythonでの文字列置換をマスターする - orangain flavor
文字列の操作 - ひきメモ
一言
綺麗に書けたし、綺麗に解けたと思える納得の出来。