Sunday, 25 November 2012

Regular expression - How not to do it

I just ordered a book on regular expressions, after giving in to the realization that they kept coming up again and again. I figure if I'm going to keep using them I might as well try to make the best of a bad situation.

Until amazon can ship me a copy of Mastering Regular Expressions over from the UK, I'll have to do this the hard way. Let's take a look at exercise 1 for the quiz tomorrow:

Devise an NFSA to accept L( ( (0 + 1) (01)*)* )
I'm not convinced this color  coding has made that any more approachable but, i'll take a crack:
EDIT: This was admittedly a bad crack at this question, but i'll leave it.

 The language is made up of all strings that are made up of any number of the substrings '0X', '1X' where X is string made up of any number of the substring '01'.
Examples:
  • empty string
  • 0
  • 1
  •  001
  • 101

Maybe if I listed them in a more ordered way:

len(x)= = 0
  • ''
numstrings = 1
 len(x) = 1
  • '0'
  • '1'
numstrings = 2
len(x) == 2
numstrings = 0

len(x) == 3
  • '001'
  • '101'
numstrings = 2
len(x) == 4
  • '0010'
  • '0011'
  • '1010'
  • '1011'
numstrings = 2

Again a pattern emerges in the listing process.  I think i should consider the length of the strings in this language. So that should be easy to figure out and probably relevant to the later proof.

for an arbitrary string x in L:
length(x) == (1 + 2x )y for arbitrary naturals y,x

if y == 0:
    return 0
else:
    if x==0:
        return y
    else:
        return (1+2x)*y

In the first return statement, we take care of the empty string. In the second, we take care of the stuation in which x is 0. Namely, all the strings in L that do not contain the substring 01.

It seems my initial list of valid strings may have been flawed. I'll come back to this later.

Thursday, 15 November 2012

Back to DFSA's

The tutorial six exercise looks challenging, particularly because I've been taking a longer break than intended due to the long weekend. I will attempt question 1.

Devise a DFSA that accepts the language of finite binary strings whose length is a multiple of 3. Explain why it is correct.
I assume any DFSA that accepts this language, must have 3 states. I say this because the question should be reworded as the following:

Devise a DFSA that accepts the language L = {x |  len(x)%3==0}. Explain why it is correct.
We can see that this function f(x) = len(x)%3 can have only 3 possible outcomes for an arbitrary string x and a corresponding arbitrary integer i:
 
          1 iff len(x) = 3i+1
f(x) =  2 iff len(x) = 3i +2
      0 iff len(x) = 3i
 
Because of this close relationship to f(x), I assume the DFSA will have 3 states. As to what they will be, I shall post more on that later.
 
Thanks for reading.