Created
February 20, 2011 23:40
-
-
Save avar/836425 to your computer and use it in GitHub Desktop.
Find words that contain at least "msixpodual"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use Text::Table; | |
my @flags = split //, "msixpodual"; | |
my %flags; | |
@flags{@flags} = (); | |
my %words; | |
while (my $word = <>) { | |
chomp $word; | |
my $cp = $word; | |
my $remaining = ''; | |
my $score = 0; | |
for my $flag (@flags) { | |
if ($cp =~ s/$flag//i) { | |
$score++; | |
} else { | |
$remaining .= $flag; | |
} | |
} | |
# Skip non-serious candidates | |
next if length($cp) > 0; | |
if ($score) { | |
$words{$word} = { | |
score => $score, | |
remaining => $remaining, | |
} | |
} | |
} | |
my @order = sort { | |
($words{$b}->{score} <=> $words{$a}->{score}) or | |
length($a) <=> length($a) | |
} keys %words; | |
my @tr; | |
for my $word (@order) { | |
push @tr => [ | |
$words{$word}->{score}, | |
$word, | |
$words{$word}->{remaining}, | |
]; | |
} | |
my $tb = Text::Table->new( | |
"Score", "Word", "Remaining" | |
); | |
$tb->load(@tr); | |
print $tb; |
There's a much improved version of this on perl5-porters now. This was just something I hacked up and is full of bugs and misfeatures :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops: length($a) <=> length($a)
(Not that it matters; if two words have the same score, then they have the same length as well. In fact, the score /is/ the length!)