Find words that contain at least "msixpodual"
#!/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; |
This comment has been minimized.
This comment has been minimized.
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
This comment has been minimized.
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!)