Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Anagrams & Letter Banks

by tybalt89 (Monsignor)
on Oct 27, 2017 at 22:48 UTC ( [id://1202211]=note: print w/replies, xml ) Need Help??


in reply to Anagrams & Letter Banks

I am interested in finding groups of words not that are all anagrams of each other, but rather which all share a common “letter bank,” which is a word (in the list) with no duplicate letters.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1202179 use strict; use warnings; chomp( my @words = <DATA> ); # find the "banks" in the word list my %banks = map { $_, [ ] } grep !/(.).*\1/, @words; # find what a word "banks" to and save if "bank" exists for ( @words ) { my $banksto = tr///csr; exists $banks{$banksto} and push @{ $banks{$banksto} }, $_; } print "@$_\n" for values %banks; __DATA__ ab aabb aaabbb xxyy xxxyyy

Replies are listed 'Best First'.
Re^2: Anagrams & Letter Banks
by johngg (Canon) on Oct 28, 2017 at 11:26 UTC

    Perhaps I'm misunderstanding something but it seems to me that my $banksto = tr///csr; is only removing adjacent duplicate letters. Adapting your code:-

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1202179 use strict; use warnings; use Data::Dumper; chomp( my @words = <DATA> ); # find the "banks" in the word list my %banks = map { $_, [ ] } grep !/(.).*\1/, @words; print Data::Dumper->Dumpxs( [ \ @words, \ %banks ], [ qw{ *words *bank +s } ] ); # find what a word "banks" to and save if "bank" exists for ( @words ) { my $banksto = tr///csr; print qq{$_ -> $banksto\n}; exists $banks{$banksto} and push @{ $banks{$banksto} }, $_; } print "@$_\n" for values %banks; __DATA__ ab aabb aaabbb aaabbab xxyy xxxyyy

    produces the following:-

    @words = ( 'ab', 'aabb', 'aaabbb', 'aaabbab', 'xxyy', 'xxxyyy' ); %banks = ( 'ab' => [] ); ab -> ab aabb -> ab aaabbb -> ab aaabbab -> abab xxyy -> xy xxxyyy -> xy ab aabb aaabbb

    My reading of the OP's requirement would imply that the added 'aaabbab' should also bank to 'ab' and e.g., 'cabbage' banks to 'cabge' not 'cabage' which is what comes out of tr//csr. What am I misunderstanding?

    Update: Thinking about it further, I realised I was fixating on the tr//csr in tybalt89's post (which usage I had never encountered before) and ignoring the join "", sort split //, $key used by the OP. That is what I was misunderstanding :-/

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202211]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-25 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found