在循环中查找所有选项组合 [英] Find all combinations of options in a loop

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

问题描述

我正在寻找循环浏览一堆选项的最佳方式,以确保我选择了所有可用的选项.

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 种不同的鞋子.我可以将其表示为一个数组,其中包含每个组的选项数.当前数组为[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?

推荐答案

您需要 笛卡尔积 你所有的数组.

You want the Cartesian Product of all your arrays.

我的网站上有一个讨论此问题的页面,包括 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天全站免登陆