Live data from Hacker News

Python code to solve xkcd 1313 by Peter Norvig

nbviewer.ipython.org

21–30 of 75 posts

Re: Python code to solve xkcd 1313 by Peter Norvig

#23

Earlier quoted context omitted.

"Write the shortest regex that only matches programs that successfully generate the shortest regex given lists of inclusions and exclusions."

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.

Re: Python code to solve xkcd 1313 by Peter Norvig

#24

This was posted a few days ago on Code Golf: http://codegolf.stackexchange.com/questions/17718/meta-regex... That link includes a perl 10-liner to do the same.

I dunno if calling a pre-implemented solution is quite the same as writing one yourself.

Re: Python code to solve xkcd 1313 by Peter Norvig

#25
post #13

Can someone explain what does this line mean and why does he use it as heuristic? key=lambda c: 3*len(matches(c, uncovered)) - len(c)

It means to use as the key for max 3 (number of things this pattern matches) - (number of characters in pattern). The basic idea is that patterns that match more things are good, while patterns that are long are bad; if you select the pattern with the maximal score for the above expression it's matching more things than others or is shorter than others, or both. Why the "3 " bit, good question. Would be interesting t…

Norvig did say why he chose 3: " I may have chosen a bad tradeoff. (I arbitrarily decided that matching a winner is 3 times more important than spending a character (because a disjunction seems to take about 3 characters on average).)"

Re: Python code to solve xkcd 1313 by Peter Norvig

#27
post #18
post #13

Can someone explain what does this line mean and why does he use it as heuristic? key=lambda c: 3*len(matches(c, uncovered)) - len(c)

IMHO, sadly, that part is serving as a shibboleth. The clue is how he explains all the simple parts of the script with comments, but leaves this part unexplained, making some of us feel left out of the cool club. Implying: if you don't grasp it immediately you must not be up to it. Overall it's a great post, but as to the cryptic parts of it, it's disappointing to see this kind of thing on the part of someone we all…

First, it is explained in the text above it.

Second, seriously?

A line of code that isn't as clear as it could be in an ipython notebook he probably dashed off in an hour is somehow a reflection on his generosity of spirit? I think you need to recheck your expectations.

Re: Python code to solve xkcd 1313 by Peter Norvig

#28
post #3

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

Most times when I read Norvig's Python I get the same feeling as when a tough riddle answer is given, except with Norvig you don't wonder whether anyone actually arrives at the answer the first time they hear it, you know he did.
Post reply on HN