Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@avar
Created February 20, 2011 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avar/836425 to your computer and use it in GitHub Desktop.
Save avar/836425 to your computer and use it in GitHub Desktop.
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;
@tamias
Copy link

tamias commented Feb 22, 2011

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!)

@avar
Copy link
Author

avar commented Feb 22, 2011

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