Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

We've already got a meta-regex-golf problem as inspired by the xkcd comic

copyright 2013 Randall Munroe

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

Rules

  • 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 \n or 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 Italy and USA.

To test your code, simply run both lists through it. Behavior may be undefined for strings which do not occur in the list.

Scoring Issues

This might have to be done on a language-by-language basis. In Perl,

m/foobarbaz/

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 m,

n="foo(bar|baz)"
m=n+n

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.

Example Session

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.

share|improve this question
    
Can you please explain, what you mean by "no filtering, no replacements, no nothing... even if those are also done with regular expressions.". Just to clarify, does it mean filtering, replacements of the list of states/regions or the focus is wider? –  Abhijit Jan 7 at 4:29
    
@Abhijit edited. Is that clearer? –  boothby Jan 7 at 4:34
    
Hopefully, let me post an answer and see if you feel it violates the rule in any way :-) –  Abhijit Jan 7 at 4:35
3  
@Eliseod'Annunzio: DC is not a state –  Kyle Kanos Jan 7 at 14:21
1  
"Behavior may be undefined for strings which do not occur in the list." this rule is broken: it allows one to return USA in case of such a string, hence you would just have to check Italian regions, and return USA otherwise. –  Lohoris Jan 14 at 18:59
show 11 more comments

5 Answers

up vote 7 down vote accepted

Perl - 51 36 bytes (for regex)

print<>=~/.A|ise|net|te|z.o|[cp]a|[lr]ia|r[cd]/?"Italy
":"USA
"

Nothing special, but may as well post it, because it's different to other 51 bytes solution.

Or alternatively, shorten my already short solution by 15 bytes. This wins for now, I think.

share|improve this answer
add comment

Perl, 40 chars

Approaching this from the other direction, i.e. matching the U.S. states:

[DNIOWy]|ss|M.n|^A.*a|or|[aguh]i|[sth]\b

The only Perl/PCRE-specific feature in the regexp is the \b word boundary anchor, which I used instead of the $ end-of-string anchor to let it match "South Carolina".

Here's the regexp in a Perl one-liner for testing:

perl -nE 'say /[DNIOWy]|ss|M.n|^A.*a|or|[aguh]i|[sth]\b/ ? "USA" : "Italy"'
share|improve this answer
    
This is a more golfy test harness: perl -pe '$_=/re/?"USA\n":"Italy\n"' –  Pseudonym Jan 7 at 23:09
1  
@Pseudonym: meh. As long as it doesn't count in the score, might as well keep it readable. –  Ilmari Karonen Jan 7 at 23:29
add comment

Ruby (plain regex), 44

$_ = gets.chomp
puts /'|-|(([^gn]i|gn|at)a|[hst]e|to|zo)$|To|La|pa/ ? "Italy" : "USA"

You know what? Case sensitivity is the best start-of-word anchor.

I'm not sure, but I think I owe the pa to Hax0r778's answer.

share|improve this answer
add comment

Perl - 51

(<STDIN> =~ m/'|-|ru|pu|at|pa|az|gu|mb|rc|ie|rd|ci|os|abr|mol|ven/)?printf("Italy\n"):printf("USA\n");
share|improve this answer
add comment

JavaScript 42

alert(/at|gn|mp|sc|-|'|((zi?|t)o|[hts]e|[lrd]ia)$/g.test(prompt())?"Italy":"USA")

I was initially going to work this out from the USA side, as eliminating KWXY from the USA list strips a lot of the States away... But Italy had it bested by a good 17 characters...

If we go with fat arrow notation we can reduce this to a simple function with a return variable.

r=s=>/at|gn|mp|sc|-|'|((zi?|t)o|[hts]e|[lrd]ia)$/g.test(s)?"Italy":"USA"

> r("South Dakota") // USA
> r("Puglia") // Italy
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.