按组保留数组的前N个元素

时间:2015-08-02 02:25:16

标签: arrays algorithm language-agnostic srl blaze-advisor

我在Blaze Advisor中使用专有语言(规则引擎)。我正在寻找一种算法,如何通过特定属性形成的组仅保留数组中的前N项。例如,有两个数组:

parrent[0].id = 1
parrent[1].id = 2

第二阵列:

child[0].parrentId = 1
child[0].result = 3.0
child[1].parrentId = 1
child[1].result = 2.0
child[2].parrentId = 1
child[2].result = 4.0
child[3].parrentId = 1
child[3].result = 6.0
child[4].parrentId = 1
child[4].result = -1.0
child[5].parrentId = 2
child[5].result = 1.0
child[6].parrentId = 2
child[6].result = 16.0
child[7].parrentId = 2
child[7].result = 2.0
child[8].parrentId = 2
child[8].result = -10.0
child[9].parrentId = 2
child[9].result = 5.0

我想仅保留parrentId数组中每个child的前三个元素,如result属性所示。在我的语言中,我可以执行所有基本操作 - 我可以使用if / else,while,for,for each construct,并创建新变量。我可以对数组asc / desc进行排序并获取已排序元素的索引。我可以删除数组的元素。

对于我的数据,我需要以下结果:

child[0].parrentId = 1
child[0].result = 3.0
child[1].parrentId = 1
child[2].result = 4.0
child[3].parrentId = 1
child[3].result = 6.0
child[6].parrentId = 2
child[6].result = 16.0
child[7].parrentId = 2
child[7].result = 2.0
child[9].parrentId = 2
child[9].result = 5.0

2 个答案:

答案 0 :(得分:1)

使用辅助类: enter image description here

功能: enter image description here

那里有代码:

len is an integer initially top.children.count - 1;
idx is an integer initially len;
while idx > atIdx do {
    top.children[idx] = top.children[idx-1];
    decrement idx;
}
top.children[atIdx] = child;

此代码可以满足您的要求:

child is an fixed array of 10 Child;

counter is an integer initially 0;
while counter < 10 do { child[counter] = a Child; increment counter }

child[0].parrentId = 1;
child[0].result = 3.0;
child[1].parrentId = 1;
child[1].result = 2.0;
child[2].parrentId = 1;
child[2].result = 4.0;
child[3].parrentId = 1;
child[3].result = 6.0;
child[4].parrentId = 1;
child[4].result = -1.0;
child[5].parrentId = 2;
child[5].result = 1.0;
child[6].parrentId = 2;
child[6].result = 16.0;
child[7].parrentId = 2;
child[7].result = 2.0;
child[8].parrentId = 2;
child[8].result = -10.0;
child[9].parrentId = 2;
child[9].result = 5.0;

groups is an array of real;

topN is an integer initially 4;

//Init the hashmap of [group] -> [array of 'N' top Child]
top3fromGroup is an association from real to TopChildren;
for each Child in child do if not groups.contains(it.parrentId) then { 
    top3fromGroup[it.parrentId] = a TopChildren;
    initCounter is an integer initially 0;
    while initCounter < topN do {
        top3fromGroup[it.parrentId].children[initCounter] = a Child initially { it.result = Double.MIN_VALUE;} 
        increment initCounter;
    }
    groups.append(it.parrentId);
}

//Filling the groups at the hashmap with the Child elements ordered inside its groups
for each real in groups do { 
    group is a real initially it;
    for each Child in child do {
        localChild is some Child initially it;
        if it.parrentId = group then {
            top is some TopChildren initially top3fromGroup[group]; 
            topValuesIdx is an integer initially 0;
            while topValuesIdx < top.children.count do {
                topChild is some Child initially top.children[topValuesIdx];
                if localChild.result > topChild.result then { 
                    insertAt(topValuesIdx, localChild, top);
                    topValuesIdx = top.children.count;
                } 
                increment topValuesIdx;
            }
        }
    }
}

//Printing the hashmap
for each real in groups do {
    group is a real initially it;
    print("Group: " group);
    childIdx is an integer initially 0;
    for each Child in top3fromGroup[it].children do {
        print("\tchild["childIdx"].parrentId = " it.parrentId); 
        print("\tchild["childIdx"].result = " it.result);
        increment childIdx;
    }
}

Eclipse / Blaze控制台上的输出将是:

Group: 1.0
    child[0].parrentId = 1.0
    child[0].result = 6.0
    child[1].parrentId = 1.0
    child[1].result = 4.0
    child[2].parrentId = 1.0
    child[2].result = 3.0
    child[3].parrentId = 1.0
    child[3].result = 2.0
Group: 2.0
    child[0].parrentId = 2.0
    child[0].result = 16.0
    child[1].parrentId = 2.0
    child[1].result = 5.0
    child[2].parrentId = 2.0
    child[2].result = 2.0
    child[3].parrentId = 2.0
    child[3].result = 1.0

Execution complete.

我知道这是一个非常简单的解决方案而不是最佳解决方案。

答案 1 :(得分:0)

使用selection algorithm可以保持数组的前N个元素。将数据分组的事实不应该给问题增加太多的复杂性,只需忽略不在组中的元素。

例如,如果您使用partial sorting,则可以对组中的元素进行部分排序。你不会像标准的局部排序一样提前知道第N个条目的索引,但是你可以修改外部循环来遍历所有条目(而不仅仅是前N个),但要跟踪你有多少已经部分排序并在完成后中断。

顺便说一句,既然你在问题中说明你可以用你的专有语言对数组进行排序,那么如果效率不是太低,你只需对整个数组进行排序然后遍历整个数组,直到你找到顶层所有感兴趣的组的N个项目。这是蛮力但如果你没有性能问题那么它可能是最简单的答案。