首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生成所有可能的客户和帐户组合

生成所有可能的客户和帐户组合
EN

Stack Overflow用户
提问于 2019-10-06 03:22:37
回答 2查看 109关注 0票数 2

我正在开发一个应用程序来存储客户和帐户的关系数据(w.r.t银行域)。通常,在银行,客户可以有一个帐户,这是一个单一的帐户,或有一个与另一个客户的共同帐户。

客户C1有一个单独的帐户A1。客户C1和C2有一个共同账户JA1,其中C1是主要持有者,C2是非主要持有者。

我正在寻找一个算法,将产生所有可能的组合关系,为给定数量的客户和帐户。

例如:如果客户数量= 2,帐户数=2,那么算法应该生成以下条目。

组合1:C1-A1-一级C1-A2-一级C2-A1-非初级C2-A2-非一级

组合2:C1-A1-一级C1-A2-非初级C2-A1-非初级C2-A2-一级

组合3:C1-A1-非一级C1-A2-一级C2-A1-一级C2-A2-非一级

组合4:C1-A1-非一级C1-A2-非初级C2-A1-一级C2-A2-一级

组合5:C1-A1-唯一C1-A2-一级C2-A2-非一级

组合#6: C1-A1-唯一C1-A2-非初级C2-A2-一级

组合7:C1-A2-唯一C1-A1-一级C2-A1-非初级

组合8:C1-A2-唯一C1-A1-非初级C2-A1-一级

编辑:这不是组合的完整列表,但是算法应该生成所有这些。

EN

回答 2

Stack Overflow用户

发布于 2019-10-06 22:46:45

这里有两个问题要解决:

  1. 为N个客户获取所有可能的帐户类型。你可以这样做:

const allAccounts = [];for (设i= 1;i <= customersNumber;i++) { allAccounts.push(C${i}-Sole);for (j= 1;j <= customersNumber;j++) { if (i === j)继续;allAccounts.push(C${i}-Primary C${j}-NonPrimary);}

对于两个客户,结果是:

代码语言:javascript
复制
[
  "C1-Sole",
  "C1-Primary C2-NonPrimary",
  "C2-Sole",
  "C2-Primary C1-NonPrimary"
]

  1. 从这个数组中获得长度r的所有可能组合。我们希望排除两种类型的组合-- here:
  • ones有两个或两个以上的单一帐户,用于相同的customer.
  • ones,这些帐户没有连接(如果我正确的话,没有普通客户),

代码语言:javascript
复制
// checks if two accounts are connected
function connected(customers1, customers2) {
    return customers1.filter(cu => customers2.includes(cu)).length > 0;
}

// checks if acc1 and acc2 are the same Sole account 
function sameSoleAccount(acc1, acc2) {
    return acc1.type === 'Sole' && acc1 === acc2;
}

function printAccount(i, a) {
    const c = a.customers;
    return a.type === 'Sole' ? `${c[0]}-A${i}-Sole` : `${c[0]}-A${i}-Primary ${c[1]}-A${i}-NonPrimary`;
}

function combination(chosen, arr, index, r) {
    if (index === r) {
        const combination = chosen.map((c, i) => printAccount(i + 1, arr[c])).join(', ');
        console.log(combination);
        return;
    }
    for (let i = 0; i < arr.length; i++) {
        if (chosen.length === 0 ||
            chosen.some(ch => !sameSoleAccount(arr[ch], arr[i])
                && connected(arr[ch].customers, arr[i].customers))) {
            const copy = chosen.slice();
            copy[index] = i;
            combination(copy, arr, index + 1, r);
        }
    }
}

function allPossibleCombinations(accountsNumber, customersNumber) {
    const allAccounts = [];
    for (let i = 1; i <= customersNumber; i++) {
        allAccounts.push({customers: [`C${i}`], type: 'Sole'});
        for (let j = 1; j <= customersNumber; j++) {
            if (i === j) continue;
            allAccounts.push({customers: [`C${i}`, `C${j}`], type: 'Joint'});
        }
    }
    console.log(`All possible combinations for ${customersNumber} customers and ${accountsNumber} accounts: `);
    combination([], allAccounts, 0, accountsNumber);
}

allPossibleCombinations(2, 2);

票数 1
EN

Stack Overflow用户

发布于 2019-10-06 13:59:09

如果您的帐户和客户之间的关系有限:

1)创建dict:

代码语言:javascript
复制
dMapCustomer = {<nCustomerId>: [<nAccountId1>, <nAccountId2>]}

2)每个客户创建所有可能的配对,这是公正的。

代码语言:javascript
复制
lCustomerPairs = [(nCustomerId, nAccountId1), (nCustomerId, nAccountId2), ...]

3)将步骤2中的所有对连接起来。

代码语言:javascript
复制
l = []
for nCustomer in lCustomer:
    l += lCustomerPairs

如果任何帐户可以与任何客户链接,那么只需:

代码语言:javascript
复制
lAccounts = [1,2,3]
lCustomers = [4,5,6]
list(product(lCustomers, lCustomers)) # all possible pairs of account and customer

函数产品从两个列表中生成所有可能的对:

代码语言:javascript
复制
def product(l1, l2): 
    pools = [tuple(pool) for pool in [l1, l2]] 
    result = [[]] 
    for pool in pools: 
        result = [x+[y] for x in result for y in pool] 
    for prod in result: 
        yield tuple(prod)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58253966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档