Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
0bb0a87
Initial structure for project defined
LySofDev Nov 30, 2015
cc29e8c
Added spec_helper
LySofDev Nov 30, 2015
3ac1082
Linked lib to spec
LySofDev Nov 30, 2015
3119abf
Added spec descriptions
LySofDev Nov 30, 2015
049754d
finished filling in tests
LySofDev Nov 30, 2015
6dc2620
Removed NumReader module reference
LySofDev Nov 30, 2015
e13bb28
Defined #int_to_word
LySofDev Nov 30, 2015
7891f6a
added 'number' param
LySofDev Nov 30, 2015
f16f544
Added documentation to method
LySofDev Nov 30, 2015
37cfe96
Added separation of hundred, thousand and million
LySofDev Nov 30, 2015
aff4774
Syntax correction
LySofDev Nov 30, 2015
b5c8700
Added documentation on range separation
LySofDev Nov 30, 2015
304b2c9
Added documentation on range separation
LySofDev Nov 30, 2015
f4245a3
Added #hundred_to_word specification
LySofDev Nov 30, 2015
3891bd1
Completed the tests for #hundred_to_word
LySofDev Nov 30, 2015
dc00671
Defined #hundred_to_word
LySofDev Nov 30, 2015
f8077ed
Added documentation to #hundred_to_word
LySofDev Nov 30, 2015
10c8bca
Added digit separation
LySofDev Nov 30, 2015
4b320ea
Defined numerics hash in #hundred_to_word
LySofDev Nov 30, 2015
b266fce
Finished defining #hundred_to_words
LySofDev Nov 30, 2015
21411cf
Syntax correction in numerics hash
LySofDev Nov 30, 2015
b3976f5
Added #strip to #hundred_to_word result
LySofDev Nov 30, 2015
e98bbbc
Added #strip to #hundred_to_word result
LySofDev Nov 30, 2015
3e7cc37
Placed unless outside of string interpolation
LySofDev Nov 30, 2015
7c80f05
Corrected incorrect reference to hundreth instead of tenth
LySofDev Nov 30, 2015
bb8cc2c
Multiplying tenth digit by 10 when referencing
LySofDev Nov 30, 2015
1b4f80e
Added result string assembly and return
LySofDev Nov 30, 2015
eafc3f3
DRY corrections
LySofDev Nov 30, 2015
429282c
Reduced the numerics hash spacing
LySofDev Nov 30, 2015
82ae5bd
Refactored result generation as part of hundred addition
LySofDev Nov 30, 2015
7d05966
Improved documentation on #int_to_word
LySofDev Nov 30, 2015
94e12b3
Improved documentation on #hundred_to_word
LySofDev Nov 30, 2015
4e4e6a7
Removed the 'unless tens == 0' check as it will never occur
LySofDev Nov 30, 2015
32c4297
Removed DRY option of result
LySofDev Nov 30, 2015
7c57394
Added spaces for clarity
LySofDev Nov 30, 2015
1d08d58
Renamed numerics hash to numeric
LySofDev Nov 30, 2015
c20a10b
Syntax corrections
LySofDev Nov 30, 2015
1091409
Syntax corrections
LySofDev Nov 30, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

# Created by https://www.gitignore.io/api/ruby

### Ruby ###
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

## Specific to RubyMotion:
.dat*
.repl_history
build/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalisation:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

82 changes: 82 additions & 0 deletions lib/solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# The hundred_to_word method accepts an integer above 0 and below 1 thousand
# and returns a string representation of the number in english numerics
def hundred_to_word(number)

# The numerics hash contains all translations of integer numbers to english
# numeric string values assuming that they do not require composition
numeric = {
1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six",
7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten", 11 => "eleven",
12 => "twelve", 13 => "thirteen", 14 => "fourteen", 15 => "fifteen",
16 => "sixteen", 17 => "seventeen", 18 => "eighteen", 19 => "nineteen",
20 => "twenty", 30 => "thirty", 40 => "forty", 50 => "fifty", 60 => "sixty",
70 => "seventy", 80 => "eighty", 90 => "ninety"
}

# Divide the number by 100 to receive a single digit representing the
# hundreth value.
hundred = number / 100

# Initiate the result string to the numeric translation of the hundred value
# unless this value is equal to 0. In that case, initiate to an empty string.
result = hundred != 0 ? "#{numeric[hundred]} hundred " : ""

# Apply the modulus of 100 operation to the number in order to receive the two
# digits representing the tens value.
tens = number % 100

# Append the numeric string value of the remaining two digits to the result
# string. If the remaining numbers exists as a key within the numeric hash,
# then append the translation to the result.
if numeric.keys.include?(tens)
result += "#{numeric[tens]}"

# If the value of tens is not present in the numeric.keys then decompose the
# number until the values are included in the numeric.keys.
else

# Remove the trailing digit for the tens digit and append a 0
tens = (tens / 10) * 10

# Capture the ones digit by applying the modulus of 10 operation to the number
ones = number % 10

# Append the composite translation of tens and ones to and ommit the tens
# value if it is equal to 0
result += "#{numeric[tens] unless tens == 0} #{numeric[ones]}"
end

# Return the result string after calling the #strip method which removes
# leading and trailing white spaces
result.strip
end

# The int_to_word method accepts an integer above 0 and below 1 billion
# and returns a string representation of the number in english numerics
def int_to_word(number)

# Dividing the number by a million returns the three digits containing millions
million = number / 1000000

# Dividing the number by a thousand returns 6 digits including the thousands
# and hundreds. The hundred value digits are trimmed by applying the modulus
# operation of the value of 'million'.
thousand = (number / 1000) % 1000

# Applying the modulus operation of 1000 returns the three digits representing
# the hundred values
hundred = number % 1000

# Initiate the result string with the million string value unless the million
# value is equal to 0. If million is equal to 0, return an empty string.
result = million != 0 ? "#{hundred_to_word(million)} million " : ""

# Append the string values for thousand and for hundred. Ignore the operation
# if a value is equal to 0
result += "#{hundred_to_word(thousand)} thousand " unless thousand == 0
result += "#{hundred_to_word(hundred)}" unless hundred == 0

# Return the result string after calling the #strip method which removes
# leading and trailing white spaces
result.strip
end
42 changes: 42 additions & 0 deletions spec/solution_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "spec_helper"

describe "solution.rb" do

describe "#hundred_to_word" do

it "returns 300 as three hundred" do
expect(hundred_to_word(300)).to eq("three hundred")
end

it "returns 750 as seven hundred fifty" do
expect(hundred_to_word(750)).to eq("seven hundred fifty")
end

it "returns 45 as forty five" do
expect(hundred_to_word(45)).to eq("forty five")
end

end

# Specifications as provided by the projects README.md file
describe "#int_to_word" do

it "returns 4527 as four thousand five hundred twenty seven" do
expect(int_to_word(4527)).to eq("four thousand five hundred twenty seven")
end

it "returns 200017 as two hundred thousand seventeen" do
expect(int_to_word(200017)).to eq("two hundred thousand seventeen")
end

it "returns 784 as seven hundred eighty four" do
expect(int_to_word(784)).to eq("seven hundred eighty four")
end

it "returns 1000215 as one million two hundred fifteen" do
expect(int_to_word(1000215)).to eq("one million two hundred fifteen")
end

end

end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative "../lib/solution"