开发者

How do I create a diff of hashes with a correction factor?

开发者 https://www.devze.com 2023-02-13 10:19 出处:网络
I want to compare hashes inside an array: h_array = [ {:name => \"John\", :age => 23, :eye_color => \"blue\"},

I want to compare hashes inside an array:

h_array = [
  {:name => "John", :age => 23, :eye_color => "blue"},
  {:name => "John", :age => 22, :eye_color => "green"},
  {:name => "John", :age => 22, :eye_color => "black"}
]

get_diff(h_array, correct_factor = 2)
# should return [{:eye_color => "blue"}, {:eye_color => "green"}, {:eye_color => "black"}]

get_diff(h_array, correct_factor = 3)
# should return 
# [[{:age => 23}, {:age => 22}, {:age => 22}], 
# [{:eye_color => "blue"}, {:eye_color => "green"}, {:eye_color => "black"}]]

I want to diff the hashes contained in the h_array. It looks 开发者_如何学Pythonlike a recursive call/method because the h_array can have multiple hashes but with the same number of keys and values. How can I implement the get_diff method?


def get_diff h_array, correct_factor
  h_array.first.keys.reject{|k|
    h_array.map{|h| h[k]}.sort.chunk{|e| e}.map{|_,e| e.size}.max >= correct_factor
  }.map{|k|
    h_array.map{|hash| hash.select{|key,_| k == key}}
  }
end


class Array
   def find_ndups     # also returns the number of items
      uniq.map { |v| diff = (self.size - (self-[v]).size); (diff > 1) ? [v, diff] : nil}.compact
   end
end
h_array = [
  {:name => "John", :age => 22, :eye_color => "blue", :hair => "black"},
  {:name => "John", :age => 33, :eye_color => "orange", :hair => "green"},
  {:name => "John", :age => 22, :eye_color => "black", :hair => "yello"}
]
def get_diff(h_array, correct_factor)
  temp = h_array.inject([]){|result, element| result << element.to_a}
  master_array = []
  unmatched_arr = []
  matched_arr = []
  temp = temp.transpose
  temp.each_with_index do |arr, index|
    ee = arr.find_ndups
    if ee.length == 0
      unmatched_arr << temp[index].inject([]){|result, arr| result << {arr.first => arr.last} }

    elsif ee.length > 0  && ee[0][1] != correct_factor && ee[0][1] < correct_factor
      return_arr << temp[index].inject([]){|result, arr| result << {arr.first => arr.last} }
    elsif ee[0][1] = correct_factor
      matched_arr  << temp[index].inject([]){|result, arr| result << {arr.first => arr.last} }
    end

  end
  return [matched_arr, unmatched_arr]
end

puts get_diff(h_array, 2).inspect

hope it helps


found this ActiveSupport::CoreExtensions::Hash::Diff module. ActiveSupport 2.3.2 and 2.3.4 has a built in Hash::Diff module which returns a hash that represents the difference between two hashes.

0

精彩评论

暂无评论...
验证码 换一张
取 消