Problems? Is your data what you think it is? PerlMonks

### 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
andye,
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.
tilly,
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

Create A New User
Node Status?
node history
Node Type: note [id://736293]
help
Chatterbox?
 [moritz]: choroba in python I use jsonschema [moritz]: I'm sure there's a Perl implementation of it too [choroba]: it seems Kelp and similar help you a lot with dispatching, but how to do something similar with contents? [moritz]: like JSON::Schema [Corion]: ambrus++ # more bugs than lines [Corion]: That's like "works on my machine" ;-) [choroba]: I mean, I'd like to say "this path should run this sub, but first it should check the user is authenticated, has the following permissions, and the contents is valid against a given schema [ambrus]: no, that means I haven't even tried to run it, might not even parse [ambrus]: so it doesn't work on my machine [choroba]: i.e. I know JSON::Schema, I'm just curious whether there's a ready solution to plug it into a web service

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2016-12-08 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
On a regular basis, I'm most likely to spy upon:

Results (141 votes). Check out past polls.