Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: how to find combine common elements of an array?

by jaredor (Priest)
on Apr 09, 2011 at 22:37 UTC ( [id://898542]=note: print w/replies, xml ) Need Help??


in reply to Re: how to find combine common elements of an array?
in thread how to find combine common elements of an array?

Here's a better version. I was going to wipe my scratchpad and saw that I could do one thing better ... then another ... then realized that there didn't need to be an identified element per subgraph...

#!/usr/bin/env perl use strict; use warnings; use List::MoreUtils(qw(uniq)); my @array = map {[split]} ("11 12", "11 13", "9 8 7", "3 4", "11 4"); my %g = (); for my $i (@array) { my @v = map {@$_} uniq map {$g{$_} or [$_]} @$i; @g{@v} = (\@v) x @v; } print join ("\n", map {join " ", sort {$a<=>$b} @$_} uniq values %g), +"\n";

Same output as above, although now the ordering of the lines is fortuitous. (But obviously can be set, if desired, by another sort.)

Hopefully it's a bit more clear that, since each iteration creates a complete set of associated vertices, the process finds the sets of vertices of the connected subgraphs of an arbitrary graph.

(Please pardon the enthusiasm, this is the kind of stuff I like to think about.)

Replies are listed 'Best First'.
Re^3: how to find combine common elements of an array?
by jaredor (Priest) on Jun 23, 2014 at 05:03 UTC

    You are too kind Miller, and you flatter me to think that my one-off is worth remembering!

    Such kindness deserves a little extra effort, and so here is a little defensive coding to go along with the main thrust of the code: If you are not sure that there will be no duplication of numbers in any given list, then in general you will not be able to collapse the list to a minimal set of elements. For example, if you just have a single list, '3 3 4', then the code will return a list of '3 3 4', not '3 4'. (However, if the currently duplicated element has been seen before, there will be no duplication.) This potential problem is countered by another uniq statement just before the assignment to @v and is (to my taste) slightly ugly, but necessary if you want to run this on slightly muddier data sets.

    You must be right about the title to this post being too obscure, since I presume you are one of only the six who upvoted this post. With 225 XP left to go, my long, hard slog to curate will only happen by me personally upvoting other posts in "Recent Threads." ;-)

    Thank you again for thinking of this post. Double thanks for crediting me elsewhere!

    --Jared

    P.S. I've only run the code through my somewhat fallible wet-ware, so caveat coder. One of these days I'll be an active Perl coder again and I won't make as many educated guesses as I do now. (Project Euler was my perl outlet for a while, but that site is down now. Gee, I can't recall, did I use the same password for PM as for PE? I hope it wasn't the other way around before, say, May 20th, 2009 ;-)

    P.P.S. EDIT Oops, this was supposed to be a reply to wind.

Re^3: how to find combine common elements of an array?
by wind (Priest) on Jun 12, 2014 at 00:03 UTC

    I recently came by a question on stackoverflow that mirrored this thread: Merging a list of lists of lists

    It's unfortunate that this thread has such a poor title, as I doubt google is going to find it very often. But I definitely remembered your solution and was able to search through all my past posts to find it.

    I'm still in awe of your use of uniq on array references, so just wanted you to know that I referenced your code. :)

    - Miller

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-29 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found