Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: (Golf) Dependency List Prioritization

by petral (Curate)
on Mar 18, 2002 at 15:14 UTC ( [id://152487]=note: print w/replies, xml ) Need Help??


in reply to (Golf) Dependency List Prioritization

This seems to do it at 93 (but there should be ways to shorten it):
sub f { $h{$_}||=[]for map@$_,%h=@_;sub x{my$x=1;$x+=x($_)for@{@h{@_}||[]};$x} +sort{x($a)<=>x$b}keys%h }
that's:
sub f { $h{$_}||=[]for map@$_,%h=@_; sub x { my$x=1; $x+=x($_)for@{@h{@_}||[]}; $x } sort{x($a)<=>x$b}keys%h }
I think this algorithm is in the panther book[1].   The basic idea is that if you count the dependencies all the way down, then something which depends on something else must have a count of at least 1 more than its depender(?).   Neither the exact counts, nor exactly what's being counted matter (that is 'a' can count 'd' in both 'b' and 'c').

[1](update) Well, something like it.   I don't want to blame the authors for my crude implementation.

one more update:     @h{@$_}=@h{@$_}for%h=@_;and   $x+=x($_)for@{@h{@_}||0};lowers it to 88.

well, one more update:   Actually, there's no need to add all those 1's, just let the size of the list be the value:
sub x { 1,map{x($_)}@{@h{@_}||0} }
which makes the whole thing 78: @h{@$_}=@h{@$_}for%h=@_;sub x{1,map{x($_)}@{@h{@_}||0}}sort{x($a)<=>x$b}keys%h    p

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found