类似开关的问卷评分功能
我最近认真完成了 PHP/JS 编码,我有点失去了 R 能力。虽然这个问题可以在 PHP/JS 中轻松解决,但解决这个问题最有效的方法是什么:我必须对调查问卷进行评分,并且我有以下情况:
raw t
5 0
6 2
7-9 3
10-12 4
15-20 5
如果 x
等于或是在 raw
给定的范围内,应返回 t
中相应行中的值。当然,这可以通过 for
循环或 switch
来完成,但想象一下 raw
中非常长的值范围集。你会如何解决这个问题?
I'd done a serious PHP/JS coding recently, and I kind-of lost my R muscle. While this problem can be easily tackled within PHP/JS, what is the most efficient way of solving this one: I have to grade a questionnaire, and I have following scenario:
raw t
5 0
6 2
7-9 3
10-12 4
15-20 5
if x
equals to, or is within range given in raw
, value in according row in t
should be returned. Of course, this can be done with for
loop, or switch
, but just imagine very lengthy set of value ranges in raw
. How would you tackle this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们似乎遗漏了示例的一部分,因为没有提到“x”
We seem to be missing a part of the example because there in no mention of "x"
我会简单地使用类似于 Corbin 提到的索引方案,但由于他没有提供示例,这里有一个简单的示例:
I would simply use an indexing scheme kind of like what Corbin alluded to, but since he didn't provide an example, here's a simple one:
注意:与西蒙娜的回答非常相似,因为我开始输入这个内容。不过最后有一个注释。我给出的索引方法本质上是西蒙娜的答案。
某处一定会涉及到一个循环。
我要做的伪代码是这样的:
它避免了必须循环 rMin 和 rMax 之间的每个数字(这就是我假设你的意思),但如果没有某种索引,那就是最好的将会得到。
注意:如果您对此有大量调用,并且索引实际上值得您花时间,那么最简单的索引类型就是分数的哈希映射 -> 。 t 条目。
基本上,您会将示例数据解析为以下内容:
您需要仔细权衡构建索引是否比仅循环范围更耗时。
注:索引方式其实就是Simone所说的。
Note: very similar to Simone's answer as I started typing this a bit back. Has a note at the end though. The indexing approach I give is essentially Simone's answer.
There will have to be a loop involved somewhere.
The pseudo code of what I would do is something like:
It avoids having to loop over each number between rMin and rMax (which is what I'm assuming you meant), but without some kind of indexing, that is the best you're going to get.
Note: if you have a ton of calls to this, and indexing would actually be worth your while, the easiest type of indexing would just be a hash map of score -> t entries.
Basically you would parse your example data into something like:
You would need to carefully weigh if building the index would be more time consuming than just looping over the ranges.
Note: the indexing approach is actually what Simone said.