串数组的数组的排列 [英] Permutations of an array of arrays of strings

查看:109
本文介绍了串数组的数组的排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根本不能完成我的头围绕如何解决这个问题,并在谷歌彻底搜查没有结果后,我转交给你一个解决的希望。

由于下面的示例数组:

 阵列(
    '类型'=>阵列(
        Toppe',
        Bukser_og_Jeans
    )
    '大小'=>阵列(
        '超小',
        '小'
    )
    '颜色'=>阵列(
        '竿'
    )

(注:这仅仅是一个示例;实际真实生活状况可能具有更少/多个组和/或每个组元素)

我怎么会去得到下面的结果结束了?

  Toppe,Extra_small,罗德
Toppe,小,棒
Bukser_og_Jeans,Extra_small,罗德
Bukser_og_Jeans,小,棒

这是一个产品的搜索和API只允许每个每次查询的类型,大小和颜色的群体之一'精'的价值,但我的任务,需要查询和聚合多个API查询的结果。

我的思维的,我需要某种形式的递归函数来做到这一点,但我一直无法甚至产生任何code来接近我​​预期的结果。

所有我已经能够找到谷歌即将封permuations甚至字符串,但那里的人们需要例如红,蓝,绿,蓝,红,绿,绿,红,蓝等,这是明显的,而不是我要找的。

我希望这里有人明白我想要做的,有如何做到这一点的想法。

编辑:发表@ikegami,转化为PHP解决办法:

  $ ITER = 0;
而(1){
    $ NUM = $ ITER ++;
    $挑=阵列();    的foreach($细化为$ refineGroup => $ groupValues​​){
        $ R = $ NUM%计数($ groupValues​​);
        $ NUM =($ NUM - $ R)/ COUNT($ groupValues​​);
        $挑[] = $ groupValues​​ [$ R]。
    }    如果($ num个大于0){
        打破;
    }    打印加入('',$新秀)\\ N。
}


解决方案

那么,在Perl中,我会使用

 使用算法::循环QW(NestedLoops);我改进%=(
   类型=> [
      Toppe',
      Bukser_og_Jeans',
   ]
   大小=> [
      '超小',
      '小',
   ]
   颜色=> [
      '竿',
   ]
);NestedLoops(
    [值(%精炼)]
    子{[加入('',@_)},
);

您可以端口 NestedLoops 到PHP。 算法::循环

也许这将是更容易的端口:

 我的%修正=(
   类型=> [
      Toppe',
      Bukser_og_Jeans',
   ]
   大小=> [
      '超小',
      '小',
   ]
   颜色=> [
      '竿',
   ]
);我@groups =值(%改进);
我的$ ITER = 0;
而(1){
   我的$ NUM = $ ITER ++;   我@pick;
   我的$组(@groups){
      我的$ R = $ NUM%@ $组;
      $ NUM =($ NUM - $ R)/ @ $组;
      推@pick,$组 - > $ R]。
   }   最后,如​​果$ NUM> 0;   说加入('',@pick);
}

I simply cannot wrap my head around how to solve this problem and after a thorough search on Google with no results, I turn to you with hopes of a solution.

Given the sample array below:

array(
    'Type' => array(
        'Toppe',
        'Bukser_og_Jeans'
    ),
    'Size' => array(
        'Extra_small',
        'Small'
    ),
    'Colour' => array(
        'Rod'
    )
)

(Note: This is merely a sample; the actual real life situation might have less/more groups and/or elements per group)

How would I go about ending up with the following result?

Toppe,Extra_small,Rod
Toppe,Small,Rod
Bukser_og_Jeans,Extra_small,Rod
Bukser_og_Jeans,Small,Rod

This is a product search and the API only allows ONE 'refinement' value from each of the Type, Size and Colour groups per query but my assignment requires to query and aggregate the results of multiple API queries.

I'm thinking that I need some kind of recursive function to do it, but I have been unable to even produce any code that comes close to my expected result.

All I've been able to find on Google is about permuations of letters or even strings, but where people need e.g. "Red,Blue,Green", "Blue,Red,Green", "Green,Red,Blue", etc., which is, clearly, not what I'm looking for.

I hope someone here understands what I want to do and has an idea of how to do it.

EDIT: The solution as posted by @ikegami, converted to PHP:

$iter = 0;
while (1) {
    $num = $iter++;
    $pick = array();

    foreach ($refinements as $refineGroup => $groupValues) {
        $r = $num % count($groupValues);
        $num = ($num - $r) / count($groupValues);
        $pick[] = $groupValues[$r];
    }

    if ($num > 0) {
        break;
    }

    print join(', ', $pick)."\n";
}

解决方案

Well, in Perl, I'd use

use Algorithm::Loop qw( NestedLoops );

my %refinements = (
   Type => [
      'Toppe',
      'Bukser_og_Jeans',
   ],
   Size => [
      'Extra_small',
      'Small',
   ],
   Colour => [
      'Rod',
   ],
);

NestedLoops(
    [ values(%refinements) ],
    sub { say join(', ', @_) },
);

You could port NestedLoops over to PHP. Algorithm::Loops

Maybe this will be easier to port:

my %refinements = (
   Type => [
      'Toppe',
      'Bukser_og_Jeans',
   ],
   Size => [
      'Extra_small',
      'Small',
   ],
   Colour => [
      'Rod',
   ],
);

my @groups = values(%refinements);
my $iter = 0;
while (1) {
   my $num = $iter++;

   my @pick;
   for my $group (@groups) {
      my $r = $num % @$group;
      $num = ( $num - $r ) / @$group;
      push @pick, $group->[$r];
   }

   last if $num > 0;

   say join(', ', @pick);
}

这篇关于串数组的数组的排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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