@Carols10cents h = http://Hash.new (0); a.each{|l| h[l] += 1}; h
PUZZLE TIME how would you implement in ruby a method that takes ["a", "b", "a", "c", "c"] and returns {"a" => 2, "b" => 1, "c" => 2} ?
-
-
- 2 more replies
New conversation -
-
-
ie the counts of the occurrences. A bunch of us can't find any pretty answers.
-
@Carols10cents Something like: a = ["a", "b", "a", "c", "c"] a.uniq.map { |x| {x => a.count(x) } } - 2 more replies
New conversation -
-
-
This Tweet is unavailable.
- 3 more replies
-
-
-
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
@carols10cents a.sort.chunk(&:itself).map {|v, vs| [v, vs.count]}.to_h -
@coreyhaines yeah but that means i have to upgrade to ruby 2.2 or rails 4 ;) i do like `itself` though. - 4 more replies
New conversation -
-
-
@Carols10cents ``` def some_method(hash) {"a" => 2, "b" => 1, "c" => 2} end ``` I bet it fails your _next_ test though. :P - End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.