Monks~

Let me first start by admitting that this was a homework problem I had. All the same, I am curious to see what kind of solutions can be had here because I have yet to come up with any.

The problem:
"Write a regular expression that matches all strings of digits with no repeated digit"
Thus 01534 would pass, and 01034 would fail.

I figured that the easiest way to do this would be to start with a restricted alphabet {0,1,2,3} for example. I also decided to require that all four numbers show up (since simply adding ?'s everywhere would allow fewer numbers)

/
(0(1(23|32)|2(13|31)|3(12|21))
|1(0(23|32)|2(03|30)|3(02|20))
|2(0(13|31)|1(03|30)|3(01|10))
|3(0(12|21)|1(02|20)|2(01|10))
)/

Solves this problem, but only slightly more easily than brute force.

As you might guess, this solution does not scale well to the 10 digit case... (actually it is about 17 megs...)

My next thought was to combine some of these cases:

/
((01|10)(23|32)
|(02|20)(13|31)
|(03|30)(12|21)
|(12|21)(03|30)
|(13|31)(02|20)
|(23|32)(01|10)
)/

The first solution has 121 characters and the second 91; I think that the second might scale to the 10 digit case more easily too.

Oh yeah! I forgot about the cruelest part of all. You are restricted to the "basic" regexp operators (),?,|,* and no others.

My impulse here is to think that there is some trade off between allowing the |'s to expand and just brute forcing all cases. My intuitition as a math major is to say that 3 is the sacred number afterwhich the brute force is no longer the better option. But I have not bothered with any sort of justification of that.

Good luck,
Matt

In reply to regexp golf - homework by Boots111

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.