1. <i id='vfuBn'><tr id='vfuBn'><dt id='vfuBn'><q id='vfuBn'><span id='vfuBn'><b id='vfuBn'><form id='vfuBn'><ins id='vfuBn'></ins><ul id='vfuBn'></ul><sub id='vfuBn'></sub></form><legend id='vfuBn'></legend><bdo id='vfuBn'><pre id='vfuBn'><center id='vfuBn'></center></pre></bdo></b><th id='vfuBn'></th></span></q></dt></tr></i><div id='vfuBn'><tfoot id='vfuBn'></tfoot><dl id='vfuBn'><fieldset id='vfuBn'></fieldset></dl></div>

    1. <legend id='vfuBn'><style id='vfuBn'><dir id='vfuBn'><q id='vfuBn'></q></dir></style></legend>

      <small id='vfuBn'></small><noframes id='vfuBn'>

      • <bdo id='vfuBn'></bdo><ul id='vfuBn'></ul>
      <tfoot id='vfuBn'></tfoot>

      创建一个独特组合的数组

      Creating an Array of Unique Combinations(创建一个独特组合的数组)
      <tfoot id='TFwUz'></tfoot>
        <bdo id='TFwUz'></bdo><ul id='TFwUz'></ul>

        • <legend id='TFwUz'><style id='TFwUz'><dir id='TFwUz'><q id='TFwUz'></q></dir></style></legend>

            <tbody id='TFwUz'></tbody>
        • <small id='TFwUz'></small><noframes id='TFwUz'>

          <i id='TFwUz'><tr id='TFwUz'><dt id='TFwUz'><q id='TFwUz'><span id='TFwUz'><b id='TFwUz'><form id='TFwUz'><ins id='TFwUz'></ins><ul id='TFwUz'></ul><sub id='TFwUz'></sub></form><legend id='TFwUz'></legend><bdo id='TFwUz'><pre id='TFwUz'><center id='TFwUz'></center></pre></bdo></b><th id='TFwUz'></th></span></q></dt></tr></i><div id='TFwUz'><tfoot id='TFwUz'></tfoot><dl id='TFwUz'><fieldset id='TFwUz'></fieldset></dl></div>

                本文介绍了创建一个独特组合的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个组件数组:

                let components = ["a", "b", "c"];
                

                可以将这些组件组合起来制造产品;即,a";+b"=ab".

                It is possible to combine those components to make products; i.e., "a" + "b" = "ab".

                我有一份可能的产品目录:

                I have a catalog of possible products:

                let catalog = ["ab", "ac", "bc"];
                

                使用 lodash 之类的东西,我们可以创建一系列可能的产品.输出数组看起来像 [ab"、ac"、bc"].

                Using something like lodash, we can create an array of possible products. Output array would look like ["ab", "ac", "bc"].

                但问题在于,并非所有这些产品都可以构建,因为一旦使用了一个组件,它就不再可用于其他需要它的产品.

                But the problem is that not all of those products could be built because once the component for one is used, it is no longer available for use in other products that require it.

                我需要一个只显示可能结果的输出.当只有 3 个组件,并且每个目录产品需要 2 个时,显然不可能一次创建多个产品.因此,表示每个都是非此即彼的输出看起来像 [[ab"],[ac"],[bc"]].但是,如果您有 4 个组件,则可以一次创建多个产品.

                I need an output that only shows possible outcomes. When there are only 3 components, and every catalog product requires 2, obviously it's not possible to create more than one product at a time. So an output that would express that each is either-or would look like [["ab"],["ac"],["bc"]]. But if you have 4 components it is possible to create more than one product at a time.

                let components = ["a", "b", "c", "d"];
                

                包含这些组件的可能目录产品应类似于 [[ad"、cb"]、[ac"、bd"]、[ab"、cd"]]].

                Possible catalog products with these components should look like [["ad", "cb"], ["ac", "bd"], ["ab", "cd"]].

                我需要一些关于输出上述数组的函数的逻辑方面的帮助.

                I need some help with the logic for a function that outputs an array like the above.

                下面是一个函数示例,该函数使用提供的组件输出可能的目录产品.但它并没有达到上述要求.

                Below is an example of a function that outputs possible catalog products with the provided components. But it doesn't achieve the requirements stated above.

                let components = ["a", "b", "c", "e"];
                
                let catalog = [["a", "b"], ["a", "c"], ["b", "c"], ["c", "d"]];
                
                // Check if subset is included in superset (respecting duplicates).
                const isSubset = (subset, superset) => {
                  const subsetCount = _.countBy(subset)
                  const supersetCount = _.countBy(superset)
                
                  return _.every(subsetCount, (count, value) => supersetCount[value] >= count)
                }
                
                catalog.forEach(el => {
                if (isSubset(catalog, components) ==  true) console.log(el)
                });
                
                // Filter all catalog items, that could be build by components
                const matches = _.pickBy(catalog, (catalog, thing) => isSubset(catalog, components))
                
                console.log(matches);

                <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

                结果应该是一组可能的目录产品,其组件不与各自数组中的其他目录产品重叠/冲突.例如,如果我们有...

                The result should be an array of possible catalog products whose components do not overlap/conflict with other catalog products in their respective arrays. So for example, if we have...

                let components = ["a", "b", "c", "d", "e"];
                
                let catalog = ["ab", "ac", "ad", "ae", "bc", "bd", "be", "cd", "ce", "de"];
                

                ...输出应该是这样的:

                ... the output should be something like:

                // [ ["ab", "cd"], ["ab", "de"], ["ac", "bd"], ["ac", "be"], ["ad", "ce"], ["ad, "bc"], ["ae", "bc"], ["ae", "cd"] ]
                

                我可能在那里遗漏了一些,但关键是输出应该是一个数组,表示其内部数组的非此即彼关系.产品的组合顺序无关紧要.例如,cd";== dc".内部数组中的任何元素都不应共享一个组件.例如,我们不应该有 [ab", ac"].

                It's possible I've missed some there, but the point is that the output should be an array that expresses the either-or relationship of its inner arrays. The products' combination order doesn't matter. For example, "cd" == "dc". No element in the inner arrays should share a component. For example, we should not have ["ab", "ac"].

                推荐答案

                已编辑以适应您评论中的需求.此示例使用一个相当简单的递归函数来生成所有唯一组合,并带有限制最大组合大小的选项(用于生成只有大小为 2 的组合的 catalog).

                Edited to accommodate the needs in your comment. This example uses a fairly straight forward recursive function to generate all unique combinations with the option to limit the maximum combination size (used to generate the catalog with only combinations of size 2).

                possibleProducts 是通过将 catalog 数组无限制地传递给同一函数而生成的所有唯一组合的过滤数组.

                The possibleProducts are a filtered array of all the unique combinations generated by passing the catalog array to the same function with no limits.

                // generates unique combinations recursively
                function combineRec(array, size, limit = false) {
                  array = array.map(e => [e]);
                  // max size of combinations.
                  size = size || array.length;
                 
                  const acc =[];
                  
                  const spread = (s, arr) => arr.forEach((e, i, a) => {
                    let 
                      seed = [...s, ...e],
                      segment = a.slice(i + 1);
                    acc.push([...s, ...e]);
                    if (s.length < size - 1 && segment.length > 0) {
                      spread(seed, segment);
                    }
                  });
                
                  array.forEach((e, i, a) => (
                    spread(e, a.slice(i + 1))
                  ))
                
                  // if limit is true return only combinations of specified size.
                  return r = limit ? acc.filter(({ length }) => length === size) : acc;
                }
                
                // filters out combinations with duplicate elements
                const filterByElements = (array) => array.filter(arr =>
                  !arr.some((a, i) => a.some(e => arr.slice(i + 1).flat().includes(e)))
                );
                
                const components = ['a', 'b', 'c', 'd', 'e', 'f'];
                
                // generate catalog (return only combinations of 2)
                let catalog = combineRec(components, 2, true);
                
                // generate all possible catalog combinations (returns all)
                let catalogCombinations = combineRec(catalog);
                
                // filter to exlude duplicates and map combination arrays to strings.
                let possibleProducts = filterByElements(catalogCombinations)
                  .sort((a, b) => a.length - b.length)
                  .map(p => p.map(c => c.join('')));
                
                console.log('catalog: ', JSON.stringify(catalog));
                console.log('possibleProducts: ', JSON.stringify(possibleProducts));

                .as-console-wrapper { max-height: 100% !important; top: 0; }

                这篇关于创建一个独特组合的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)
                • <bdo id='4gP8h'></bdo><ul id='4gP8h'></ul>
                  <tfoot id='4gP8h'></tfoot>

                  • <legend id='4gP8h'><style id='4gP8h'><dir id='4gP8h'><q id='4gP8h'></q></dir></style></legend>
                    • <i id='4gP8h'><tr id='4gP8h'><dt id='4gP8h'><q id='4gP8h'><span id='4gP8h'><b id='4gP8h'><form id='4gP8h'><ins id='4gP8h'></ins><ul id='4gP8h'></ul><sub id='4gP8h'></sub></form><legend id='4gP8h'></legend><bdo id='4gP8h'><pre id='4gP8h'><center id='4gP8h'></center></pre></bdo></b><th id='4gP8h'></th></span></q></dt></tr></i><div id='4gP8h'><tfoot id='4gP8h'></tfoot><dl id='4gP8h'><fieldset id='4gP8h'></fieldset></dl></div>

                      <small id='4gP8h'></small><noframes id='4gP8h'>

                          <tbody id='4gP8h'></tbody>