{"id":301,"date":"2013-09-05T14:00:45","date_gmt":"2013-09-05T21:00:45","guid":{"rendered":"http:\/\/alexboisvert.com\/musings\/?p=301"},"modified":"2013-09-01T16:03:08","modified_gmt":"2013-09-01T23:03:08","slug":"npr-puzzle-for-2013-09-01-a-perl-solution","status":"publish","type":"post","link":"https:\/\/alexboisvert.com\/musings\/2013\/09\/05\/npr-puzzle-for-2013-09-01-a-perl-solution\/","title":{"rendered":"NPR Puzzle for 2013-09-01: a perl solution"},"content":{"rendered":"<p>Here&#8217;s <a href=\"http:\/\/www.npr.org\/2013\/09\/01\/217627890\/shush-listen-carefully\">this past week&#8217;s NPR puzzle<\/a>:<\/p>\n<blockquote><p>Think of a well-known celebrity who goes by a single name \u2014 the last two letters of which are alphabetically separated by only one letter (like A and C, or B and D). Replace this pair of letters with the one that separates them, and you&#8217;ll have a common, everyday word. What is it?<\/p><\/blockquote>\n<p>It used to be that a puzzle like this was tough to solve with a computer program, because there wasn&#8217;t a list of famous names available anywhere.  Well, <a href=\"http:\/\/crosswordnexus.com\/downloads\/FamousNames.txt\">now there is such a list<\/a>, and it makes solving things like this a snap.  You will still need a list of common words to solve this puzzle, but those are available all over.  I used the <a href=\"http:\/\/www.puzzlers.org\/dokuwiki\/doku.php?id=solving:wordlists:about:start\">Enable dictionary from the NPL<\/a>.<\/p>\n<p>A few interesting quirks happened along the way to solving this puzzle.  First, it turns out that this particular person isn&#8217;t listed in Wikipedia by only his\/her first name.  So I had to tweak the code a bit to look at all possible first names in my list.  This created a bit of a logjam because ROBERT -> ROBES comes up a ton.  So the final hacked code to give the answer is below.  Not my best work, but not bad for ten minutes.<\/p>\n<p>It&#8217;s also a relief to see that the intended answer had a score of 100.  YAY FAMOUS PEOPLE LIST<br \/>\n<!--more--><br \/>\n[perl]#!\/usr\/bin\/perl -w<\/p>\n<p>use strict;<\/p>\n<p>my $USAGE = &#8220;Usage: $0 NAME_FILE WORDLIST_FILE&#8221;;<\/p>\n<p>my $name_file = $ARGV[0] or die $USAGE;<br \/>\nmy $word_list_file = $ARGV[1] or die $USAGE;<\/p>\n<p>my %names;<br \/>\nopen FILE1, $name_file or die $!;<br \/>\nwhile (<FILE1>)<br \/>\n{<br \/>\n\tchomp;<br \/>\n\t# Everything uppercase for consistency<br \/>\n\tmy $name = uc $_;<br \/>\n\tmy $score = 100;<br \/>\n\t# Take out names with spaces<br \/>\n\t#next if $name =~ \/ \/;<br \/>\n\tif ($name =~ \/^(.*)\\t(\\d+)$\/) {$name = $1;$score = $2;}<br \/>\n\t$names{$name} = $score;<br \/>\n}<br \/>\nclose FILE1;<\/p>\n<p>my %words;<br \/>\nopen FILE2, $word_list_file or die $!;<br \/>\nwhile (<FILE2>)<br \/>\n{<br \/>\n\tchomp;<br \/>\n\t$words{uc $_} = 1;<br \/>\n}<br \/>\nclose FILE2;<\/p>\n<p># Go through names to find a match<br \/>\nmy %used;<br \/>\nwhile (my ($name, $score) = each(%names))<br \/>\n{<br \/>\n\tmy $first; my $second;<br \/>\n\tif ($name =~ \/^(.*?) \/) {$name = $1;}<br \/>\n\tif ($name =~ \/^(.*)(..)$\/)<br \/>\n\t{<br \/>\n\t\t$first = $1;<br \/>\n\t\t$second = $2;<br \/>\n\t}<br \/>\n\tnext unless $second;<br \/>\n\t#print &#8220;$name $first $second\\n&#8221;;<br \/>\n\tmy $num = ord(substr($second,0,1));<br \/>\n\tif (ord(substr($second,1,1)) == $num + 2)<br \/>\n\t{<br \/>\n\t\tmy $let = chr($num + 1);<br \/>\n\t\tmy $word = $first . $let;<br \/>\n\t\tif ($words{$word} &#038;&#038; !$used{$name})<br \/>\n\t\t{<br \/>\n\t\t\tprint &#8220;$score\\t$name\\t$word\\n&#8221;;<br \/>\n\t\t\t$used{$name} = 1;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}[\/perl]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s this past week&#8217;s NPR puzzle: Think of a well-known celebrity who goes by a single name \u2014 the last two letters of which are alphabetically separated by only one letter (like A and C, or B and D). Replace &hellip; <a href=\"https:\/\/alexboisvert.com\/musings\/2013\/09\/05\/npr-puzzle-for-2013-09-01-a-perl-solution\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","hentry","category-coding","category-puzzles"],"_links":{"self":[{"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":6,"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/posts\/301\/revisions\/307"}],"wp:attachment":[{"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alexboisvert.com\/musings\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}