language agnostic - How to generate every possible combination of a string when given a list of values -
Say that there is a list of my values, this list can be of any length:
"100", "200", "300" , "400", "500", ... and I have a template string that contains some tokens which need to be replaced:
"@ Token1 @ - @ tokens2 @ - @ tokens3 @ -....- token n @"
Using the list of values, I can add values to the templates How can I create every possible combination?
Values can be used more than once, so the result can be "100-100-100". Additional points for a method that takes into account a variable amount of tokens!
edit: a fixed number of deleted token version
Takes advantage of recycling just for fun:
r ($ values, false, $ numtokens); 100-100-100 as well to get / wrong function r ($ value, $ unique = true, $ depth = 3, $ deposit = array ()) {if ($ depth == 0) { Print implode ("-", $ accumulated). "\ N"; } Else {foreach ($ id = & gt; $ value as $ t) {if ($ unique) unset ($ value [$ id]); R ($ values, unique $, $ depth-1, array_merge ($, array array ($ t)); If ($ unique) $ value [$ id] = $ t; }}}
(some adaptations may be required for different languages)