my $given = $ARGV[0];
# ...
while (<$dict_fh>) {
chomp;
print "$_\n" if is_subset($_, $given);
}
sub is_subset {
my ($str1, $str2) = @_;
my (%h1, %h2);
$h1{$_}++ for split //, $str1;
$h2{$_}++ for split //, $str2;
for my $chr (keys %h2) {
return if ! $h1{$chr} || $h2{$chr} > $h1{$chr};
}
return 1;
}
####
my $input = $ARGV[0];
$input = str2val($input);
# dictionary already pre-processed
# create hash of possible subset values
# ...
while (<$dict_fh>) {
chomp;
my ($word, $val) = split /\t/;
print "$word\n" if $is_subset{$input - $val};
}
####
a = 1
b = (a * max) + 1
c = (b * max) + 1
...
z = (y * max) + 1
####
sub str2val {
my ($str) = @_;
my %convert = (A => 1, B => 6, C => 31, D => 156, E => 781, F => 3906);
my $val = 0;
for my $char (split //, $str) {
$val += $convert{$char};
}
return $val;
}
sub val2str {
my ($val) = @_;
my %convert = (A => 1, B => 6, C => 31, D => 156, E => 781, F => 3906);
my $str = '';
return $str if $val < 1;
for my $char (sort {$convert{$b} <=> $convert{$a}} keys %convert) {
my $count = int($val / $convert{$char});
$str .= ($char x $count);
$val %= $convert{$char};
}
return $str;
}
####
my $input = $ARGV[0];
my %is_subset = map {$_ => 1} powerset(split //, $input);
while (<$dict_fh>) {
chomp;
my ($word, $normalized) = split /\t/;
print $word if $is_subset{$normalized};
}
sub powerset {
# ...
return map {join '', sort @$_} @subsets;
}