Word Count (Ruby)
CoderByte offers the following challenge: "Using Ruby, use the WordCount (str) function to pass the passed str string parameter and return the number of words contained in the string (ie," Never eat shredded wheat ", return 4) Words will be split single spaces.
I solved this, but is there an easier solution (that doesn't use regex or methods other than .length)? I have a conditional inside a conditional inside a loop inside a loop. I also set the current variable to false both inside and outside the first for loop.
Are these bad practices? Is there a better solution?
def WordCount(string)
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
counter = 0
current = false
for i in 0...string.length
prev = current
current = false
for j in 0...alphabet.length
if string[i] == alphabet[j]
current = true
if prev == false
counter += 1
end
end
end
end
return counter
end
WordCount(STDIN.gets)
source to share
Hmm,
s = "Never eat shredded wheat"
puts s.split.count
# => 4
If you don't want to count underscores and numbers:
s = "Never eat shredded wheat 1 _ ?"
puts s.split.reject { |w| w =~ /(\W|_|\d)/ }.count
# => 4
even more advanced regex:
s = "Never __ 111 ?? eat shredded wheat. _Word?"
p s.split.reject { |w| w !~ /([a-zA-Z]+(_[a-zA-Z]+)*)/ }
# => ["Never", "eat", "shredded", "wheat.", "_Word?"]
source to share
The most elegant solution I saw when searching for words in Ruby was:
words = 'This is a word'
p words.scan(/\S+/).size #=> 4
For most convenience the monkey String patch:
class String
def number_of_words
self.scan(/\S+/).size
end
end
p 'Hi there, how are you?'.number_of_words #=> 5
The main problem I see with your code is that you are coding , but you are not coding in Ruby (style). You rarely see what people use here / here for example. If you know how to write idiomatic Ruby, code that will accept 10 lines in other languages is barely 1 line.
source to share
Punctuation is obviously a problem. In addition to the apostrophe mentioned elsewhere, high school students wrap certain groups of words such as compound adjectives, dashes are used to highlight slips, ellipses (such as the symbol "..." or multiple periods) indicate a continuation or change of thought, slashes provide a choice etc. One way to deal with this (without using regrex) is to use String # tr (or String # gsub ) to convert those punctuation marks to spaces (remove '
if you want to "not be treated as one word"):
def word_count str
str.tr("'-/–…\.", ' ').split.size
end
word_count "It was the best of times, it was the worst of times"
#=> 12
word_count "I don't think his/her answer is best."
#=> 9
word_count "Mozart is a much-beloved composer." # with hyphen
#=> 6
word_count "I pay the bills–she has all the fun." # with dash
#=> 9
word_count "I wish you would…oh, forget it." # with ellipse
#=> 7
word_count "I wish you would––oh, forget it." # with dashes
#=> 7
word_count ""
#=> 0
On a Mac, a dash is an option, a hyphen; ellipse, option, semi-colony (or "semicolon", both are accepted :-)).
Now we just need to figure out how to count the few words ("most modern") in one word. Actually, I just scratched the surface of this complex object. Sorry if I got carried away. What's the question again?
source to share