在ruby中使用哈希排序的正确方法 [英] correct way of using hash sort in ruby

查看:98
本文介绍了在ruby中使用哈希排序的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ruby的新手,我正在尝试编写一个dijkstra函数,但是我的哈希排序似乎根本不起作用

  def distance(start_code,end_code,map)
#初始化散列距离
#distance初始化为-1
dist_hash = Hash.new()
start_hash = Hash.new()
parent_hash = Hash.new()
close_list = Array.new()
find = -1
map.citylist.each do | e |
dist_hash [e] = [+ 1.0 / 0.0]
结束

start_hash [start_code] = 0
parent_hash [start_code] = start_code

while(start_hash.empty?)== false


#sort the hash
start_hash.sort_by {| k,v | v)
puts'value'
puts start_hash.values()
#pop散列中的第一项
h = start_hash.shift()
curr_key = h [ 0]
curr_val = h [1]
curr_city = map.findcity(curr_key)
close_list<< curr_city.code

#为相邻列表中的每一个
curr_city.get_adj_city()。每个都有| e |


#如果它在关闭列表中,那么igonore
if close_list.include?(e)== false
#如果它不在start_hash中,那么add如果start_hash.has_key?(e)== false
dist = map.adj_dist(curr_city.code,e)
dist = dist + curr_val
start_hash [e ] = dist
parent_hash [e] = curr_city.code
#如果在start_hash检查是否有更好的距离
else
dist = map.adj_dist(curr_city.code ,e)
if(dist + curr_val)< start_hash [e]
parent_hash [e] = curr_city.code
start_hash [e] = dist
end
end
#end pf检查单个城市
结束
检查结束时是否包含在结尾


结束
#结束检查整个列表

如果为curr_city.code == end_code
find = 0
break
end

end
#end如果发现== 0
ptr = end_code,则检查节点
#result

把ptr
放入final list

while ptr!= start_code
ptr = parent_hash [ptr]
puts ptr
end
return 0
else
return -1
end

end



当我试图调用d.distance(BUE,LOS,map)时,b

输出结果如下:

 
0

1680
4651
价值
10053
8047
4651

11094
15839
15839
8047
4651
10779
....

在hash.sort_by之后但未排序。我正确使用该方法吗?

解决方案


这些值在hash.sort_by之后打印出来,但是没有排序。我是否正确地使用了方法?


没有。当我不确定事情是如何工作的时候,我打开IRB并尝试了几件事情:

  hash = {a :1,b:2,c:4,d:3} 
=> {:a => 1,:b => 2,:c => 4,:d => 3}
hash.sort
=> [[:a,1],[:b,2],[:c,4],[:d,3]]
hash
=> {:a => 1,:b => 2,:c => 4,:d => 3}
hash.sort_by {| k,v | v}
=> [[:a,1],[:b,2],[:d,3],[:c,4]]
hash
=> {:a => 1,:b => 2,:c => 4,:d => 3}

sort_by 不会改变散列,它会返回结果。尝试:

hash = hash.sort_by {| k,v |

 <$ c  

不要使用这个,它是一个数组,你会误导任何人阅读这段代码。 $ c> sorted_tuples = hash.sort_by {| k,v | v}

或类似的东西。


I'm new to ruby and I'm trying to write a dijkstra function but my hash sort seems doesn't work at all

def distance(start_code, end_code, map)
#initialize hash for distance 
#distance are initialized to -1
dist_hash=Hash.new()
start_hash=Hash.new()
parent_hash=Hash.new()
close_list=Array.new()
find=-1
map.citylist.each do |e|
  dist_hash[e]=[+1.0/0.0]
end

start_hash[start_code]=0
parent_hash[start_code]=start_code

while (start_hash.empty?)==false


   #sort the hash
  start_hash.sort_by {|k,v| v}
  puts 'value'      
  puts start_hash.values()  
   #pop the first item in the hash
  h=start_hash.shift()
  curr_key=h[0]
  curr_val=h[1]
  curr_city=map.findcity(curr_key)
  close_list<<curr_city.code

   #for every one in adjacent list
  curr_city.get_adj_city().each do |e|


     #if it in the close list then igonore
    if close_list.include?(e)==false  
       #if it is not in the start_hash then add to start hash
      if start_hash.has_key?(e)==false
        dist=map.adj_dist(curr_city.code, e)
        dist=dist+curr_val
        start_hash[e]=dist
        parent_hash[e]=curr_city.code
       #if it is in the start_hash check if we have better distance
      else
        dist=map.adj_dist(curr_city.code, e)
        if (dist+curr_val)<start_hash[e]
          parent_hash[e]=curr_city.code
          start_hash[e]=dist
        end
      end
       #end pf checking single adj city
    end
     #end of check if include in close


  end
   #end of check whole list

  if curr_city.code==end_code
    find=0
    break
  end

end
#end of check node
#result
if find==0
  ptr=end_code
  puts ptr
  puts "final list"

  while ptr!=start_code
    ptr=parent_hash[ptr]
    puts ptr
  end
  return 0
else
  return -1
end

end

When I'm trying to call d.distance("BUE", "LOS", map)

the output looks like

value
0
value
1680
4651
value
10053
8047
4651
value
11094
15839
15839
8047
4651
10779
....

the values are printed out right after hash.sort_by but not sorted. Am I using the method correctly?

解决方案

the values are printed out right after hash.sort_by but not sorted. Am I using the method correctly?

No. When I'm not sure how something works, I open up IRB and try a few things with it:

hash = {a:1, b:2, c:4, d: 3}
=> {:a=>1, :b=>2, :c=>4, :d=>3}
hash.sort
=> [[:a, 1], [:b, 2], [:c, 4], [:d, 3]]
hash
=> {:a=>1, :b=>2, :c=>4, :d=>3}
hash.sort_by{|k,v| v }
=> [[:a, 1], [:b, 2], [:d, 3], [:c, 4]]
hash
=> {:a=>1, :b=>2, :c=>4, :d=>3}

sort_by does not alter the hash, it returns a result. Try:

hash = hash.sort_by{|k,v| v } # <- don't use this, it's an array and you'll mislead anyone reading this code.

sorted_tuples = hash.sort_by{|k,v| v }

or something like it.

这篇关于在ruby中使用哈希排序的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆