ikegami:
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:
<pedagogical_mode>
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;
}
</pedagogical_mode>
--roboticus
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|