3
1
Introduction
Given a String containing an arithmetic expression, your task is to output a truthy or falsey value based on whether it contains unmatched parentheses.
Input
Your program should take in a String containing an arithmetic expression. It may take input in any way except assuming it to be present in a pre-defined variable. Reading from file, input box, modal dialog box etc. is fine. Taking input as function argument is allowed as well!
Output
Your program should output a truthy or falsey value based on whether the input String contains any unmatched parentheses. It may output in any way except except writing to a variable. Writing to file, screen, console, terminal etc. is allowed. Outputting with function return
is allowed as well!
Rules
For the purpose of this challenge, a parentheses is defined as any one of
[
,{
,(
,)
,}
,]
. A parentheses is said to be unmatched if it does not have any corresponding opening/closing parentheses. For example,[1+3
does contain unmatched parentheses.Ordering of parentheses does not matter in this challenge. So,
]1+3[
does not contain any unmatched parentheses.{[)(}]
doesn't, as well.The input String will only contain following characters :
{
,[
,(
,)
,]
,}
,1
,2
,3
,4
,5
,6
,7
,8
,9
,0
,+
,-
,*
,/
and^
.Your program should output a truthy value if the input String contains an unmatched parentheses.
Your program should output a falsey value if the input String does not contain an unmatched parentheses.
You must specify the truthy and falsey values in your post.
The input String will never be empty.
If the input String does not contain any parentheses, your program should output falsey value.
Standard loopholes apply.
Test Cases
Input -> Output
"{" Truthy
"{{{{}}" Truthy
"{[]}" Falsey
"[]][" Falsey
")()()()(" Falsey
"1+4" Falsey
"{)" Truthy
"([)]" Falsey
"(()" Truthy
"2*{10+4^3+(10+3)" Truthy
"-6+[18-{4*(9-6)}/3]" Falsey
"-6+[18-{4*)9-6](/3}" Falsey
Winning Criterion
This is code-golf, so the shortest code in bytes wins!
Note
I'll be adding a similar challenge but in which order will matter, after some time! Stay Tuned!
I'm 99% sure this is a dupe but I can't find the older one. – Stephen – 2017-06-03T16:14:45.730
@StephenS This one is the one that came to my mind, but I don't think it's quite a dupe...
– ETHproductions – 2017-06-03T16:21:51.847Related – Adnan – 2017-06-03T16:40:29.630
Can I use inconsistent values, given that they are always appropriately truthy or falsey? – Erik the Outgolfer – 2017-06-03T17:17:08.560
@EriktheOutgolfer Yes, you can! – Arjun – 2017-06-03T17:21:12.163
@EriktheOutgolfer I've received a different answer to that question in the past. Maybe we should clarify on this meta post for a consensus?
– musicman523 – 2017-06-03T18:05:04.587@StephenS I don't think that this one is a dupe but the order will matter one is almost definitely a dupe of the one ETHproductions linked to – caird coinheringaahing – 2017-06-03T18:47:17.030
3
Possible duplicate of Are the brackets fully matched?
– Stephen – 2017-06-03T19:42:14.8105@StephenS I think the fact that this is entirely orderless makes it quite different. Neither
][
or([)]
would be valid in the other challenge but are valid here. – Martin Ender – 2017-06-03T21:01:06.7932This question's title should really make it clearer that order is irrelevant. That's not what most people think about when talking about unmatched parentheses. – None – 2017-06-05T16:30:14.100
Is
(()
truthy or falsey? The rules and test cases don't make it clear whether the count of parentheses is important. All answers seem to assume that the count matters though. – Zgarb – 2017-06-07T07:06:48.800@Arjun The rules say that a paren is unmatched if "it doesn't have a corresponding opening/closing parentheses" in the string. In
(()
, each paren arguably has a corresponding pair; the)
is just used as a pair of two different(
s. This interpretation of the task is not refuted by any test case, since the only case that has multiple parens of the same type happens to have a matching count. – Zgarb – 2017-06-07T07:29:06.083@Zgarb Sorry, I misinterpreted your comment.
(()
should return truthy as it contains an unmatched paren. Thanks for pointing that out. I will add that to the test cases. :) – Arjun – 2017-06-07T07:31:39.363