Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^3: Finding Combinations of Pairs

by andye (Curate)
on Jan 14, 2009 at 17:21 UTC ( #736293=note: print w/replies, xml ) Need Help??

in reply to Re^2: Finding Combinations of Pairs
in thread Finding Combinations of Pairs

I like this solution, because it's so simple and efficient:

#!/usr/bin/perl -w use strict; my @lines = <DATA>; my (%words, %count); @words{map {split ' '} @lines} =1; my @uniq = keys %words; # create list of unique words foreach my $i (0 .. $#uniq - 1) { foreach my $j ($i+1 .. $#uniq) { # count the pairs do { $count{"$uniq[$i] $uniq[$j]"}++ if /$uniq[$i]/ an +d /$uniq[$j]/ } foreach @lines; } } print map "$_ : $count{$_}\n", # print the pairs in s +orted order sort {$count{$a} <=> $count{$b}} keys %count; __DATA__ dog monkey cat cat ball stone monkey iron cat zoo

(NB see parent node)

Replies are listed 'Best First'.
Re^4: Finding Combinations of Pairs
by Limbic~Region (Chancellor) on Jan 14, 2009 at 18:57 UTC
    like this solution, because it's so simple and efficient:

    It has at least one bug and I am not sure why you think it is efficient.

    First the bug. You have /$uniq[$i]/. This can do the wrong thing for at least two reasons. The first is that it will match "and" in the line of "where is the sand" even though the word "and" never appears. The second reason is that you haven't use \Q\E or quotemeta to ensure any metacharacters are escaped. It doesn't take case into consideration either, but I am not sure any of the solutions do (including my own).

    Another potential bug is the fact that you don't consider that in the line "hello world goodbye cruel world" that each pairing of world should double and not be counted once. I am not sure if this is or isn't a requirement but it is a bug in one intepretation.

    As to why it isn't efficient. This can be done in a single pass and yet your solution has you going through every line in the data (N^2 - N) / 2 times (where N = # of unique words). In other words, you check every single pairing of unique word against every single line even if the word doesn't appear on that line.

    Cheers - L~R

      In the context of a discussion about providing obfuscated and inefficient answers that look reasonable, I assumed that that post was a well-executed joke.
        My funny bone is often broke. In spoken conversation I have learned to pick up on many social cues when to not take something literally but it is an effort and I commonly get it wrong. Written communication is so much worse. Thanks for the pointer.

        Cheers - L~R

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2021-10-26 03:44 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (90 votes). Check out past polls.