doubaoxue5788 2015-07-23 22:58
浏览 40

列出所有选项字符串的组合,同时遵循提供的数组的顺序和特定选项的规则 - php

I am working on a project where a company sells a multitude of valves with a vast array of variations and need to build a site map with links to every valve data as the current navigation does not use conventional links but codes built via a user interacting with the many options on the page. I have been able to generate everything up until the options which begin to get very tricky because of the vast array of combinations of these options there are and the rules that they follow.

I am attempting to build every option string combinations with the rules you can find below.

Given the array of option codes (at the end of the post) I must generate a list of all possible combinations but they must follow the order that they are presented and follow the rules given by the row_mod. For example when the option_code of O is in the combinations the option_code of ON and OP are not allowed to be in the combination.

The only row_mod info that needs to be followed currently when building these strings is the "naWithOp##" as that refers to another option that is not allowed to be with it.

Some of the possible combinations that would need to come out of the script would be:

  1. OTKZ
  2. OTK
  3. OT
  4. OTOSZ
  5. OTOS
  6. O
  7. OL
  8. OLKZ
  9. OLK

I know this isn't even close to the exhaustive list of how many are actually possible but I can't figure out how to generate all the possible combinations.


Array
(
    [0] => Array
        (
            [option_code] => O
            [row_mod] => allowAll naWithOpON naWithOpOP
        )

    [1] => Array
        (
            [option_code] => ON
            [row_mod] => allowAll naWithOpO naWithOpOP
        )

    [2] => Array
        (
            [option_code] => OP
            [row_mod] => allowAll naWithOpO naWithOpON
        )

    [3] => Array
        (
            [option_code] => T
            [row_mod] => allowAll naWithOpL
        )

    [4] => Array
        (
            [option_code] => L
            [row_mod] => allowAll naWithOpT naWithCoilX
        )

    [5] => Array
        (
            [option_code] => K
            [row_mod] => allow2 allow3 allow3P naWithOpOS
        )

    [6] => Array
        (
            [option_code] => OS
            [row_mod] => allow2 allow3 allow3P naWithOpK
        )

    [7] => Array
        (
            [option_code] => Z
            [row_mod] => allowAll naWithPreB
        )

)

Basically I must start on the first option code and iterate down the list starting with the first given option 'O' then go to the next option 'ON' and check whether its valid and if it is then add it to the current combination. And so on and so forth building allowed combinations and storing them to an array.

User Interface example: I created a jsfiddle that is interactive in the way that the options work together. Found here

I'm not sure what approach I should be taking but I have tried using recursive algorithms to no avail.

  • 写回答

1条回答 默认 最新

  • du77887 2015-07-23 23:17
    关注

    You have eight different options, which appear to be usable either zero or one times. Thus "not used" makes this nine. So you have

    • 9 possibilities for the first option,
    • 8 possibilities for the second (since the first cannot be reused),
    • 7 possibilities for the third (since the first two cannot be reused)
    • ...

    Your basic number of options then, not counting the extra rules for option compatibility, is 9!, or 362880 combinations.

    You could cycle through all possible combinations, and then write a function to test whether the combination is valid, given the rules for each type. Only list items for which the function returns true, and reject the others. It might be a bit slow, but it'll work.

    To generate these combinations, I would visualise the sequence of options as a 9-digit number in base 9 (i.e. you have 9 things and they can each take one of 9 values, subject to limitations I note below). I think I am right in saying this is 99 (38742048910) different combinations, if there was no rule to disallow an option from being used twice.

    Thus, just loop through these numbers in base 9, reject any numbers that have any duplicate digits, and of the ones that get through, convert it to an options string, and reject any that don't pass the per-options rule function. The list of items you are left with will be the full set of permitted combinations.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号