What is the regular expression for the language 0m1n where m+n is even?
Asked
Active
Viewed 302 times
6
-
Either I'm tired or your question makes very little sense. – Andy E Mar 09 '10 at 15:22
-
I don't think this has anything to do with regexp... – Andy E Mar 09 '10 at 15:25
-
@Andy E: It's not because you're tired mate. – Binary Worrier Mar 09 '10 at 15:27
-
3Yes, it does. I'd explain it, but all I'd be doing is copying the question here -- I don't think it can be much simpler than that. – Michael Myers Mar 09 '10 at 15:27
-
How do we know this is programming related? – Goran Mar 09 '10 at 15:30
-
1Why people above don't try to help but joke with the question? it can be wrong or very simple. that doesn't mean, it doesn't have an answer. – erasmus Mar 09 '10 at 15:31
-
@Binary Worrier: Thanks, I thought my brain was going to explode trying to make sense of this question. – Andy E Mar 09 '10 at 15:32
-
4Why was this question closed? Its a question about computation which is the basis of programming. Its a valid question although definitely homeworkish. Its a pretty interesting boundary case of a regular language that doesn't look regular. – Il-Bhima Mar 09 '10 at 15:34
-
1@Binary Worrier, @Andy E please explain what is wrong for you with this question? People want to learn here. why their questions are closed and mocked? – erasmus Mar 09 '10 at 15:40
-
The sad part is, if the question claimed to be about a C# 1.0 program, but posed exactly the same problem, nobody would have dared challenge it. Has SO finally crossed the line, from being hesitant to help with homework, to full-on anti-intellectualism? – Ken Mar 09 '10 at 15:53
-
@erasmus How about rephrasing it to "Regex for a string of consecutive zeros followed by consecutive ones, whose total length is even?" Or at least adding one or two examples? – Amarghosh Mar 09 '10 at 16:10
-
@erasmus, @Ken: I voted to close this because the question looks like nonsense. Granted, it appears to be a valid question, but that doesn't change the fact that it looks like nonsense to me.. I've voted to close on hundreds of questions over the last year and a half, of those a decent percentage are ones that look like garbage. This is only the second time I've been wrong, and for being wrong I apologise. However I do not apologies for closing questionable questions. There are trolls on SO that ask unanswerable questions ONLY so they can see folks tie them selves . . . – Binary Worrier Mar 09 '10 at 17:18
-
. . . in knots trying to provide an answer, those questions and other unanswerable questions should be closed. In my opinion – which is provided as is, and which you are of course allowed to disagree with – this question is poorly constructed and is too brief, the asker could have provided some context/background. – Binary Worrier Mar 09 '10 at 17:19
-
@Binary Worrier, this question is not brief and provides everything for a possible answer. No background is needed. if you have a regex question like this, you can just ask it. secondly, besides closing the question with wrong decision you mocked with it. you said "@Andy E: It's not because you're tired mate. – Binary Worrier" What is this? I think you should be more kind. The aim here should be to help people not mock them. – erasmus Mar 09 '10 at 21:55
-
@earsmus: I again apologise for closing the question. I will also apologise for hurting your feelings. Andy commented "Either I'm tired or your question makes very little sense", I simply agreed with Andy (I'm sorry but without reading the answers I would still not "get" your question). I've often received much harsher and more pointed sarcasm on answers I've posted. I fear I'll offend you again with this advice, but I suggest for your own sake you learn to be a bit more thick skinned when reading comments on your posts. I'm sorry for making you feel bad, it is not and was never my intention. – Binary Worrier Mar 10 '10 at 08:25
2 Answers
14
If you mean a string 000...111...
where the length of the string is even, you can use ^(00)*(01)?(11)*$

SLaks
- 868,454
- 176
- 1,908
- 1,964
-
this is not an answer because it also validates 00 01 1 11 whose lenght is not even. – erasmus Mar 09 '10 at 15:53
-
-
2+1 for the answer. Now, how do I up-vote you for understanding the question in the first place? – Amarghosh Mar 09 '10 at 16:06
-
-
Yes, +1 for understanding the question in the first place, then answering so casually! Wow, that's style. :) @Amarghosh, if you remember your comment from four years ago, I took care of that for you. :) – zx81 Jul 23 '14 at 05:15
1
Ok, so you need to consider for zero the cases when there are odd and when they are even. This requires two states, one for even zeros, one for odd zeros. Then for the odd zero case you need to have 1 one then an even number of ones. For the even case you just need an even number of ones.
Its easy to write the DFA, but I don't know how to plot it here, so I'm going to hazard a guess at the regular expression:
(0 (00)* 1 (11)*) \/ (00)*(11)*

Il-Bhima
- 10,744
- 1
- 47
- 51
-
Here're plotted machines for that regex. Full NFA: http://static.max99x.com/misc/nfa.png. Cleaned NFA: http://static.max99x.com/misc/nfa2.png. Minimized DFA: http://static.max99x.com/misc/dfa.png. – Max Shawabkeh Mar 09 '10 at 19:13
-
@Max: Awesome! Is that a tool of your own design? I remember implementing a NFA to minimal DFA converter many years ago, but it never occurred to me to render it with graphviz :) – Il-Bhima Mar 09 '10 at 21:59
-
@Il-Bhima: Yeah. http://max99x.com/school/automata-editor. Might be somewhat buggy, though, since it was a quick school project. – Max Shawabkeh Mar 09 '10 at 22:16