0

I had this question in my end semester exam, unfortunately I couldn't solve it and tried it for several days and no luck.

Condition- For every substring of length 4 in a string of length n, write a RE to force the rule- there must be exactly three 1's.

My solution kind of looked like (1+11+111+€)(0111)*(0+€).

But this is obviously wrong, string 11011 is a valid solution too.

Update- my new solution is (1+11+111+€)(0111)*(0+01+011+€).

Update- the plus operator is actually 'OR'

Update- € is empty string

Update - the string length has no requirements. A string of length 5 will have 2 substrings of length 4, the first 4 chars and the last 4 chars

3 Answers3

0

I think the professor is looking for the realization that in order for the condition to hold, a 1 may never be bracketed by zeros on both sides.

(0?11)*

To complete the picture, we also need to include the case for n=1 where either a 0 or a 1 is allowed (I presume).

^[01]$|(0?11)*

I'm using traditional regex here, where [...] denotes a character class, | is "or", parentheses do grouping, and * specifies zero or more repetitions (Kleene star).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Wouldn't 011011 be in your RE language? But consider the first substring of length 4, 0110 - it has two 1's but we need exactly three 1's. – Ajinkya Jumbad Feb 07 '17 at 11:42
  • Oh sorry, I missed the "exactly" and assumed "at least". Do you want me to delete this answer or should I leave it as a clue for someone else to perhaps develop to a working answer? – tripleee Feb 07 '17 at 11:53
  • It's okay, you can leave it here. If you get the answer, post it here ! – Ajinkya Jumbad Feb 07 '17 at 11:56
0

Edit: Consider the following regular expression where refers to the empty string and assuming the alphabet consists of only {0,1,€}:

€ + (0111)* + (1110)* + (1101)* + (1011)*

Rohit
  • 1
  • 1
0

Python 2

import re

# regular expression
# '^' in the start of the expression means from the very start of the string
# "[^1.]*" means match any character unless it is '1'
# '$' in the end of the expression means till the very end of the string
# Summary:
# from the start of the string till the end of it check if we have
# '1' exactly 3 times, 
# and before or after them you might find any type of characters
# and these characters must not be equal '1' ...
expression = '^[^1.]*1[^1.]*1[^1.]*1[^1.]*$'

# testing strings
tests = ["01110", "11100", "00111", "010101", "00100100111", "00100"]


prog = re.compile(expression)

for test in tests:
    print 'matched' if prog.match(test) else 'not matched'
  • could you explain how you came up with this ? – Ajinkya Jumbad Feb 17 '17 at 10:40
  • I have updated my answer adding more explanation on how the regular expression works, this is a link that may help you figure out how regular expressions work http://www.rexegg.com/regex-quickstart.html – Omar Sherif Feb 17 '17 at 14:26