Regular Expression Problem

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3102 Views - Last Post: 14 July 2007 - 10:58 PM

#1 ajaymatrix   User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Regular Expression Problem

Posted 11 July 2007 - 11:12 PM

Given a set of alphabets S= {A,B,C,D}

Write a Regular Expression for Determining all possible combinations where the letter B occurs twice..

eg :

ABCB : valid
BBCD : valid
ABBB : invalid
DCBA : invalid


I know the code if the 2 B's occur together which is rather simple...

Any one has some idea ...
Is This A Good Question/Topic? 0
  • +

Replies To: Regular Expression Problem

#2 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Regular Expression Problem

Posted 11 July 2007 - 11:57 PM

This must be school/course work. Post the code you have written so far to try and solve this question.
Was This Post Helpful? 0
  • +
  • -

#3 ajaymatrix   User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: Regular Expression Problem

Posted 12 July 2007 - 07:36 AM

/BB/ is for 2 Bs occuring together..

but if the Bs are occuring at some other position then how to determine...
I am not that very good at Regex..

It was not school/course work.. this was the question asked in a popular web company's aptitude test...
Was This Post Helpful? 0
  • +
  • -

#4 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Regular Expression Problem

Posted 12 July 2007 - 10:16 AM

That seems to have nothing to do with the question:


Write a Regular Expression for Determining all possible combinations where the letter B occurs twice..

Your regular expression is just checking if there are two consecutive B's in the string, it's not "determining all possible combinations". I don't think a regexp can do that. But if the strings are already constructed and all you need to do is see if there are two B's anywhere:

my $foo = 'BCBD';
if (2 == ($foo =~ tr/B/B/)) {
   print "Valid\n"
}
else {
   print "Invalid\n";
}	 


Was This Post Helpful? 0
  • +
  • -

#5 ajaymatrix   User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: Regular Expression Problem

Posted 12 July 2007 - 11:14 AM

hey..
that even i know if we know the string beforehand...
Was This Post Helpful? 0
  • +
  • -

#6 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Regular Expression Problem

Posted 12 July 2007 - 12:01 PM

first you have to figure out all the possible permutations of two B's within the character set: A,B,C,D, for example, assuming only the B can be repeated:

ABBB
ABBC
ABBD
ACBB
ADBB

and so on.

I don't think you would even use a regular expression for that so the original question makes little sense to me. Is this apptitude test online?

This post has been edited by KevinADC: 12 July 2007 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#7 ajaymatrix   User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: Regular Expression Problem

Posted 12 July 2007 - 01:39 PM

no the test was not online...

maybe they didn't want the exact soln.. maybe they were looking for the approach...
Was This Post Helpful? 0
  • +
  • -

#8 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Regular Expression Problem

Posted 12 July 2007 - 02:25 PM

well, that is the question you posted so I am not sure what further advice to give you. Post the actual question if you can, if not, I guess thats the end of this discussion?
Was This Post Helpful? 0
  • +
  • -

#9 ajaymatrix   User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: Regular Expression Problem

Posted 12 July 2007 - 09:32 PM

that was a written test and the question posted above was what was asked..
Maybe instead of a complete solution they were expecting what was your approach...


so the question remained pretty ambiguous and I couldn't think of any solution..

That is why I thought it would be a better to have a discussion in this forum..

Any way thanks for your precious time Kevin...
Was This Post Helpful? 0
  • +
  • -

#10 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Regular Expression Problem

Posted 13 July 2007 - 10:50 AM

I am not sure if you are being saracastic or not about my precious time. If you're not, you're welcome. If you are, well, sorry you feel that way, I was just trying to help you the best I know how.
Was This Post Helpful? 0
  • +
  • -

#11 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: Regular Expression Problem

Posted 13 July 2007 - 11:27 AM

Hi, I don't belong to this forum i.e. I know nothing about Perl or Python, but I wonder if this is the regular expression that you want
/[ABCD]*BB[ABCD]*/

I apologize if I intrude but I have to try.
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Regular Expression Problem

Posted 14 July 2007 - 01:05 PM

Kevin is correct - regular expressions are a matching/description tool - they cannot build all possible permutations of a string.
Was This Post Helpful? 0
  • +
  • -

#13 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: Regular Expression Problem

Posted 14 July 2007 - 01:33 PM

Hi I see now after I read it all carefully that my try was wrong.
I tried to solve this but I don't think it can be solved with just reg expr, as usual the more experienced ones are right.
Was This Post Helpful? 0
  • +
  • -

#14 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Regular Expression Problem

Posted 14 July 2007 - 02:24 PM

The easiest way to get the permutations is to use the List::Permutor module:

use List::Permutor;
my $perm = new List::Permutor qw/A B C D/;
while (my @set = $perm->next) {
	print "One order is @set.\n";
}


but of course there are never two B's in any of the permutations. We would have to add another B to the list but then they will all have two B's.

I think ajaymatrix might have misunderstood the original question or did not ask the same question as was on the test. Hard to say since the orignal question is not available for examination.
Was This Post Helpful? 0
  • +
  • -

#15 ajaymatrix   User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: Regular Expression Problem

Posted 14 July 2007 - 09:13 PM

I was never sarcastic..
I really appreciate the help you gave me in understanding this question...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2