in reply to using perl to find words for scrabble

#!/usr/bin/perl # https://perlmonks.org/?node_id=11105638 use strict; use warnings; use Path::Tiny; my @words = grep /^[a-z]{4,}\z/ && /[aeiouy]/, # lc, size & vow +el path('/usr/share/dict/words')->lines({chomp => 1}); my @tiles = map +('a' .. 'z')[rand 26], 1 .. 9; print "tiles: @tiles\n"; my @matches; my $pattern = join '', map "$_?", sort @tiles; for my $word ( @words ) { if( join('', sort split //, $word) =~ /^$pattern$/ ) { push @matches, $word; } } print "\nmatches:\n\n@matches\n";

Typical output:

tiles: h l n l q g a l o matches: along gall gallon goal hall halo halon hang hogan llano loan loll long

Replies are listed 'Best First'.
Re^2: using perl to find words for scrabble
by Aldebaran (Curate) on Sep 16, 2019 at 22:31 UTC
    Typical output:

    ...indeed, as I can replicate. Thanks for your post. Yours has the advantage of getting up and running quickly and having some form of probability to try different combinations.

    C:\Users\tblaz\Documents\evelyn>perl 1.tyb.pl >1.tyb.txt C:\Users\tblaz\Documents\evelyn>type 1.tyb.txt tiles: o o y o q p i p g matches: goop goopy pipy pogy poop yogi C:\Users\tblaz\Documents\evelyn>

    I know, poop, ha-ha, but who knew pipy and pogy were english words?

    It seems to me that the method of letter generation should reflect the distribution of a given game and that a hash would be the appropriate data structure to do so. What is an elegant way to initialize the 26 key-value pairs? Frankly, there has to be a lot of initialization to pull off any game....

    distribution of letters in scrabble

    Thanks for your comments

      Simple way to do letter frequency:

      #!/usr/bin/perl # https://perlmonks.org/?node_id=11105638 use strict; use warnings; use Path::Tiny; my @letters = split //, 'aaaaaaaaabbccddddeeeeeeeeeeeeffgggghhiiiiiiii +ijkllllmmnnnnnnooooooooppqrrrrrrssssttttttuuuuvvwwxyyz'; my @tiles = @letters[map rand @letters, 1 .. 9]; print "tiles: @tiles\n"; my $pattern = join '', map "$_?", sort @tiles; my @matches = grep join('', sort split //) =~ /^$pattern$/, grep /^[@tiles]{2,}\z/ && /[aeiouy]/, # lc, size & vowel path('/usr/share/dict/words')->lines({chomp => 1}); print "\nmatches:\n\n@matches\n";

        Actually, this has the chance of selecting more than one 'q'.
        The following is a better way.

        #!/usr/bin/perl # https://perlmonks.org/?node_id=11105638 use strict; use warnings; use Path::Tiny; use List::Util qw( shuffle ); my @letters = split //, 'aaaaaaaaabbccddddeeeeeeeeeeeeffgggghhiiiiiiii +ijkllllmmnnnnnnooooooooppqrrrrrrssssttttttuuuuvvwwxyyz'; my @tiles = ( shuffle @letters )[0 .. 8]; print "tiles: @tiles\n"; my $pattern = join '', map "$_?", sort @tiles; my @matches = grep join('', sort split //) =~ /^$pattern$/, grep /^[@tiles]{2,}\z/ && /[aeiouy]/, # lc, size & vowel path('/usr/share/dict/words')->lines({chomp => 1}); print "\nmatches:\n\n@matches\n";