Regular Expressions
Moderator: Coders of Rage
-
- Chaos Rift Regular
- Posts: 173
- Joined: Thu Feb 11, 2010 9:46 pm
Regular Expressions
Not technically a programming question since this is a theoretical CS question, but sort of relevant.
I need to give a regular expression for a language that contains an odd number of 1's or exactly two 0's. The alphabet is {0, 1}.
Here's my attempt, idk if this is correct or not.
(1* 0 1* 0 1*) OR ( (0* 1 0* 1 0*)* 1 (0* 1 0* 1 0*)* )
Can anyone help me out? I'm new to this and could use some help getting on the right track.
I need to give a regular expression for a language that contains an odd number of 1's or exactly two 0's. The alphabet is {0, 1}.
Here's my attempt, idk if this is correct or not.
(1* 0 1* 0 1*) OR ( (0* 1 0* 1 0*)* 1 (0* 1 0* 1 0*)* )
Can anyone help me out? I'm new to this and could use some help getting on the right track.
Last edited by X Abstract X on Tue Oct 18, 2011 11:47 am, edited 2 times in total.
- LeonBlade
- Chaos Rift Demigod
- Posts: 1314
- Joined: Thu Jan 22, 2009 12:22 am
- Current Project: Trying to make my first engine in C++ using OGL
- Favorite Gaming Platforms: PS3
- Programming Language of Choice: C++
- Location: Blossvale, NY
Re: Regular Expressions
I'm trying really hard to read this, but with all the 1's 2's and 0's it's hard to see what you're asking.X Abstract X wrote:Not technically a programming question since this is a theoretical CS question, but sort of relevant.
I need to give a regular expression for a language that contains an odd number of 1's or exactly two 2's. The alphabet is {0, 1}.
Here's my attempt, idk if this is correct or not.
(1* 0 1* 0 1*) OR ( (0* 1 0* 1 0*)* 1 (0* 1 0* 1 0*)* )
Can anyone help me out? I'm new to this and could use some help getting on the right track.
What do you mean you have to "give" a regular expression for a language? What are you trying to match exactly? And is there any 2's or is it just a 0 and a 1 for the language?
There's no place like ~/
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Regular Expressions
I'm not really sure what you're asking. Could you explain "for a language that contains and odd number of 1's or exactly two 2's. The alphabet is {0, 1}."?
Try providing us with some examples of what you'd like to be matched by the regular expression, and what you don't want to match.
Try providing us with some examples of what you'd like to be matched by the regular expression, and what you don't want to match.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Regular Expressions
I think he's in one of those way-the-fuck-too-abstract CS classes and is assuming that what he is doing is some sort of standard CS question... but it's not. =/
And I can't make heads or tails from the grammar/syntax of your "regular expression."
And I can't make heads or tails from the grammar/syntax of your "regular expression."
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: Regular Expressions
I took a very similar class this summer, "Theory of Computation", class can go bite itself. However as it stands you have written a contradiction, sort of. If Sigma = {0, 1} then it not feasible two have any 2's, let alone exactly two 2's. I assume you meant something else?X Abstract X wrote:Not technically a programming question since this is a theoretical CS question, but sort of relevant.
I need to give a regular expression for a language that contains an odd number of 1's or exactly two 2's. The alphabet is {0, 1}.
Here's my attempt, idk if this is correct or not.
(1* 0 1* 0 1*) OR ( (0* 1 0* 1 0*)* 1 (0* 1 0* 1 0*)* )
Can anyone help me out? I'm new to this and could use some help getting on the right track.
However the set of possible answers would include:
1, 01, 10, 010, 001, 100, ... (thus the pattern we want to match is (0*10*)*
But we don't want to match an even number of one's, so we can't match the following pattern:
01010, 1010, 0110, 110, 11, 011, ... (thus the pattern we don't want to match is (0*10*10*)*
Unfortunately this approach isn't very helpful in finding the regex we desire in all cases. However, a DFA is. I'm going on the assumption that your class is similarly designed to mine, if not post back and I'll explain in more detail. I came up with the following DFA (for your given requirements, ignoring the "exactly two 2's")
Notice the '+' means OR. Also notice that the regex handles the case of just one 1, as well as every odd case beyond just 1. Ignore all the scratch work, the regex I believe you want is the one in red.
edit: whoops, I forgot a zero.. the regex you really want is
R = 0*10* + 0*10*(0*10*1)*
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Regular Expressions
Wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeell!!!!!
He blinded me with science...
He blinded me with science...
-
- Chaos Rift Regular
- Posts: 173
- Joined: Thu Feb 11, 2010 9:46 pm
Re: Regular Expressions
Gah, I meant to write exactly two 0's. Thanks for all the replies, I skimmed them and short seems to know exactly what I'm talking about. Gonna read your reply in more detail and reply back. The class I'm in is also called Theory of Computation.
Edit: Thank you very much short! The key is to construct the DFA instead of trying to reason through it in my head :P
Edit: Thank you very much short! The key is to construct the DFA instead of trying to reason through it in my head :P
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Regular Expressions
I can't quite seem to get the same answer as short, but if I'm looking at this correctly, the right regex is:
This is for the whole problem, including the "or exactly 2 zeros".
Code: Select all
(0*10*(10*10*)*)|(1*01*01*)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: Regular Expressions
I don't have time to go back and do the work again, but it seems like you figured it out. Glad to help!!
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
-
- Chaos Rift Regular
- Posts: 173
- Joined: Thu Feb 11, 2010 9:46 pm
Re: Regular Expressions
Yep, thanks short. You're example was great, showed me how to use a DFA to get my regex :D
Ginto, that's the same answer that I got
Ginto, that's the same answer that I got
Last edited by X Abstract X on Tue Oct 18, 2011 11:54 pm, edited 1 time in total.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Regular Expressions
I have no clue what the hell just happened in this topic. I'm just glad Short is a psychic savant. If these forums had a reputation rating, he would have just gained a lot of it.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: Regular Expressions
+1 Reputationdandymcgee wrote:I have no clue what the hell just happened in this topic. I'm just glad Short is a psychic savant. If these forums had a reputation rating, he would have just gained a lot of it.
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: Regular Expressions
lol don't make me blush. I do think psychic savant would make an awesome forum title...k1net1k wrote:+1 Reputationdandymcgee wrote:I have no clue what the hell just happened in this topic. I'm just glad Short is a psychic savant. If these forums had a reputation rating, he would have just gained a lot of it.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Regular Expressions
short wrote:lol don't make me blush. I do think psychic savant would make an awesome forum title...k1net1k wrote:+1 Reputationdandymcgee wrote:I have no clue what the hell just happened in this topic. I'm just glad Short is a psychic savant. If these forums had a reputation rating, he would have just gained a lot of it.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!