Perl: the Markov chain saw PerlMonks

### Re: Hangman - Hanging with Friends

by jwkrahn (Monsignor)
 on Sep 24, 2011 at 10:55 UTC ( #927642=note: print w/replies, xml ) Need Help??

in reply to Hangman - Hanging with Friends

```# Scrabble distribution
our %letter_distribution = qw(
a 9  b 2  c 2  d 4
e 12 f 2  g 3  h 2
i 9  j 1  k 1  l 4
m 2  n 6  o 8  p 2
q 1  r 6  s 4  t 6
u 4  v 2  w 2  x 1
y 2  z 1
) ;

You never use this variable anywhere so why is it here?

```  sort { score_word(\$a) cmp score_word(\$b) }

score_word() returns a numeric value so that should be:

```  sort { score_word(\$a) <=> score_word(\$b) }

And if you used a Schwartzian Transform you wouldn't have as much overhead on all those subroutine calls.

```# search for matching words
my @possible_words =
map \$_->[ 1 ],
sort { \$a->[ 0 ] <=> \$b->[ 0 ] }
map length() == length( \$word_pattern ) && pattern_word( \$word_pat
+tern, \$_ ) ? [ score_word( \$_ ), \$_ ] : (),
@dictionary;

```sub score_word {
my (\$word) = @_ ;
my \$points = 0 ;
my @letters = split //, \$word ;
\$points += \$letter_points{\$_} foreach @letters ;
return \$points ;
}

You could use List::Util::sum and reduce that to:

```use List::Util qw/ sum /;

sub score_word { sum( @letter_points{ split //, \$_[ 0 ] } ) }

```  my %deny_letters = map { \$_ => 1 } split(//, \$pattern) ;
my @p = split //, \$pattern ;

Why split the same thing twice:

```  my @p = split //, \$pattern ;
my %deny_letters = map { \$_ => 1 } @p ;

Replies are listed 'Best First'.
Re^2: Hangman - Hanging with Friends
by onelesd (Pilgrim) on Sep 25, 2011 at 01:37 UTC
You never use this variable anywhere so why is it here?

I had used it in a prior version as part of the "best letters" algorithm. In this version the best letters are selected based on how many possible words a letter is part of. I think the letter distribution should also be part of the algorithm but I haven't figured a good way to balance those two points yet.

Thank you for your other suggestions!

Create A New User
Node Status?
node history
Node Type: note [id://927642]
help
Chatterbox?
 hippo does not feed the trolls.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2017-11-24 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (348 votes). Check out past polls.

Notices?