0
Okay, the challenge is that you have to write a regex that matches half of all strings.
Some rules to clarify:
For ANY string length(>0), if I permute all strings(of that length), exactly half of them should be matched. Put more lightly, if I generate a random string of that length it should have a 50% chance of being matched. (this means that you cannot just match all even-length strings)
You may assume that strings contain only A-Z, a-z, and 0-9.
0 length strings are undefined, you can choose to either match them or not.
Shortest regex wins.
/^[A-Z0-4]*$/ works, doesn't it? – Tim Seguine – 2014-01-16T20:53:23.107
@TimSeguine No, if strings could have only uppercase, or only lower case it would. But since most strings have a combination of both, that misses most strings – Cruncher – 2014-01-16T20:55:46.760
/^[\w0-4]/
works, doesn't it? – John Dvorak – 2014-01-16T20:56:46.377oh yeah sorry, it works for one character but gets worse and worse after that. If i remove the star and the $ though, it should work. It takes then half of all one character strings and all of the other strings that start with one of those characters. – Tim Seguine – 2014-01-16T20:56:50.197
@JanDvorak I don't see how. Can you explain? – Cruncher – 2014-01-16T21:00:14.717
@Cruncher the same as above. If it starts with lowercase or a low digit, match. Otherwise, don't. – John Dvorak – 2014-01-16T21:01:46.563
@JanDvorak but doesn't \w match all word characters? – Tim Seguine – 2014-01-16T21:02:32.983
@TimSeguine if it's case insensitive, then
A-Z
it is. – John Dvorak – 2014-01-16T21:02:58.263@JanDvorak The problem doesn't say anything about case insensitivity – Tim Seguine – 2014-01-16T21:04:07.007
@TimSeguine I mean, if
\w
matches both lowercase and uppercase – John Dvorak – 2014-01-16T21:05:31.0301@JanDvorak I know, that is exactly my point. You are matching too many one character strings. There are 62, and you match 57 of them. It does it that way in PCRE at least IIRC. Is there a dialect in which it doesn't? If there is, then I concede defeat. – Tim Seguine – 2014-01-16T21:08:14.787
@JanDvorak Please don't say that
\W
matches uppercase letters. – Kendall Frey – 2014-01-16T21:15:35.243