Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day Limbic~Region,

Here's my take on a solution. It uses the 2of12inf.txt dictionary. It takes about a minute to run on my OS. [as always, YMMV] It doesn't use any particularly noticeable amounts of memory.

#!/usr/bin/env perl use 5.010; use strict; use warnings; use autodie; die "Usage: $0 dictionary max_letters min_letters\n" unless @ARGV >= 2 +; my ($DICT, $MAX, $MIN) = @ARGV; $MIN //= 1; my %dict_extract; open my $fh, '<', $DICT; while (<$fh>) { s/%?\r?\n?$//; next if length > $MAX; ++$dict_extract{+length}{join '' => sort split ''}; } close $fh; my $best_letters = ''; my $most_words = 0; for my $letters (keys %{$dict_extract{$MAX}}) { my $count = get_count($letters, {}); if ($most_words < $count) { $most_words = $count; $best_letters = $letters; } } say "Best: $best_letters - Count: $most_words"; sub get_count { my ($letters, $counted) = @_; return 0 if $counted->{$letters}++; my $len = length $letters; my $count = $dict_extract{$len}{$letters} // 0; if ($len > $MIN) { $count += get_count($_, $counted) for map { my @sub_string = split '', $letters; splice @sub_string, $_, 1; join '' => @sub_string; } 0 .. $len - 1; } return $count; }


Best: aeinprst - Count: 346

I also wrote a complementary script to check the results (this only takes a second or two to run):

#!/usr/bin/env perl use 5.010; use strict; use warnings; use autodie; die "Usage: $0 dictionary solution max_letters\n" unless @ARGV == 3; my ($DICT, $SOLUTION, $MAX) = @ARGV; my $count = 0; open my $fh, '<', $DICT; while (<$fh>) { s/%?\r?\n?$//; next if length > $MAX; if (matching_word($_)) { ++$count; say; } } close $fh; say "Total: $count"; sub matching_word { state $solution_letters = [ split '' => $SOLUTION ]; my ($word) = @_; foreach my $letter (@$solution_letters) { my $pos = index $word, $letter; if ($pos != -1) { substr($word, $pos, 1) = ''; return 1 if length $word == 0; } } return 0; }


air airs an ... tripes trips tsar Total: 346

-- Ken

In reply to Re: Challenge: 8 Letters, Most Words by kcott
in thread Challenge: 8 Letters, Most Words by Limbic~Region

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2018-01-24 00:53 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (254 votes). Check out past polls.