获取与组相关的所有值的唯一组合

问题描述 投票:2回答:1

我有一组属性值:

例:

[
  {
    memberAttribute: { attributeName: 'a' },
    value: '1'  
  },
  {
    memberAttribute: { attributeName: 'a' },
    value: '2'  
  },
  {
    memberAttribute: { attributeName: 'b' },
    value: '1'  
  },
  {
    memberAttribute: { attributeName: 'b' },
    value: '2'  
  }
]

现在我想获得给定成员属性的所有唯一组合。

因此,如果我想要成员属性'a'和'b'的唯一组合,结果将是:

[
   {
     memberAttribute: { attributeName: 'a' },
     value: '1'  
   },
   {
     memberAttribute: { attributeName: 'b' },
     value: '1'  
   }
],
[
   {
     memberAttribute: { attributeName: 'a' },
     value: '1'  
   },
   {
     memberAttribute: { attributeName: 'b' },
     value: '2'  
   }
],
[
   {
     memberAttribute: { attributeName: 'a' },
     value: '2'  
   },
   {
     memberAttribute: { attributeName: 'b' },
     value: '1'  
   }
],
[
   {
     memberAttribute: { attributeName: 'a' },
     value: '2'  
   },
   {
     memberAttribute: { attributeName: 'b' },
     value: '2'  
   }
]

我需要能够提供n个输入成员属性,但只能获得2个输入属性的所需结果。

当前可怕的解决方案:

export const getAttributeCombinations = (
  attributes: MemberAttributeValue[]
) => {
  // TODO - This algorithm only supports 2 attribute types
  // It should support any number of attribute types

  const combinations = new Array<Array<MemberAttributeValue>>();

  for (const attribute of attributes) {
    let unusedAttributes = allExcept(attribute, attributes);

    const permutate = () => {
      const combination = [attribute];
      const toRemove = new Array<Number>();

      for (let i = 0; i < unusedAttributes.length; i++) {
        const unusedAttribute = unusedAttributes[i];

        if (!attributeTypeAlreadyExists(unusedAttribute, combination)) {
          toRemove.push(i);
          combination.push(unusedAttribute);
        }
      }

      for (const index of toRemove) {
        unusedAttributes = remove(index, 1, unusedAttributes);
      }

      combinations.push(combination);
    };

    permutate();

    while (unusedAttributes.length > 0) {
      permutate();
    }
  }

  const sortedCombinations = map(sortByAttributeName, combinations);
  return uniqByCombination(sortedCombinations);
};

失败的笑话测试示例:

it('given 3 attribute types should return 12 combinations', () => {
    const inclusionAttributes: MemberAttributeValue[] = [
      {
        memberAttribute: {
          attributeName: 'gender',
          aliases: [],
          contentType: ContentType.String,
          type: AttributeType.Mandatory
        },
        value: 'Male'
      },
      {
        memberAttribute: {
          attributeName: 'gender',
          aliases: [],
          contentType: ContentType.String,
          type: AttributeType.Mandatory
        },
        value: 'Female'
      },
      {
        memberAttribute: {
          attributeName: 'age band',
          aliases: [],
          contentType: ContentType.String,
          type: AttributeType.Mandatory
        },
        value: '0-50'
      },
      {
        memberAttribute: {
          attributeName: 'age band',
          aliases: [],
          contentType: ContentType.String,
          type: AttributeType.Mandatory
        },
        value: '51+'
      },
      {
        memberAttribute: {
          attributeName: 'likes',
          aliases: [],
          contentType: ContentType.String,
          type: AttributeType.Mandatory
        },
        value: 'cats'
      },
      {
        memberAttribute: {
          attributeName: 'likes',
          aliases: [],
          contentType: ContentType.String,
          type: AttributeType.Mandatory
        },
        value: 'dogs'
      },
      {
        memberAttribute: {
          attributeName: 'likes',
          aliases: [],
          contentType: ContentType.String,
          type: AttributeType.Mandatory
        },
        value: 'goats'
      }
    ];

    const combinations = getAttributeCombinations(inclusionAttributes);

    expect(combinations.length).toBe(12);

    for (const combination of combinations) {
      expect(combination.length).toBe(3);
    }
  });
javascript typescript combinations permutation ramda.js
1个回答
3
投票

这在Ramda中相对简单,只是Ramda的xprod函数只在两个列表上起作用。如果它在列表列表上工作,我们可以在几个步骤中完成。但是写我们自己很容易:

const xproduct = reduce(pipe(xprod, map(unnest)), [[]])

const transform = pipe(
  groupBy(path(['memberAttribute', 'attributeName'])),
  values,
  xproduct
)

const inclusionAttributes = [
  {"memberAttribute": {"attributeName": "gender"}, "value": "Male"}, 
  {"memberAttribute": {"attributeName": "gender"}, "value": "Female"}, 
  {"memberAttribute": {"attributeName": "age band"}, "value": "0-50"}, 
  {"memberAttribute": {"attributeName": "age band"}, "value": "51+"}, 
  {"memberAttribute": {"attributeName": "likes"}, "value": "cats"}, 
  {"memberAttribute": {"attributeName": "likes"}, "value": "dogs"}, 
  {"memberAttribute": {"attributeName": "likes"}, "value": "goats"}
]

console.log(transform(inclusionAttributes))
//=> Male/0-50/cats, Male/0-50/dogs, Male/0-50/goats, Male/51+/cats,...

你可以在Ramda REPL上看到这个。

© www.soinside.com 2019 - 2024. All rights reserved.