Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Challenge: Letter Power Revisited

by delirium (Chaplain)
on Apr 21, 2009 at 20:02 UTC ( #759097=note: print w/ replies, xml ) Need Help??

in reply to Challenge: Letter Power Revisited

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.

Comment on Re: Challenge: Letter Power Revisited
Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://759097]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-11-25 23:14 GMT
Find Nodes?
    Voting Booth?

    My preferred Perl binaries come from:

    Results (160 votes), past polls