forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountLetter.rb
More file actions
30 lines (23 loc) · 702 Bytes
/
CountLetter.rb
File metadata and controls
30 lines (23 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
=begin
In this kata, you've to count letters in a given string and return the
letter count in a hash with 'letter' as key and count as 'value'.
The key must be 'symbol' instead of string in Ruby and 'char' instead
of string in Crystal.
Example:
letter_count('arithmetics') #=> {:a=>1, :c=>1, :e=>1, :h=>1, :i=>2, :m=>1,
:r=>1, :s=>1, :t=>2}
=end
# My Solution
def letter_count(str)
letter = Hash.new {|x,k| x[k] = 0}
str.split("").each {|x| letter[x.to_sym] += 1}
letter
end
# Better Soltuion
def letter_count(str)
str.chars.each_with_object(Hash.new(0)) { |c, h| h[c.to_sym] += 1 }
end
# Another Solution
def letter_count(str)
str.chars.map { | i | [:"#{i}", str.count(i)] }.to_h
end