如何生成具有 X 标记和每个标记的 Y 值的字符串的所有可能性

发布于 2024-09-27 12:27:11 字数 515 浏览 1 评论 0原文

所以,我有一个模板字符串,其中包含 X 数量的令牌。假设它可能看起来像这样:

template = "render=@layer0@-@layer1@-@layer2@-@layer3@-@layer4@"

显然,令牌采用 @tokenname@ 的形式。在这个假设的情况下,它有五个令牌。每个令牌都有一组不同的可能值。例如:

token0Values = ['t0value1'];
token1Values = ['t1value1','t1value2'];
token2Values = ['t2value1','t2value2','t2value3'];
token3Values = ['t3value1','t3value2'];
token4Values = ['t4value1','t4value2','t4value3','t4value4'];

我的问题是,在给定模板和每个标记的可能值的情况下,如何生成字符串的每种可能的排列?

So, I have a template string with X amount of tokens in it. Hypothetically it could look like this:

template = "render=@layer0@-@layer1@-@layer2@-@layer3@-@layer4@"

The tokens, obviously, take the form of @tokenname@. In this hypothetical case it has five tokens. Each token has a different set of possible values. For example:

token0Values = ['t0value1'];
token1Values = ['t1value1','t1value2'];
token2Values = ['t2value1','t2value2','t2value3'];
token3Values = ['t3value1','t3value2'];
token4Values = ['t4value1','t4value2','t4value3','t4value4'];

My question then is, how do I generate every possible permutation of the string given the template and the possible values for each token?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南烟 2024-10-04 12:27:11

我将尝试一下 php/AS

令牌是可能值的二维数组

{
[0] =“苹果”、“香蕉”、“梨”
[1] =“胡萝卜”,“豌豆”
[2] =“土豆”、“芹菜”、“黄油”、“肉汁”

function getPermutations(tokens){

       var perms = array();

       //exit condition : there's only one token; 
       //total permutations = values array
       //so just return it

       if (tokens.length == 1)
           return tokens[0];

       //otherwise  
       //strip 1st element of the array as your "prefix"   

       prefices= tokens.shift();

       //get the permuations of the children

       childPermutations = getPermutations(tokens);

       //loop through the possible values, or "prefices"

       foreach (prefices as prefix){

            //concatenate to each of the child permutations

            foreach(childPermutations as perm)
                perms[]=prefix + perm;
       }

       //return the glob
       return perms;
}

可能有效,或者类似的东西

I'll take a stab at it in sorta php/AS

tokens is a two dimensional array of possible values

{
[0] = "apple","banana","pear"
[1] = "carrot","pea"
[2] = "potato", "celery", "butter","gravy"
}

function getPermutations(tokens){

       var perms = array();

       //exit condition : there's only one token; 
       //total permutations = values array
       //so just return it

       if (tokens.length == 1)
           return tokens[0];

       //otherwise  
       //strip 1st element of the array as your "prefix"   

       prefices= tokens.shift();

       //get the permuations of the children

       childPermutations = getPermutations(tokens);

       //loop through the possible values, or "prefices"

       foreach (prefices as prefix){

            //concatenate to each of the child permutations

            foreach(childPermutations as perm)
                perms[]=prefix + perm;
       }

       //return the glob
       return perms;
}

that might work, or something similar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文