在数字集合中查找最接近的匹配
所以今天有人问我在集合中查找结束匹配的最佳方法是什么。
例如,您有一个如下所示的数组:
1, 3, 8, 10, 13, ...
哪个数字最接近 4?
集合是数字的、无序的并且可以是任何东西。 与要匹配的号码相同。
让我们看看我们可以从选择的各种语言中得到什么。
So I got asked today what was the best way to find the closes match within a collection.
For example, you've got an array like this:
1, 3, 8, 10, 13, ...
What number is closest to 4?
Collection is numerical, unordered and can be anything. Same with the number to match.
Lets see what we can come up with, from the various languages of choice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
J 中的 11 个字节:
示例:
我对外行人的细分:
11 bytes in J:
Examples:
my breakdown for the layman:
更短的 Python:41 个字符
Shorter Python: 41 chars
我在Python中的尝试:
My attempt in python:
Groovy 28B
Groovy 28B
一些 C# Linq 的...有太多方法可以做到这一点!
如果您使用列表,则还有更多方法,因为普通 ol 数组没有 .Sort()
Some C# Linq ones... too many ways to do this!
Even more ways if you use a list instead, since plain ol arrays have no .Sort()
假设这些值从一个名为 T 的表开始,其中有一个名为 N 的列,并且我们正在查找值 4,那么在 Oracle SQL 中它需要 59 个字符:
我使用 select * 来减少空格要求。
Assuming that the values start in a table called T with a column called N, and we are looking for the value 4 then in Oracle SQL it takes 59 characters:
I've used select * to reduce the whitespace requirements.
因为我实际上需要这样做,这是我的 PHP
Because I actually needed to do this, here is my PHP
PostgreSQL:
如果两条记录共享相同的“abs(4 - id)”值,则输出将是不确定的,并且可能不是常量。 为了解决这个问题,我建议类似未经测试的猜测:
此解决方案提供了 O(N log N) 数量级的性能,其中 O(log N) 是可能的,例如: https://stackoverflow.com/a/8900318/1153319
PostgreSQL:
In the case where two records share the same value for "abs(4 - id)" the output would be in-determinant and perhaps not a constant. To fix that I suggest something like the untested guess:
This solution provides performance on the order of O(N log N), where O(log N) is possible for example: https://stackoverflow.com/a/8900318/1153319
像 Python 一样,Ruby 有一个用于 Enumerable 的 min 方法,因此您不需要进行排序。
Ruby like Python has a min method for Enumerable so you don't need to do a sort.
语言:C,字符数:79
签名:
用法:
Language: C, Char count: 79
Signature:
Usage:
Scala(62 个字符),基于 J 和 Ruby 解决方案的思想:
用法:
Scala (62 chars), based on the idea of the J and Ruby solutions:
Usage:
PostgreSQL:
这是由 RhodiumToad 在 FreeNode 上指出的,其性能约为 O(log N)。,比此处的其他 PostgreSQL 答案要好得多。
两个条件都应该是“或等于”,以便更好地处理 id 存在的情况。 这也可以处理两个记录共享相同的“abs(4 - id)”值的情况,然后其他 PostgreSQL 在这里回答。
PostgreSQL:
This was pointed out by RhodiumToad on FreeNode and has performance on the order of O(log N)., much better then the other PostgreSQL answer here.
Both of the conditionals should be "or equal to" for much better handling of the id exists case. This also has handling in the case where two records share the same value for "abs(4 - id)" then that other PostgreSQL answer here.
上面的代码不适用于浮点数。
这是我修改后的 php 代码。
The above code doesn't works for floating numbers.
So here's my revised php code for that.
我和 https://stackoverflow.com/users/29253/igorgue 基于此处的其他一些答案。 只有 34 个字符:
Python by me and https://stackoverflow.com/users/29253/igorgue based on some of the other answers here. Only 34 characters:
Haskell 条目(已测试):
通过将接近 4 的数字放在前面来对列表进行排序。
head
采用第一个元素(最接近 4)。Haskell entry (tested):
Sorts the list by putting numbers closer to 4 near the the front.
head
takes the first element (closest to 4).Ruby
不是最有效的方法,但很短。
Ruby
Not the most efficient method, but pretty short.
仅返回一个数字:
returns only one number:
仅返回一个数字:
returns only one number:
Perl——66 个字符:
Perl -- 66 chars:
已编辑 = 在 for 循环中
EDITED = in the for loop
这是另一个 Haskell 答案:
Here's another Haskell answer:
Haskell,60 个字符 -
Haskell, 60 characters -
Kdb+,23B:
用法:
Kdb+, 23B:
Usage:
Python,不确定如何格式化代码,也不确定代码是否会按原样运行,但它的逻辑应该可以工作,并且可能有内置函数可以做到这一点......
Python, not sure how to format code, and not sure if code will run as is, but it's logic should work, and there maybe builtins that do it anyways...
在 Java 中使用可导航地图
In Java Use a Navigable Map
你们中的一些人似乎没有意识到该列表是
无序的
(尽管通过示例,我可以理解您的困惑)。 在 Java 中:不完全简洁,但嘿,它是 Java。
Some of you don't seem to be reading that the list is
unordered
(although with the example as it is I can understand your confusion). In Java:Not exactly terse but hey its Java.
Common Lisp 使用迭代库。
Common Lisp using iterate library.
F# 中的 41 个字符:
如
41 characters in F#:
as in
红宝石。 一次穿越。 很好地处理负数。 也许不是很短,但肯定很漂亮。
Ruby. One pass-through. Handles negative numbers nicely. Perhaps not very short, but certainly pretty.