Live data from Hacker News

Python code to solve xkcd 1313 by Peter Norvig

nbviewer.ipython.org

51–60 of 75 posts

Re: Python code to solve xkcd 1313 by Peter Norvig

#51
post #48

It's too bad he didn't try to tackle the optimal regexp problem and settled for approximations - it may be a NP-hard problem, but all the example solutions are short enough that the instances might be still tractable. Would've been nice to know for sure.

Another xkcd applies here: http://xkcd.com/356/

Re: Python code to solve xkcd 1313 by Peter Norvig

#52

Earlier quoted context omitted.

Such a regex cannot exist: 1. Take an existing solution to finding the shortest regex given a list of inclusions an exclusions. 2. Take another unrelated arbitrary program. 3. Combine the two, when the arbitrary program terminates, use the existing solution to solve the problem. 4. Such a regex would have to solve the halting problem to know if the arbitrary program terminates and the existing solution solves the pro…

So long as the unrelated arbitrary program is of finite length, and the regex golfer / arbitrary program composite is running on a machine with a finite amount of state, the halting problem can be solved by running each program for 2^(bits of state) steps or until a state is repeated. So as long as you can show that a program that is not finite in length will not halt in a finite amount of time (I think this is the c…

Or, to make it easier, just put a generous time limit on each program for whatever computer you're running them on.

Re: Python code to solve xkcd 1313 by Peter Norvig

#53
post #9

I am not sure why Norvig omits president Obama. That said, "[mtg]a" does match him, so at least Munroe tries.

Given that, an interesting variation of the problem is: what's the easiest way to transform an expression to incorporate new data? After the next election, one can toss away the result and re-generate a new one from scratch. But is there an easier way to absorb additional terms, both inclusions and exclusions?

Re: Python code to solve xkcd 1313 by Peter Norvig

#54
post #48

It's too bad he didn't try to tackle the optimal regexp problem and settled for approximations - it may be a NP-hard problem, but all the example solutions are short enough that the instances might be still tractable. Would've been nice to know for sure.

I coded a brute-force one before: https://github.com/darius/sketchbook/blob/master/regex/find_...

Though it cares about the size of the AST rather than the concrete syntax. I can't try running it now, I'm on an iPad.

Re: Python code to solve xkcd 1313 by Peter Norvig

#55
One thing not mentioned in this article:

1. The greedy algorithm has an O(log(n)) approximation ratio, meaning it produces a regex guaranteed to use a number of terms within a multiplicative O(log(n)) factor of the optimal regex.

2. Unless P != NP, set cover cannot be approximated better than the greedy algorithm. In other words, the only general solutions you'll find (unless you're using some special insight about how regular expressions cover sets of strings) will be no better than a constant factor improvement in produced regex size than the greedy algorithm.

That being said, regexes (esp disjunctions of small regexes) are not arbitrary sets. So this problem is a subset of set cover, and certainly may have efficient exact solutions.

Re: Python code to solve xkcd 1313 by Peter Norvig

#56
post #21
post #3

I love Norvig's Python posts. He really gets the spirit of the language and has fun with it.

Yes, I love expressions as this one: winners, losers = (winners - losers), (losers - winners)

Was that in an earlier version? I can't find that in the article now.

Re: Python code to solve xkcd 1313 by Peter Norvig

#58
post #57

What tool does Norvig use to create this json file? Does iPython have this as a feature (somehow allowing formatted text)?

By "this json file", do you mean the ipython notebook itself? (It's saved as JSON behind-the-scenes. That's what you'll get if you click "download notebook".)

If so, yes, that's what a saved ipython notebook is. See: http://ipython.org/notebook.html for an overview of ipython notebooks.

You can export it to html, latex, etc using "ipython nbconvert --to ". See: http://ipython.org/ipython-doc/dev/interactive/notebook.html...

Basically, the website you're seeing (nbviewer) hosts ipython notebooks (json files) and converts them to static html for viewing.

Re: Python code to solve xkcd 1313 by Peter Norvig

#59
post #56
post #21

Earlier quoted context omitted.

Yes, I love expressions as this one: winners, losers = (winners - losers), (losers - winners)

Was that in an earlier version? I can't find that in the article now.

Yep, he originally threw out all names that appeared on both lists, while he's now arbitrarily assigning them to the winners' set. Not sure about the reasoning behind the change, but there it is. The quoted line computed new winning and losing sets that excluded all commonalities.

Re: Python code to solve xkcd 1313 by Peter Norvig

#60
post #23

Earlier quoted context omitted.

Such a regex cannot exist: 1. Take an existing solution to finding the shortest regex given a list of inclusions an exclusions. 2. Take another unrelated arbitrary program. 3. Combine the two, when the arbitrary program terminates, use the existing solution to solve the problem. 4. Such a regex would have to solve the halting problem to know if the arbitrary program terminates and the existing solution solves the pro…

If a regex is required to only match x if p(x), is the regex required to match ALL x such that p(x)? Although this is probably just pointless nitpicking on my part.

Of course it is required (within the computational limits of your computer, of course)

If p(x) means 'x is an integer', you could use the regex /^1$/ since it matches integers only (in particular, the integer 1). That's not sufficient, you need to match all integers.

edited:format

Post reply on HN