Guest User

Untitled

a guest
Nov 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. Where:
  2. "Jason" = 2, "Judah" = 3, "Allison" = 1, "Teresa" = 1, "Michelle" = 1?
  3.  
  4. names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
  5. counts = Hash.new(0)
  6. names.each { |name| counts[name] += 1 }
  7. # => {"Jason" => 2, "Teresa" => 1, ....
  8.  
  9. names.inject(Hash.new(0)) { |total, e| total[e] += 1 ;total}
  10.  
  11. {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}
  12.  
  13. names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
  14. counts = {}
  15. names.group_by(&:itself).each { |k,v| counts[k] = v.length }
  16. # counts > {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}
  17.  
  18. Multiset.new(*names)
  19.  
  20. require 'multiset'
  21.  
  22. names = %w[Jason Jason Teresa Judah Michelle Judah Judah Allison]
  23.  
  24. histogram = Multiset.new(*names)
  25. # => #<Multiset: {"Jason", "Jason", "Teresa", "Judah", "Judah", "Judah", "Michelle", "Allison"}>
  26.  
  27. histogram.multiplicity('Judah')
  28. # => 3
  29.  
  30. require 'multiset'
  31.  
  32. names = %w[Jason Jason Teresa Judah Michelle Judah Judah Allison]
  33.  
  34. histogram = Multiset[*names]
  35. # => #<Multiset:#2 'Jason', #1 'Teresa', #3 'Judah', #1 'Michelle', #1 'Allison'>
  36.  
  37. names.each_with_object(Hash.new(0)) { |name, hash| hash[name] += 1}
  38.  
  39. => {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}
  40.  
  41. arr = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
  42. result = {}
  43. arr.uniq.each{|element| result[element] = arr.count(element)}
  44.  
  45. array_with_lower_case_a = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
  46. hash_grouped_by_name = array_with_lower_case_a.group_by {|name| name}
  47. hash_grouped_by_name.map{|name, names| [name, names.length]}
  48. => [["Jason", 2], ["Teresa", 1], ["Judah", 3], ["Michelle", 1], ["Allison", 1]]
  49.  
  50. another_array_with_lower_case_a = ["Jason", "jason", "Teresa", "Judah", "Michelle", "Judah Ben-Hur", "JUDAH", "Allison"]
  51. hash_grouped_by_first_name = another_array_with_lower_case_a.group_by {|name| name.split(" ").first.capitalize}
  52. hash_grouped_by_first_name.map{|first_name, names| [first_name, names.length]}
  53. => [["Jason", 2], ["Teresa", 1], ["Judah", 3], ["Michelle", 1], ["Allison", 1]]
  54.  
  55. names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
  56. Hash[names.group_by{|i| i }.map{|k,v| [k,v.size]}]
  57. # => {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}
  58.  
  59. a = [1, 2, 3, 2, 5, 6, 7, 5, 5]
  60. a.each_with_object(Hash.new(0)) { |o, h| h[o] += 1 }
  61.  
  62. # => {1=>1, 2=>2, 3=>1, 5=>3, 6=>1, 7=>1}
  63.  
  64. C:Documents and Settingsa.grimm>irb
  65. irb(main):001:0> Array = nil
  66. (irb):1: warning: already initialized constant Array
  67. => nil
  68. C:/Ruby19/lib/ruby/site_ruby/1.9.1/rbreadline.rb:3177:in `rl_redisplay': undefined method `new' for nil:NilClass (NoMethodError)
  69. from C:/Ruby19/lib/ruby/site_ruby/1.9.1/rbreadline.rb:3873:in `readline_internal_setup'
  70. from C:/Ruby19/lib/ruby/site_ruby/1.9.1/rbreadline.rb:4704:in `readline_internal'
  71. from C:/Ruby19/lib/ruby/site_ruby/1.9.1/rbreadline.rb:4727:in `readline'
  72. from C:/Ruby19/lib/ruby/site_ruby/1.9.1/readline.rb:40:in `readline'
  73. from C:/Ruby19/lib/ruby/1.9.1/irb/input-method.rb:115:in `gets'
  74. from C:/Ruby19/lib/ruby/1.9.1/irb.rb:139:in `block (2 levels) in eval_input'
  75. from C:/Ruby19/lib/ruby/1.9.1/irb.rb:271:in `signal_status'
  76. from C:/Ruby19/lib/ruby/1.9.1/irb.rb:138:in `block in eval_input'
  77. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:189:in `call'
  78. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:189:in `buf_input'
  79. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:103:in `getc'
  80. from C:/Ruby19/lib/ruby/1.9.1/irb/slex.rb:205:in `match_io'
  81. from C:/Ruby19/lib/ruby/1.9.1/irb/slex.rb:75:in `match'
  82. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:287:in `token'
  83. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:263:in `lex'
  84. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:234:in `block (2 levels) in each_top_level_statement'
  85. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:230:in `loop'
  86. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:230:in `block in each_top_level_statement'
  87. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `catch'
  88. from C:/Ruby19/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `each_top_level_statement'
  89. from C:/Ruby19/lib/ruby/1.9.1/irb.rb:153:in `eval_input'
  90. from C:/Ruby19/lib/ruby/1.9.1/irb.rb:70:in `block in start'
  91. from C:/Ruby19/lib/ruby/1.9.1/irb.rb:69:in `catch'
  92. from C:/Ruby19/lib/ruby/1.9.1/irb.rb:69:in `start'
  93. from C:/Ruby19/bin/irb:12:in `<main>'
  94.  
  95. C:Documents and Settingsa.grimm>
  96.  
  97. arr = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
  98.  
  99. arr.uniq.inject({}) {|a, e| a.merge({e => arr.count(e)})}
  100.  
  101. names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
  102.  
  103. name_frequency_hash = {}
  104.  
  105. names.each do |name|
  106. count = names.count(name)
  107. name_frequency_hash[name] = count
  108. end
  109. #=> {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}
Add Comment
Please, Sign In to add comment