Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Your skill will accomplish
what the force of many cannot

Re^2: possible combinations in sequence

by ikegami (Pope)
on Jun 09, 2006 at 06:04 UTC ( #554432=note: print w/ replies, xml ) Need Help??

in reply to Re: possible combinations in sequence
in thread possible combinations in sequence

If you want a huge speed boost, replace
@rv=($i, @rv, map{$_.':'.$i} @rv);
push @rv, $i, map{$_.':'.$i} @rv;

By the way, I found confusing the use of $i for something which is not an index and not even numerical. $part would have worked fine, since it's an element of @parts. Well, it was named @parts before you renamed to worthless @t. Nice algorithm, but shoddy code.

Comment on Re^2: possible combinations in sequence
Select or Download Code
Re^3: possible combinations in sequence
by roboticus (Canon) on Jun 09, 2006 at 12:10 UTC

    Thanks for the tip on the speed boost. Once I get the benchmarking stuff installed I'll play with it. While I do like your suggestion, I prefer the order that my method generates--all one-word combinations first, then the two-word combinations, etc.)

    Re: shoddy code. Yeah, I guess so, consider me properly admonished. ++ for calling me on it and keeping me honest. When I thunk up the technique to use, I just erased the original function body and started whacking away at it. I didn't think to clarify things by using better variable names. (Of course, I just came off of a little golfing trip so my head was in "trim keystrokes" mode.</lame_excuse_mode>) Now, I guess the proper thing to do is to clean it up a little and insert your suggestion, so here goes:


    sub function { my @parts = split /:/, shift; # Null is the complete list of combinations for # an empty word list my @combinations=(); # Sequentially (recursively with tail recursion # removed) rebuild the combination list adding one # new word each iteration for my $new_word (@parts) { # Given a complete set of combinations for a # given list of words, we can add a new word to # the list and generate all new valid combinations # by concatenating to the original list: push @combinations, # the new word (a single word is a valid # combination) $new_word, # and the original list with the new word # glommed onto the end of each member map {$_.':'.$new_word} @combinations ; } return @combinations; }


      Just when you thought it couldn't get any faster...




Log In?

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

How do I use this? | Other CB clients
Other Users?
As of 2014-03-15 23:33 GMT
Find Nodes?
    Voting Booth?

    Have you used a cryptocurrency?

    Results (322 votes), past polls