⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 09 - sorting an array by frequency of appearance.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
module Enumerable  def sort_by_frequency    histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash}    sort_by { |x| [histogram[x], x] }  endend[1,2,3,4,1,2,4,8,1,4,9,16].sort_by_frequency# => [3, 8, 9, 16, 2, 2, 1, 1, 1, 4, 4, 4]#---[1,2] <=> [0,2]                                     # => 1[1,2] <=> [1,2]                                     # => 0[1,2] <=> [2,2]                                     # => -1[1,2] <=> [1,1]                                     # => 1[1,2] <=> [1,3]                                     # => -1[1,2] <=> [1]                                       # => 1[1,2] <=> [3]                                       # => -1[1,2] <=> [0,1,2]                                   # => 1[1,2] <=> []                                        # => 1#---module Enumerable  def sort_by_frequency_faster    histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash}    sort_by { |x| histogram[x] }  endend[1,2,3,4,1,2,4,8,1,4,9,16].sort_by_frequency_faster# => [16, 8, 3, 9, 2, 2, 4, 1, 1, 4, 4, 1]#---module Enumerable  def sort_by_frequency_descending    histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash}    sort_by { |x| [histogram[x] * -1, x]}  endend[1,2,3,4,1,2,4,8,1,4,9,16].sort_by_frequency_descending# => [1, 1, 1, 4, 4, 4, 2, 2, 3, 8, 9, 16]#---module Enumerable  def sort_distinct_by_frequency    histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash }    histogram.keys.sort_by { |x| [histogram[x], x] }  endend[1,2,3,4,1,2,4,8,1,4,9,16].sort_distinct_by_frequency# => [3, 8, 9, 16, 2, 1, 4]#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -