KATA:
Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if the string is valid, and false if it’s invalid.
This Kata is similar to the Valid Parentheses Kata, but introduces new characters: brackets [], and curly braces {}. Thanks to @arnedag for the idea!
All input strings will be nonempty, and will only consist of parentheses, brackets and curly braces: ()[]{}.
What is considered Valid?
A string of braces is considered valid if all braces are matched with the correct brace.
Examples
“(){}[]” => True
“([{}])” => True
“(}” => False
“[(])” => False
“[({})](]” => False
PAST SOLUTION:
def validBraces(string):
while '()' in string or '[]' in string or '{}' in string:
string = string.replace('()','')
string = string.replace('[]','')
string = string.replace('{}','')
return string == ''
括号匹配验证
本文介绍了一个函数,用于检查字符串中的括号(包括圆括号()、方括号[]和花括号{}
357

被折叠的 条评论
为什么被折叠?



