Created
March 9, 2012 05:50
-
-
Save brianjolly/2005228 to your computer and use it in GitHub Desktop.
Number words letter counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LettersCounter | |
def count word | |
word.gsub(/\s|-/, '').length | |
end | |
def sum_from_list words | |
lengths = words.map { |word| count word } | |
lengths.inject(:+) | |
end | |
end | |
require 'minitest/autorun' | |
require 'linguistics' | |
describe LettersCounter do | |
before do | |
@it = LettersCounter.new | |
end | |
describe 'count' do | |
it 'can count the number of letters in a word' do | |
@it.count('one').must_equal 3 | |
end | |
it 'should not count spaces or hyphens' do | |
@it.count('three hundred and forty-two').must_equal 23 | |
@it.count('one hundred and fifteen').must_equal 20 | |
end | |
end | |
describe 'sum_from_list' do | |
before do | |
Linguistics::use :en | |
end | |
it 'can calculate the sum of letters used' do | |
words = ['three hundred and forty-two', 'one hundred and fifteen'] | |
@it.sum_from_list(words).must_equal 43 | |
end | |
it 'can calculate the sum of letters from 1 to 5' do | |
words = (1..5).map { |number| number.en.numwords } | |
@it.sum_from_list(words).must_equal 19 | |
end | |
it 'can calculate the sum of letters from 1 to 1000' do | |
words = (1..1000).map { |number| number.en.numwords } | |
@it.sum_from_list(words).must_equal 21124 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment