- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 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
最佳答案
里面有代码:
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 片段: int[] testArray={10,20,30,40}; int i= 0; testArray[i++]= testArray[i++]+1; System.o
我想知道我是否可以通过某种方式在 C++ 中进行前/后函数调用。我有一个包含很多函数的包装器类,在每次调用包装器函数后,我应该调用另一个始终相同的函数。 所以我不想像这样对每个函数调用 postFun
我有一个像这样的头文件: #pragma once #include "gamestate.h" #include "ExitListener.h" class InitialGameState :
学习左值和右值。定义是任何可以是“地址”的东西都是左值,否则就是右值。 我检查了运算符的优先级,前缀和后缀增量都比“地址”运算符具有更高的优先级。 对于下面的两个例子,谁能解释一下为什么第一个“&++
在我的学习过程中,我遇到了前后迭代器,我想知道是否有办法让它们就地创建容器元素。从文档来看,容器似乎需要实现 push_back 函数才能与 back_iterator 一起使用。但是有没有一种方法可
我有两个关于 Java 中运算符优先级的类似问题。 第一个: int X = 10; System.out.println(X++ * ++X * X++); //it prints 1440 根据
请放轻松,不要对我开枪,因为我还是新手。 当我运行这段代码时,我完全糊涂了,终生无法弄清楚为什么: int y = 9; cout << "++y = " << ++y << "\n--y = " <
两种表达方式有区别吗: (*x)++ 和 ++(*x) 我可以看到这两个语句都替换了 *x 中 (*x+1) 的内容。但是它们之间有什么区别吗? 最佳答案 (*x)++ 计算为*x的值;作为副作用,*
我有一个如下所示的数据集: Date CONSUMER DISCR CONSUMER STAPLES ENERGY FINANCIALS HEALTH CARE
我希望检查名称字段中输入的前两个字符是否为字母 - 除此之外没有什么区别(空格、'、- 等都是公平的游戏)。这是我到目前为止所拥有的,但它不起作用。想法?谢谢! if (document.form01
我制作了一个简单的脚本,为像素和所有附近的像素着色为相同的颜色 Click foto
我需要编写一个循环,以下列格式输出从昨天算起的最近 30 天: 2014-02-02 2014-02-03 2014-02-04 ... 2014-03-04 我想我需要像这样使用循环: for ($
我正在做一些练习,但我对这个感到困惑: public static int f (int x, int y) { int b=y--; while (b>0) { if (x%2!=0
我需要一个 4 个字符的正则表达式。前 3 个字符必须是数字,最后 1 个字符必须是字母或数字。 我形成了这个,但它不起作用 ^([0-9]{3}+(([a-zA-Z]*)|([0-9]*)))?$
我需要编写一个循环,以下列格式输出从昨天算起的最近 30 天: 2014-02-02 2014-02-03 2014-02-04 ... 2014-03-04 我想我需要像这样使用循环: for ($
我有下面的程序,我试图找到前 1000 个素数的总和。在代码中,解决方案1和2有什么区别?为什么我不应该将 count 变量放在 if 条件之外?如果我把变量放在 if 之外,我显然没有得到我需要的答
这个问题在这里已经有了答案: Replace First N Occurrences in the String (7 个答案) 关闭 4 年前。 我有一个如下的字符串 const str = '_
我正在尝试测量以纳秒为单位的平均访问延迟,但在第一次迭代后我收到“段错误(核心转储)”。我错过了什么吗?我是否滥用了指针。这是导致错误的函数: #include #include #include
我有一个 SQL 问题 (MySQL)。我如何从下表创建一个新表(表名称:“well_master_prod_inj”)。 我需要按井名和日期聚合数据。我希望每个井名只有一行数据以及显示以下数据的列:
我是一名优秀的程序员,十分优秀!