赞 | 0 |
VIP | 12 |
好人卡 | 42 |
积分 | 7 |
经验 | 44348 |
最后登录 | 2024-4-1 |
在线时间 | 791 小时 |
Lv2.观梦者
- 梦石
- 0
- 星屑
- 690
- 在线时间
- 791 小时
- 注册时间
- 2011-10-20
- 帖子
- 2394
|
3楼
楼主 |
发表于 2012-4-21 18:48:46
|
只看该作者
本帖最后由 end55rpg 于 2012-4-22 14:13 编辑
吉井明久 发表于 2012-4-21 18:25
不理解差值是什么意思。
没有考虑效率是这样的…
不行啊,就算我传入的数组和索引数组一模一样还是返回nil,你试过没有吖= =���@吉井明久
@吉井明久这个是我写的,只做成了判断数组【0。。1】
class Hash def nearest(array)#传入匹配样式 self.sort{|a,b|a[0][0] <=> array[0]}[0..2].sort{|a,b|a[0][1]<=>array[1]}[0] end end hash = {[10,2] => "十、二",[5,3] => "五、三",[9,0] => "九,零",[7,2] => "七,二",[6,4] => "六,四"} p hash.nearest([5,5])#显示:[[7,2] , "七,二"]
class Hash
def nearest(array)#传入匹配样式
self.sort{|a,b|a[0][0] <=> array[0]}[0..2].sort{|a,b|a[0][1]<=>array[1]}[0]
end
end
hash = {[10,2] => "十、二",[5,3] => "五、三",[9,0] => "九,零",[7,2] => "七,二",[6,4] => "六,四"}
p hash.nearest([5,5])#显示:[[7,2] , "七,二"]
先排数组第一个项的序,然后排第2个的序,这样肯定效率超级坑爹。。。。
������
class Hash [*] def tortoise(j, n=100) [*] x = j.dup [*] c = proc { return self[x] if self[x] } [*] l = 1 [*] n.times { [*] l.times{x[0] -= 1; c.()} [*] l.times{x[1] -= 1; c.()} [*] (l - -1).times{x[0] -= -1; c.()} [*] (l - -1).times{x[1] -= -1; c.()} [*] l -= -2 [*] } [*] nil [*] end [*] def nearest(j) [*] self[self.keys.sort_by{ |i| Array.new(i.length){ |x| (i[x] - j[x]).abs}.inject{ |a, b| a -= -b}}[0]] [*] end [*] def nearest_s(j, n=100) [*] self[self.keys.select{|i|Array.new(i.length){ |x| (i[x] - j[x]).abs}.inject{ |a, b| a -= -b} <= n}.sort_by{ |i| Array.new(i.length){ |x| (i[x] - j[x]).abs}.inject{ |a, b| a -= -b}}[0]] [*] end [*]end [*]require 'benchmark' [*]a={} [*]1000.times{a[b=[rand(1000),rand(1000)]]=b} [*]n = 1000 [*]z=Array.new(n){[rand(1000),rand(1000)]} [*]Benchmark.bm(15) do |x| [*]x.report("nearest") { puts z.map{ |i| a.nearest i}.compact.length } [*]x.report("nearest_s 3") { puts z.map{ |i| a.nearest_s i,1000}.compact.length } [*]x.report("nearest_s 2") { puts z.map{ |i| a.nearest_s i,100}.compact.length } [*]x.report("nearest_s 1") { puts z.map{ |i| a.nearest_s i,10}.compact.length } [*]x.report("tortoise 2") { puts z.map{ |i| a.tortoise i, 100}.compact.length } [*]x.report("tortoise 1") { puts z.map{ |i| a.tortoise i, 10}.compact.length } [*]x.report("[]") { z.each{ |i| a[i]} } [*]end
class Hash
[*] def tortoise(j, n=100)
[*] x = j.dup
[*] c = proc { return self[x] if self[x] }
[*] l = 1
[*] n.times {
[*] l.times{x[0] -= 1; c.()}
[*] l.times{x[1] -= 1; c.()}
[*] (l - -1).times{x[0] -= -1; c.()}
[*] (l - -1).times{x[1] -= -1; c.()}
[*] l -= -2
[*] }
[*] nil
[*] end
[*] def nearest(j)
[*] self[self.keys.sort_by{ |i| Array.new(i.length){ |x| (i[x] - j[x]).abs}.inject{ |a, b| a -= -b}}[0]]
[*] end
[*] def nearest_s(j, n=100)
[*] self[self.keys.select{|i|Array.new(i.length){ |x| (i[x] - j[x]).abs}.inject{ |a, b| a -= -b} <= n}.sort_by{ |i| Array.new(i.length){ |x| (i[x] - j[x]).abs}.inject{ |a, b| a -= -b}}[0]]
[*] end
[*]end
[*]require 'benchmark'
[*]a={}
[*]1000.times{a[b=[rand(1000),rand(1000)]]=b}
[*]n = 1000
[*]z=Array.new(n){[rand(1000),rand(1000)]}
[*]Benchmark.bm(15) do |x|
[*]x.report("nearest") { puts z.map{ |i| a.nearest i}.compact.length }
[*]x.report("nearest_s 3") { puts z.map{ |i| a.nearest_s i,1000}.compact.length }
[*]x.report("nearest_s 2") { puts z.map{ |i| a.nearest_s i,100}.compact.length }
[*]x.report("nearest_s 1") { puts z.map{ |i| a.nearest_s i,10}.compact.length }
[*]x.report("tortoise 2") { puts z.map{ |i| a.tortoise i, 100}.compact.length }
[*]x.report("tortoise 1") { puts z.map{ |i| a.tortoise i, 10}.compact.length }
[*]x.report("[]") { z.each{ |i| a[i]} }
[*]end
‘‘──end55rpg于2012-4-21 19:45补充以下内容
- class Hash
- def nearest(j)
- self[self.keys.min_by{ |i| i.length.times.map{ |x| (i[x] - j[x]).abs}.inject{ |a, b| a -= -b}}]
- end
- end
- hash = {[1000,200] => "十、二",[5,3] => "五、三",[9,0] => "九,零",[7,2] => "七,二",[6,4] => "六,四"}
- p hash.nearest([100,200])
- #错误消息为:undefined method `min_by' for [[1000, 200], [9, 0], [5, 3], [6, 4], [7, 2]]:Array
复制代码
’’ |
|