Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm a little rusty, but this looked fun and I took a stab at it. Here's a convaluted, winding, unoptimized memory hog that generates a high scoring solution, but not always the highest possible score given arbitrary lines of input...
#!/usr/bin/perl -w use strict; my @lines=<DATA>; my %hash = (); my %highscores = (); my @final_words = (); my $final_score = 0; my $longword = ''; for my $letter ( "a" .. "z" ) { my $line = ''; for (@lines) { $line = $_; my @words = split; for my $word (@words) { my $tmp = scalar grep {/$letter/} split (//,$word); if (!$hash{$line}{$letter}{highscore} || $hash{$line}{$letter}{high +score} < $tmp) { $hash{$letter}{$line}{highscore} = $tmp; $hash{$letter}{$line}{word} = $word; $hash{$line}{$letter}{highscore} = $tmp; $hash{$line}{$letter}{word} = $word; } } } $highscores{$letter} = 0; $highscores{$letter} += $hash{$letter}{$_}{highscore} for keys %{$has +h{$letter}}; if (!$highscores{bigletter}{score} || $highscores{bigletter}{score} < + $highscores{$letter}) { $highscores{bigletter}{score} = $highscores{$letter}; $highscores{bigletter}{letter} = $letter; } } my $bl = $highscores{bigletter}{letter}; print "Letter with highest score is \"$bl\" with $highscores{bigletter +}{score} occurrences\n"; for my $line (keys %{$hash{$bl}}) { push @final_words,$hash{$line}{$bl}{word}; } print "\nWords chosen:\n"; print "$_\n" for @final_words; $longword .= $_ for @final_words; my @final_letters = split (//,$longword); for my $letter ( "a" .. "z" ) { my $count = grep {/$letter/} @final_letters; $final_score += ($count * $count + $count) / 2; } print "\nFinal score: $final_score\n"; __DATA__ alabama arkansas alaska delaware hawaii indiana kansas montana delawar +e sazerac sangrita radiator gastank engine heater fender wheel detent battery clutch mirr +or window alloy amalgam vanadium copper steel rutabaga limabean cress carrot sorrel squash cabbage pepper lettuce be +et leek celery endive rhubarb parsnip pumpkin azalia camellia dahlia gardenia gentian vervain canna hepatica bluebel +l anemone oleander lasagna macaroni pastina gnocchi tortelli alfabeto mascagni britten menotti unamas tacotime pizzahut tacobell panago tacomayo edojapan hardees caramel marzipan taiglach taffy brittle fondant toffee dragee __OUTPUT__ Letter with highest score is "a" with 26 occurrences Words chosen: alabama unamas sazerac radiator lasagna amalgam caramel azalia mascagni rutabaga Final score: 457
Note the score is less than the shown maximum of 474 for this data set.

In reply to Re: Challenge: Letter Power Revisited by delirium
in thread Challenge: Letter Power Revisited by Limbic~Region

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 studying the Monastery: (4)
As of 2024-03-29 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found