查找选项的组合在一个循环 [英] Find all combinations of options in a loop

查看:133
本文介绍了查找选项的组合在一个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找通过一堆选项,以循环的最佳方式,以确保我打了所有可用的选项。

I'm looking for the best way to loop through a bunch of options to make sure that I hit all available options.

我创建了一个功能,它允许客户端构建基本上都是铺在彼此顶部其他图像的图像。这些其他图像被分成不同的组。它们对图像的侧连杆,他们可以点击通过所有的不同的图像滚动到查看它们。

I've created a feature that allows a client to build images that are basically other images layered on top of each other. These other images are split up into different groups. They have links on the side of the image that they can click to scroll through all the different images to view them.

现在我正在做一个自动化的过程,要运行,当用户点击其中一个链接,改变图像的功能。我需要确保,在此过程中,不同的图像的每一个可能的组合被击中。

Now I'm making an automated process that is going to run the function that changes the image when a user clicks one of the links. I need to make sure that every possible combo of the different images is hit during this process.

说,有3种不同的帽子,4种不同的衬衫,5种不同的裤子和6种不同双鞋。我可以重新present这与为每组期权数量的数组。目前的数组是 [3,4,5,6]

Say there are 3 different hats, 4 different shirts, 5 different pairs of pants and 6 different pairs of shoes. I can represent this as an array with the number of options for each group. The current array is [3, 4, 5, 6].

什么是通过这个数组进行循环的最佳方式,以确保所有可能的选择将显示?

What is the best way to loop through this array to make sure that all possible options will be shown?

推荐答案

您想笛卡尔乘积所有的阵列

我有一个页面讨论这个,包括JavaScript实现,我的网站上:结果
http://phrogz.net/lazy-cartesian-product

I have a page discussing this, including implementations in JavaScript, on my site:
http://phrogz.net/lazy-cartesian-product

例如,通过他们都在前进为了迭代很快,你可以​​使用:

For example, to iterate through them all quickly in "forward" order, you can use:

hats   = ['fez','fedora']
shirts = ['t-shirt','long']
pants  = ['shorts','jeans']
shoes  = ['sneaker','loafer']

lazyProduct( [hats,shirts,pants,shoes], function(hat,shirt,pant,shoe){
  // Your function is yielded unique combinations of values from the arrays
  console.log(hat,shirt,pant,shoe);
});

function lazyProduct(sets,f,context){
  if (!context) context=this;
  var p=[],max=sets.length-1,lens=[];
  for (var i=sets.length;i--;) lens[i]=sets[i].length;
  function dive(d){
    var a=sets[d], len=lens[d];
    if (d==max) for (var i=0;i<len;++i) p[d]=a[i], f.apply(context,p);
    else        for (var i=0;i<len;++i) p[d]=a[i], dive(d+1);
    p.pop();
  }
  dive(0);
}

输出:

fez t-shirt shorts sneaker
fez t-shirt shorts loafer
fez t-shirt jeans sneaker
fez t-shirt jeans loafer
fez long shorts sneaker
fez long shorts loafer
fez long jeans sneaker
fez long jeans loafer
fedora t-shirt shorts sneaker
fedora t-shirt shorts loafer
fedora t-shirt jeans sneaker
fedora t-shirt jeans loafer
fedora long shorts sneaker
fedora long shorts loafer
fedora long jeans sneaker
fedora long jeans loafer
fez t-shirt shorts sneaker
fez t-shirt shorts loafer

这是等同于结果:

hats.forEach(function(hat){
  shirts.forEach(function(shirt){
    pants.forEach(function(pant){
      shoes.forEach(function(shoe){
        console.log(hat,shirt,pant,shoe);
      });
    });
  });
});

或(旧版本浏览器):

for (var h=0;h<hats.length;h++){
  var hat = hats[h];
  for (var s=0;s<shirts.length;s++){
    var shirt = shirts[s];
    for (var p=0;p<pants.length;p++){
      var pant = pants[p];
      for (var e=0;e<shoes.length;e++){
        var shoe = shoes[e];
        console.log(hat,shirt,pant,shoe);        
      }
    }
  }
}

...但它支持在运行时定义的阵列的任意数量。 (如果你正在使用从我的网站的第一个懒的实施,可以随意挑出来的项目,迭代反向,否则容易反复停止在任何一点。)

…but it supports an arbitrary number of arrays defined at runtime. (And if you're using the first "lazy" implementation from my site, you can pick out items at random, iterate in reverse, or easily stop iteration at any point.)

这篇关于查找选项的组合在一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆