arrays - 按组保留数组的前 N ​​个元素

标签 arrays algorithm language-agnostic srl

我在 Blaze Advisor 中使用专有语言(rule enginge)。我正在寻找一种算法,如何按特定属性形成的组只保留数组中的前 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

我想为 child 数组中的每个 parrentId 保留前三个元素,如 result 属性所示。在我的语言中,我可以完成所有基本操作——我可以使用 if/else、while、for、每个结构,并创建新变量。我可以对数组进行 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

最佳答案

辅助类: 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.

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

关于arrays - 按组保留数组的前 N ​​个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768025/

相关文章:

java - 有目的地避免 ArrayIndexOutOfBoundsException

c - 如何使用 C 中的指针来显示每个字母在数组中出现的次数?

c++ - 存储一组非重叠范围并严格查找值是否存在于任何一个范围中

algorithm - 我可以根据初始 key 和输出哈希来识别哈希算法吗?

java - 不能缩短具有大字符串数字的字符串数组

java - 在可重试操作中发送一次电子邮件(或任何消息)且仅发送一次

python - 使用 numpy 从两个对象向量生成对矩阵

php - 通过特定属性的值在数组中搜索对象的最有效方法

algorithm - 为给定的字符串生成所有唯一的子字符串

language-agnostic - 我应该更改代码以使其更具可测试性吗?