Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi Ken!

Ok, I didn't post the entire code. I did archive a version with the labeled next which I will post below, sans the dictionary file which is 150+K lines. Right now I don't remember where I got this word list from - sorry. I don't claim that this is a masterpiece of Perl code. I didn't write this with the intention of posting it here and as such it contains some things that I wouldn't want new Perler's to emulate. There are some unnecessary lc operations, I would use explicit my variables for the loops, etc.

Here is how this code came about... I was playing "Word Nut" on my cellphone. I got up to a high level with the addition of turning the difficulty level up to the max. I was confronted with a grid of boxes like a crossword puzzle, except that there are no sentences for clues! The puzzle that "triggered" me had just 5,6,7 letter boxes, no "easy" 3 or 4 letter words. I typed in something more than 20 completely valid 5 letter words (formed according to the 7 letters given). None of these words appeared in the puzzle, but the program counts them and I got bonus points for finding valid words that don't appear.

You can get a hint from the program, but that means that you are required to watch a bunch of ads. For some reason that offended me and I decided to retaliate with some Perl code!

In order to "get started", it is important to find a word, any word that appears in the crossword grid. Once that is done, there will be an intersecting word and you will know that for example, for that word, the 2nd letter is an "r" or whatever.

For the "triggering" puzzle, I typed in 32 valid 5 letter words with the help of my "cheater" program before I got the first "hit" in the crossword grid! Before completely solving the puzzle, there were 42 valid words that did not appear in the grid!

These puzzles use uncommon words like "resaw". I am a native born, reasonably read English speaker and although I understand that word, I've never used it myself in a sentence, read a sentence with it or heard anybody else say that word!

Here is the code... I don't see any obvious way that this can "fail" other than the code that I did post. Please also note that this code works "most of the time".

use strict; use warnings; use Data::Dumper; # Word Master quick hack on 5/18/2021 # en-US.dic is a flat file of sorted words (about 150K words) open (my $fh, '<', 'en-US.dic') or die "can't find dictionary file: en +-US.dic"; my @dic = map{chomp; $_}grep {!/'/ and !/[A-Z]/}<$fh>; #no apostrophes + allowed #no proper nouns Rome (capit +al letters) $0 =~ m!(\\|/)([\w.]+)\s*$!; print "Word Master ($2)\n"; #like wm.pl this allows renaming the pro +gram print "# Enter list of letters by using semicolon followed by the lett +ers\n"; print "# example>:omdee\n"; print "# in pattern, use simple dash for single unknown letters\n"; print "# m-de would match \"mode\"\n"; print "# quit|exit|q to exit program\n"; print "\n"; my $line; my %master_letter_freq; my $master_letter_list =""; #could be repeated letters like ee,tt,etc while ( (print "list of letters or pattern: "),$line=<>, $line !~ /\s* +quit|exit|q\s*$/i) { next unless $line =~ /\S/; #skip blank lines chomp $line; my $cur_pattern; if ($line =~ /^\s*:([a-zA-Z]+)\s*$/) # new list of letters { $master_letter_list = lc $1; ## Force all letter lists to LOWE +R CASE only for (split //,$master_letter_list) { $master_letter_freq{$_}++; } print "master_letter_freq:\n"; print Dumper \%master_letter_freq; ##################### } elsif ($line =~ /^\s*([a-zA-Z-]+)\s*$/) #no leading ":", this is a p +attern { $cur_pattern = lc $1; # Force all patters to LOWER CASE only if ($master_letter_list eq "") { print "No master letter list exists -> can't run this patt +ern! Error!\n"; next; } my $regex = ''; for (split //, $cur_pattern) # gen regex { if ($_ ne '-' and !exists $master_letter_freq{$_}) { print "Pattern has a letter that's not in master list! + Error!\n"; next; } if ($_ eq '-') {$regex .= "[$master_letter_list]";} else {$regex .= "$_";} } my @result = grep{/^$regex$/i}@dic; # filter out any result if the number of times a # letter is repeated is more than the number of times it is # repeated in the master letter list # RESULT: foreach (@result) { my %seen; $seen{$_}++ for (split //,lc $_); # print "Testing Result $_, seen histogram is:\n"; # print Dumper \%seen; foreach (keys %seen) { next RESULT if ($seen{$_} > $master_letter_freq{$_}); } print "$_\n"; } } else { print "Illegal input line!\n"; } } __END__ Copied from Command Line - shows difference on different runs list of letters or pattern: :otrwreh list of letters or pattern: --r-w threw throw list of letters or pattern: ---w thew trow whew list of letters or pattern: list of letters or pattern: :otrwreh list of letters or pattern: --r-w threw throw list of letters or pattern: ---w thew trow whew list of letters or pattern: q list of letters or pattern: :otrwreh master_letter_freq: $VAR1 = { 'w' => 1, 'h' => 1, 't' => 1, 'o' => 1, 'r' => 2, 'e' => 1 }; list of letters or pattern: ---w thew trow list of letters or pattern

Added: sorry if I offended anybody by describing the origins of the code. That is relevant here. I wanted to post some code that I have seen fail. If I change the code, that might affect its ability to "fail". I am not asking for help to make the code "better". I just don't understand why this can fail "some, but not all of the time". I do acknowledge that there are style failings in this code. I was just explaining that I did this quickly and hence some style failings.


In reply to Re^2: Next from inner loop only works "most of the time" by Marshall
in thread Next from inner loop only works "most of the time" by Marshall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (None)
    As of 2024-04-19 00:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found