REGEX L(r) = {a^n b^m : n + m is even}, r =?
So I did a problem earlier that said:
L(r) = {w in {a,b}* : w contains at least 2 a's}
For that one I said {a^2n , b} because that guarantees a string like aab or aabaab etc. Not sure how to approach the one I posted about in the title. Possibly a solution might be a^2n, b^2m so its always even, but also 2 odd numbers like a^n b^3m is also always even. Am i allowed to set boundaries like n>=m?
Thank you!
regex math automata formal-languages computability
|
show 3 more comments
So I did a problem earlier that said:
L(r) = {w in {a,b}* : w contains at least 2 a's}
For that one I said {a^2n , b} because that guarantees a string like aab or aabaab etc. Not sure how to approach the one I posted about in the title. Possibly a solution might be a^2n, b^2m so its always even, but also 2 odd numbers like a^n b^3m is also always even. Am i allowed to set boundaries like n>=m?
Thank you!
regex math automata formal-languages computability
1
Consider the automaton which acceptsa^nwhen n is even. The "failure" case would be thatnis odd. In that "failure" case, you want a following automatonb^mwhich accepts whenmis odd. Otherwise, you want a following automatonb^mwhich accepts whenmis even.
– Corion
Nov 13 '18 at 10:02
1
what about^(?=(?:..)*$)[ab]*$(regex101.com/r/1aldWm/1)
– Nahuel Fouilleul
Nov 13 '18 at 10:31
@Corion how would I write that in terms of r? I understand the logic just not the syntax.
– solnay
Nov 13 '18 at 10:39
2
I think this question better suits Mathematics StackExchange site and it'sregular-expressionstag. Also, are you sure there is a possibility to use a regular expression for this? Seems more like a grammar is to be used here, since irregularities you mention oppose the "regular" part in "regex" :)
– Asunez
Nov 13 '18 at 10:45
1
@Asunez As I showed, the language is regular, as it can be accepted by a DFA.
– Corion
Nov 13 '18 at 10:48
|
show 3 more comments
So I did a problem earlier that said:
L(r) = {w in {a,b}* : w contains at least 2 a's}
For that one I said {a^2n , b} because that guarantees a string like aab or aabaab etc. Not sure how to approach the one I posted about in the title. Possibly a solution might be a^2n, b^2m so its always even, but also 2 odd numbers like a^n b^3m is also always even. Am i allowed to set boundaries like n>=m?
Thank you!
regex math automata formal-languages computability
So I did a problem earlier that said:
L(r) = {w in {a,b}* : w contains at least 2 a's}
For that one I said {a^2n , b} because that guarantees a string like aab or aabaab etc. Not sure how to approach the one I posted about in the title. Possibly a solution might be a^2n, b^2m so its always even, but also 2 odd numbers like a^n b^3m is also always even. Am i allowed to set boundaries like n>=m?
Thank you!
regex math automata formal-languages computability
regex math automata formal-languages computability
edited Nov 13 '18 at 10:27
mrzasa
8,973103878
8,973103878
asked Nov 13 '18 at 9:37
solnaysolnay
134
134
1
Consider the automaton which acceptsa^nwhen n is even. The "failure" case would be thatnis odd. In that "failure" case, you want a following automatonb^mwhich accepts whenmis odd. Otherwise, you want a following automatonb^mwhich accepts whenmis even.
– Corion
Nov 13 '18 at 10:02
1
what about^(?=(?:..)*$)[ab]*$(regex101.com/r/1aldWm/1)
– Nahuel Fouilleul
Nov 13 '18 at 10:31
@Corion how would I write that in terms of r? I understand the logic just not the syntax.
– solnay
Nov 13 '18 at 10:39
2
I think this question better suits Mathematics StackExchange site and it'sregular-expressionstag. Also, are you sure there is a possibility to use a regular expression for this? Seems more like a grammar is to be used here, since irregularities you mention oppose the "regular" part in "regex" :)
– Asunez
Nov 13 '18 at 10:45
1
@Asunez As I showed, the language is regular, as it can be accepted by a DFA.
– Corion
Nov 13 '18 at 10:48
|
show 3 more comments
1
Consider the automaton which acceptsa^nwhen n is even. The "failure" case would be thatnis odd. In that "failure" case, you want a following automatonb^mwhich accepts whenmis odd. Otherwise, you want a following automatonb^mwhich accepts whenmis even.
– Corion
Nov 13 '18 at 10:02
1
what about^(?=(?:..)*$)[ab]*$(regex101.com/r/1aldWm/1)
– Nahuel Fouilleul
Nov 13 '18 at 10:31
@Corion how would I write that in terms of r? I understand the logic just not the syntax.
– solnay
Nov 13 '18 at 10:39
2
I think this question better suits Mathematics StackExchange site and it'sregular-expressionstag. Also, are you sure there is a possibility to use a regular expression for this? Seems more like a grammar is to be used here, since irregularities you mention oppose the "regular" part in "regex" :)
– Asunez
Nov 13 '18 at 10:45
1
@Asunez As I showed, the language is regular, as it can be accepted by a DFA.
– Corion
Nov 13 '18 at 10:48
1
1
Consider the automaton which accepts
a^n when n is even. The "failure" case would be that n is odd. In that "failure" case, you want a following automaton b^m which accepts when m is odd. Otherwise, you want a following automaton b^m which accepts when m is even.– Corion
Nov 13 '18 at 10:02
Consider the automaton which accepts
a^n when n is even. The "failure" case would be that n is odd. In that "failure" case, you want a following automaton b^m which accepts when m is odd. Otherwise, you want a following automaton b^m which accepts when m is even.– Corion
Nov 13 '18 at 10:02
1
1
what about
^(?=(?:..)*$)[ab]*$ (regex101.com/r/1aldWm/1)– Nahuel Fouilleul
Nov 13 '18 at 10:31
what about
^(?=(?:..)*$)[ab]*$ (regex101.com/r/1aldWm/1)– Nahuel Fouilleul
Nov 13 '18 at 10:31
@Corion how would I write that in terms of r? I understand the logic just not the syntax.
– solnay
Nov 13 '18 at 10:39
@Corion how would I write that in terms of r? I understand the logic just not the syntax.
– solnay
Nov 13 '18 at 10:39
2
2
I think this question better suits Mathematics StackExchange site and it's
regular-expressions tag. Also, are you sure there is a possibility to use a regular expression for this? Seems more like a grammar is to be used here, since irregularities you mention oppose the "regular" part in "regex" :)– Asunez
Nov 13 '18 at 10:45
I think this question better suits Mathematics StackExchange site and it's
regular-expressions tag. Also, are you sure there is a possibility to use a regular expression for this? Seems more like a grammar is to be used here, since irregularities you mention oppose the "regular" part in "regex" :)– Asunez
Nov 13 '18 at 10:45
1
1
@Asunez As I showed, the language is regular, as it can be accepted by a DFA.
– Corion
Nov 13 '18 at 10:48
@Asunez As I showed, the language is regular, as it can be accepted by a DFA.
– Corion
Nov 13 '18 at 10:48
|
show 3 more comments
1 Answer
1
active
oldest
votes
You correctly observe that n and m must either be both even or both odd. It only needs to be added that an odd number is one more than an even number.
A simple regular expression for "an even number of as" ( {a2n : n ≥ 0}) is (aa)*, while "an odd number of as" is (aa)*a.
Building on that, we can two cases for the original question: (aa)*(bb)* and (aa)*a(bb)*b, which can be combined into (aa)*(ab+ε)(bb)*. (Assuming you are using + for alternation and ε for the empty string.)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277926%2fregex-lr-an-bm-n-m-is-even-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You correctly observe that n and m must either be both even or both odd. It only needs to be added that an odd number is one more than an even number.
A simple regular expression for "an even number of as" ( {a2n : n ≥ 0}) is (aa)*, while "an odd number of as" is (aa)*a.
Building on that, we can two cases for the original question: (aa)*(bb)* and (aa)*a(bb)*b, which can be combined into (aa)*(ab+ε)(bb)*. (Assuming you are using + for alternation and ε for the empty string.)
add a comment |
You correctly observe that n and m must either be both even or both odd. It only needs to be added that an odd number is one more than an even number.
A simple regular expression for "an even number of as" ( {a2n : n ≥ 0}) is (aa)*, while "an odd number of as" is (aa)*a.
Building on that, we can two cases for the original question: (aa)*(bb)* and (aa)*a(bb)*b, which can be combined into (aa)*(ab+ε)(bb)*. (Assuming you are using + for alternation and ε for the empty string.)
add a comment |
You correctly observe that n and m must either be both even or both odd. It only needs to be added that an odd number is one more than an even number.
A simple regular expression for "an even number of as" ( {a2n : n ≥ 0}) is (aa)*, while "an odd number of as" is (aa)*a.
Building on that, we can two cases for the original question: (aa)*(bb)* and (aa)*a(bb)*b, which can be combined into (aa)*(ab+ε)(bb)*. (Assuming you are using + for alternation and ε for the empty string.)
You correctly observe that n and m must either be both even or both odd. It only needs to be added that an odd number is one more than an even number.
A simple regular expression for "an even number of as" ( {a2n : n ≥ 0}) is (aa)*, while "an odd number of as" is (aa)*a.
Building on that, we can two cases for the original question: (aa)*(bb)* and (aa)*a(bb)*b, which can be combined into (aa)*(ab+ε)(bb)*. (Assuming you are using + for alternation and ε for the empty string.)
edited Nov 13 '18 at 17:12
answered Nov 13 '18 at 17:06
ricirici
152k19132197
152k19132197
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277926%2fregex-lr-an-bm-n-m-is-even-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Consider the automaton which accepts
a^nwhen n is even. The "failure" case would be thatnis odd. In that "failure" case, you want a following automatonb^mwhich accepts whenmis odd. Otherwise, you want a following automatonb^mwhich accepts whenmis even.– Corion
Nov 13 '18 at 10:02
1
what about
^(?=(?:..)*$)[ab]*$(regex101.com/r/1aldWm/1)– Nahuel Fouilleul
Nov 13 '18 at 10:31
@Corion how would I write that in terms of r? I understand the logic just not the syntax.
– solnay
Nov 13 '18 at 10:39
2
I think this question better suits Mathematics StackExchange site and it's
regular-expressionstag. Also, are you sure there is a possibility to use a regular expression for this? Seems more like a grammar is to be used here, since irregularities you mention oppose the "regular" part in "regex" :)– Asunez
Nov 13 '18 at 10:45
1
@Asunez As I showed, the language is regular, as it can be accepted by a DFA.
– Corion
Nov 13 '18 at 10:48