in reply to Scrabble word arrangements with blank tiles
1) Find all unique sets of letters given an input of letters and wildcards
2) Calculate the number of words for each unique set
The maximum possible number of letter sets is 26**wildcards, which means that you can comfortably fit all the letter sets in memory up to 4 wildcards. Scrabble only has 2 blanks, so this isn't a problem. Calculating the number of words possible from each set is a simple factorial / letter count operation. The following is a rough but workable solution, which you can edit as necessary for various letter / wildcard combinations.
use strict; use warnings; my $letters = 'ABCDEF??'; my $length = length($letters); ## We'll be using factorials a lot, so it's best to pre-calculate my @factorial = (0,1); $factorial[$_] = $factorial[$_-1] * $_ for 2..$length; my ($blanks, @lsets, $lset, $nlset, $words, $twords); ## Find the number of blanks while ($letters =~ /[^A-Z]/) { $letters =~ s/[^A-Z]//; $blanks++; } ## Produce unique letter sets, given blanks @lsets = ($letters); while ($blanks--) { my %lsets; for $lset (@lsets) { for ('A'..'Z') { $nlset = join '', sort split //, $lset.$_; $lsets{$nlset} = (); } } @lsets = keys %lsets; } ## Calculate the number of words possible from each letter set for (@lsets) { $words = $factorial[$length]; my %lcount; $lcount{$_}++ for split //, $_; for (keys %lcount) { $words /= $factorial[$lcount{$_}] if $lcount{$_} > 1; } $twords += $words; } print $twords;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Scrabble word arrangements with blank tiles
by Anonymous Monk on Nov 14, 2005 at 20:53 UTC | |
|
Re^2: Scrabble word arrangements with blank tiles
by tphyahoo (Vicar) on Nov 16, 2005 at 15:24 UTC |