We've already got a meta-regex-golf problem as inspired by the xkcd comic
But, this regex golf looks fun, too! I want to distinguish between the states of the US and the regions of Italy. Why? I'm a citizen of both countries, and I always have trouble with this*.
The regions of Italy are
Abruzzo, Valle d'Aosta, Puglia, Basilicata, Calabria, Campania, Emilia-Romagna, Friuli-Venezia Giulia, Lazio, Liguria, Lombardia, Marche, Molise, Piemonte, Sardegna, Sicilia, Trentino-Alto Adige/Südtirol, Toscana, Umbria, Veneto
and the states of the USA are
Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin, Wyoming
Your job is to write a program which distinguishes these lists with a regular expression. This is a new game, so here's the
- Distinguishing between lists must be done with a single matching regular expression.
- Your score is the length of that regular expression, smaller is better.
To be clear: all work must be done by the regular expression -- no filtering, no replacements, no nothing... even if those are also done with regular expressions. That is, the input should be passed directly into a regular expression, and only the binary answer (match / no match) can be used by later parts of the code. The input should never be inspected or changed by anything but the matching expression. Exception: eating a newline with something akin to Ruby's
chomp is fine.
Your program should take a single entry (optionally followed by
EOF if it makes things easier) from either list from stdin, and print to stdout the name of that list. In this case, our lists are named
To test your code, simply run both lists through it. Behavior may be undefined for strings which do not occur in the list.
This might have to be done on a language-by-language basis. In Perl,
is a matching regular expression. However, in Python,
import re re.compile('foobarbaz')
does the same thing. We wouldn't count the quotes for Python, so I say we don't count the
m/ and final
/ in Perl. In both languages, the above should receive a score of 9.
To clarify a point raised by Abhijit, the actual length of the matching expression is the score, even if you generate it dynamically. For example, if you found a magical expression
then you should not report a score of 12:
m has length 24. And just to be extra clear, the generated regular expression can't depend on the input. That would be reading the input before passing it into the regular expression.
input> Calabria Italy input> New Hampshire USA input> Washington USA input> Puglia Italy
* Actually, that's a lie. I have never had any trouble with this at all.