in reply to Another word puzzle with too many permutations
if you noticed I intentionally put dorf into dictionary,@words = qw/toyota dorf honda audi ford/; %hash=('adduhifoorndatoyota' => ''); $unique = 0; foreach $word (@words){ foreach $key (sort keys %hash){ $length = 0; if($key=~m/\#/){$_ = $'} else {$_= $key} foreach $l (split //, $word){if(/$l/){$_ = "$`$'"; $length++;}} if($length == length($word)){ $newKey = '#'.$_; if($key=~m/\#/){ if($newKey eq '#'){$newKey .= $unique++} $hash{$newKey} = $hash{$key}; $hash{$newKey}.= ' '.$word; } else { $hash{$newKey} = $word; } } } } foreach (sort keys %hash){ print $hash{$_},"\n" if /\#\d+/; } -----OUTPUT----- toyota dorf honda audi toyota honda audi ford
Update:
@words = qw/toyota dorf honda audi ford/; %hash=('adduhifoorndatoyota' => ''); $unique = 0; foreach $word (@words){ foreach $key (sort keys %hash){ $length = 0; if($key=~m/\#/){$_ = $'} else {$_= $key} foreach $l (split //, $word){if(/$l/){$_ = "$`$'"; $length++;}} if($length == length($word)){ $newKey = '#'.$_; if($key=~m/\#/){ if($newKey eq '#'){$newKey .= $unique++} $hash{$newKey} = $hash{$key}; $hash{$newKey}.= ' '.$word; } else { $hash{$newKey} = $word; } } } } foreach (sort keys %hash){ print $hash{$_},"\n" if /\#\d+/; } -----OUTPUT----- toyota dorf honda audi toyota honda audi ford
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Another word puzzle with too many permutations
by hdb (Monsignor) on Oct 16, 2013 at 12:51 UTC |