-
Notifications
You must be signed in to change notification settings - Fork 252
/
word_count.rb
35 lines (33 loc) · 859 Bytes
/
word_count.rb
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
33
34
35
#Problem : https://exercism.org/tracks/ruby/exercises/word-count
#Solution
class Phrase
def initialize(input)
@phrase = input.downcase
end
def word_count
result = Hash.new(0)
curr_word=""
len = @phrase.length
for i in 0...len
unless @phrase[i].match(/^[a-zA-Z0-9']/)
result[curr_word]+=1 unless curr_word==""
curr_word=""
next
end
next if @phrase[i]=="'" and \
(i==len-1 || i==0 || !@phrase[i+1].match(/^[a-zA-Z0-9']/) || !@phrase[i-1].match(/^[a-zA-Z0-9']/))
curr_word+=@phrase[i]
end
result[curr_word]+=1 unless curr_word=="" or curr_word=="'"
result
end
end
#Solution 2
class Phrase
def initialize(phrase)
@phrase = phrase
end
def word_count
@phrase.downcase.scan(/\b[\w']+\b/).tally
end
end