forked from CoderAcademy-BRI/ruby-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_count_letters.rb
More file actions
32 lines (20 loc) · 858 Bytes
/
08_count_letters.rb
File metadata and controls
32 lines (20 loc) · 858 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
31
32
# Count Letters
# Write a method that will take a string, keep count
# of each letter and return the totals as a hash.
# Example:
# count_letters("hello") should return {"h"=>1, "e"=>1, "l"=>2, "o"=>1}
# count_letters("mississippi") should return {"m"=>1, "i"=>4, "s"=>4, "p"=>2}
# Check your solution by running the tests:
# ruby tests/08_count_letters_test.rb
#need to split out characters
#need to keep count of number of times that a character is in a string
#need to return key- pair value in hash. letter in string and number of times it appears in string
def count_letters(string)
result = {}
string.chars.each{|element| result[element] = string.count(element)}
end
puts count_letters("hello")
# return result # return the hash
# end
# # puts count_letters("hello")
# puts count_letters('string')