我正在开发一个应用程序来存储客户和帐户的关系数据(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-一级
编辑:这不是组合的完整列表,但是算法应该生成所有这些。
发布于 2019-10-06 22:46:45
这里有两个问题要解决:
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);}
对于两个客户,结果是:
[
"C1-Sole",
"C1-Primary C2-NonPrimary",
"C2-Sole",
"C2-Primary C1-NonPrimary"
]
// 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);
发布于 2019-10-06 13:59:09
如果您的帐户和客户之间的关系有限:
1)创建dict:
dMapCustomer = {<nCustomerId>: [<nAccountId1>, <nAccountId2>]}2)每个客户创建所有可能的配对,这是公正的。
lCustomerPairs = [(nCustomerId, nAccountId1), (nCustomerId, nAccountId2), ...]3)将步骤2中的所有对连接起来。
l = []
for nCustomer in lCustomer:
l += lCustomerPairs如果任何帐户可以与任何客户链接,那么只需:
lAccounts = [1,2,3]
lCustomers = [4,5,6]
list(product(lCustomers, lCustomers)) # all possible pairs of account and customer函数产品从两个列表中生成所有可能的对:
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)https://stackoverflow.com/questions/58253966
复制相似问题