20
1
You will be given a (possibly empty) string containing brackets ([{()}]
) and any other characters (A
-Z
, a
-z
, 0
-9
, punctuation). You need to check if it adheres to the following rules:
- Non-bracket characters are ignored.
- Every open bracket
[{(
has a closing bracket)}]
. So[](
is not allowed. - Brackets are nested properly.
[(])
is not allowed. - Curly brackets cannot contain square brackets within them. Simple brackets cannot contain either curly or square brackets within them. So
[({})]
,[{[]}]
and({})
are not allowed. Brackets can be nested with similar brackets, so[[{((()))}{{(())}}]()]{()}
is allowed.
Output is a single truthy/falsey value as per your choice.
Shortest code wins.
Test cases
b[[a{(/)}(())+={{}-}],]
-> Valid
([h][e][l][l][o])
-> Invalid
[///[{(\/(arg()))}1{{((-)-2)}}]()]{()}
-> Valid
hi
-> Valid
2
Possible duplicate of Fix unbalanced brackets
– FUZxxl – 2015-12-02T11:09:25.1039@FUZxxl That looks like a much harder challenge. I feel like there is another dupe somewhere though. – Martin Ender – 2015-12-02T11:17:45.740
@MartinBüttner Yes, it can. I've added some testcases. And did you find the duplicate you were looking for? – ghosts_in_the_code – 2015-12-02T16:56:59.543
@ghosts_in_the_code No, but I haven't kept searching. Although I think if there is one that checks multiple bracket types it doesn't give them different priorities, so I think this should be fine. – Martin Ender – 2015-12-02T16:58:10.760
1
@MartinBüttner: This challenge might be what you were thinking of.
– Ilmari Karonen – 2015-12-02T22:52:24.430@IlmariKaronen Thanks, but I don't think I've ever seen that one. – Martin Ender – 2015-12-02T22:57:28.033
1I think we should close the other question as a duplicate of this; this is better because it has fewer bonuses. – lirtosiast – 2015-12-03T03:55:57.590
@ThomasKwa Please see this link. http://meta.codegolf.stackexchange.com/questions/7653/duplicates-of-each-other-wrt-bracket-balancing
– ghosts_in_the_code – 2015-12-03T09:54:11.703