From 0bb0a875eba80bb51fa0556f0e151e90a42c641b Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:25:46 +0000 Subject: [PATCH 01/38] Initial structure for project defined --- .gitignore | 41 +++++++++++++++++++++++++++++++++++++++++ lib/solution.rb | 0 spec/solution_spec.rb | 0 3 files changed, 41 insertions(+) create mode 100644 .gitignore create mode 100644 lib/solution.rb create mode 100644 spec/solution_spec.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48c59a5 --- /dev/null +++ b/.gitignore @@ -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 + diff --git a/lib/solution.rb b/lib/solution.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb new file mode 100644 index 0000000..e69de29 From cc29e8cfd0b80586e28177bf47325d68e24e6eb3 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:27:16 +0000 Subject: [PATCH 02/38] Added spec_helper --- spec/solution_spec.rb | 1 + spec/spec_helper.rb | 0 2 files changed, 1 insertion(+) create mode 100644 spec/spec_helper.rb diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index e69de29..2f7ebd1 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -0,0 +1 @@ +require_relative "../lib/solution" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..e69de29 From 3ac1082877aabe5e156aacf0b96ed8d63a38e7af Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:27:57 +0000 Subject: [PATCH 03/38] Linked lib to spec --- spec/solution_spec.rb | 2 +- spec/spec_helper.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index 2f7ebd1..671fd6c 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -1 +1 @@ -require_relative "../lib/solution" +require "spec_helper" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e69de29..2f7ebd1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require_relative "../lib/solution" From 3119abfa45d7ad6b53b7c3b12763f41329ae49fa Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:31:44 +0000 Subject: [PATCH 04/38] Added spec descriptions --- spec/solution_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index 671fd6c..d70408f 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -1 +1,9 @@ require "spec_helper" + +describe NumReader do + + describe "#int_to_word" do + + end + +end From 049754de28a2cc9e2c2159ccb93bc0af92d46fca Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:34:28 +0000 Subject: [PATCH 05/38] finished filling in tests --- spec/solution_spec.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index d70408f..edca5a7 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -1,9 +1,25 @@ require "spec_helper" -describe NumReader do +describe "solution.rb" do describe "#int_to_word" do + it "returns 4527 as four thousand five hundred twenty seven" do + expect(NumReader.int_to_word(4527)).to eq("four thousand five hundred twenty seven") + end + + it "returns 200017 as two hundred thousand seventeen" do + expect(NumReader.int_to_word(200017)).to eq("two hundred thousand seventeen") + end + + it "returns 784 as seven hundred eighty four" do + expect(NumReader.int_to_word(784)).to eq("seven hundred eighty four") + end + + it "returns 1000215 as one million two hundred fifteen" do + expect(NumReader.int_to_word(1000215)).to eq("one million two hundred fifteen") + end + end end From 6dc26208179656aaae26fa8db418fa8a563114aa Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:35:07 +0000 Subject: [PATCH 06/38] Removed NumReader module reference --- spec/solution_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index edca5a7..af2a0ad 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -5,19 +5,19 @@ describe "#int_to_word" do it "returns 4527 as four thousand five hundred twenty seven" do - expect(NumReader.int_to_word(4527)).to eq("four thousand five hundred twenty seven") + expect(int_to_word(4527)).to eq("four thousand five hundred twenty seven") end it "returns 200017 as two hundred thousand seventeen" do - expect(NumReader.int_to_word(200017)).to eq("two hundred thousand seventeen") + expect(int_to_word(200017)).to eq("two hundred thousand seventeen") end it "returns 784 as seven hundred eighty four" do - expect(NumReader.int_to_word(784)).to eq("seven hundred eighty four") + expect(int_to_word(784)).to eq("seven hundred eighty four") end it "returns 1000215 as one million two hundred fifteen" do - expect(NumReader.int_to_word(1000215)).to eq("one million two hundred fifteen") + expect(int_to_word(1000215)).to eq("one million two hundred fifteen") end end From e13bb28caa7538ccb36c01a32c110904580f46b8 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:38:32 +0000 Subject: [PATCH 07/38] Defined #int_to_word --- lib/solution.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/solution.rb b/lib/solution.rb index e69de29..a45d5ef 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -0,0 +1,3 @@ +def int_to_word + +end From 7891f6a83594b67c22d5a4a70fb7878645449580 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:38:58 +0000 Subject: [PATCH 08/38] added 'number' param --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index a45d5ef..f3df5d7 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,3 +1,3 @@ -def int_to_word +def int_to_word(number) end From f16f5444dba5052182f4b8d9d74c533d8d199773 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:39:57 +0000 Subject: [PATCH 09/38] Added documentation to method --- lib/solution.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/solution.rb b/lib/solution.rb index f3df5d7..36e73b7 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,3 +1,5 @@ +# 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) end From 37cfe9684d5b63843c271d6658b80951f102df1b Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:47:44 +0000 Subject: [PATCH 10/38] Added separation of hundred, thousand and million --- lib/solution.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/solution.rb b/lib/solution.rb index 36e73b7..7b4fa56 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -2,4 +2,8 @@ # and returns a string representation of the number in english numerics def int_to_word(number) + million = umber / 1000000 + thousand = (number / 1000) % million + hundred = number % 1000 + end From aff4774267c8a2ec21c49c75be66a3e24cdf4c7b Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:48:01 +0000 Subject: [PATCH 11/38] Syntax correction --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index 7b4fa56..f5f94df 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -2,7 +2,7 @@ # and returns a string representation of the number in english numerics def int_to_word(number) - million = umber / 1000000 + million = number / 1000000 thousand = (number / 1000) % million hundred = number % 1000 From b5c87008b2a1516b49cf2ac8ba02b900ed1e3441 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:54:01 +0000 Subject: [PATCH 12/38] Added documentation on range separation --- lib/solution.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/solution.rb b/lib/solution.rb index f5f94df..7f6d4ba 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -2,8 +2,17 @@ # 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) % million + + # Applying the modulus operation of 1000 returns the three digits representing + # the hundred values hundred = number % 1000 end From 304b2c9c408b88bac05fc3dc77f0e65ebecbb30e Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:54:59 +0000 Subject: [PATCH 13/38] Added documentation on range separation --- lib/solution.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index 7f6d4ba..0768400 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -2,8 +2,7 @@ # 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 + # 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 From f4245a33d1d0e8883c96f67d8b7b542cba820167 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:56:30 +0000 Subject: [PATCH 14/38] Added #hundred_to_word specification --- spec/solution_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index af2a0ad..4c191dd 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -2,6 +2,22 @@ describe "solution.rb" do + describe "#hundred_to_word" do + + it "returns 300 as three hundred" do + + end + + it "returns 750 as seven hundred fifty" do + + end + + it "returns 45 as forty five" do + + end + + end + describe "#int_to_word" do it "returns 4527 as four thousand five hundred twenty seven" do From 3891bd1f2de21d4cb4a7ef9ccb493ca56e65e0a4 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:57:44 +0000 Subject: [PATCH 15/38] Completed the tests for #hundred_to_word --- spec/solution_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index 4c191dd..fd40b51 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -5,15 +5,15 @@ 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 From dc006715b6c7223f357a0273c0106ce25c208cbd Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 18:58:19 +0000 Subject: [PATCH 16/38] Defined #hundred_to_word --- lib/solution.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/solution.rb b/lib/solution.rb index 0768400..06b907d 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,3 +1,7 @@ +def hundred_to_word(number) + +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) From f8077ed2e3a039e4d2cf8721c29debc71f2b15f7 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:00:57 +0000 Subject: [PATCH 17/38] Added documentation to #hundred_to_word --- lib/solution.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index 06b907d..7135485 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,3 +1,5 @@ +# 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) end @@ -12,7 +14,7 @@ def int_to_word(number) # 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) % million + thousand = million != 0 ? (number / 1000) % million : number / 1000 # Applying the modulus operation of 1000 returns the three digits representing # the hundred values From 10c8bcaa1d3dbc1208dc5c68a224cc661512ad4e Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:06:58 +0000 Subject: [PATCH 18/38] Added digit separation --- lib/solution.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index 7135485..be24d59 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,7 +1,17 @@ # 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) - + tens = { + + } + ones = { + + } + + h = number / 100 + t = (number / 100) % 10 + o = number % 10 + end # The int_to_word method accepts an integer above 0 and below 1 billion @@ -14,7 +24,7 @@ def int_to_word(number) # 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 = million != 0 ? (number / 1000) % million : number / 1000 + thousand = (number / 1000) % 1000 # Applying the modulus operation of 1000 returns the three digits representing # the hundred values From 4b320eabd6e23bba804ce3cb29311e9b3485211f Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:11:35 +0000 Subject: [PATCH 19/38] Defined numerics hash in #hundred_to_word --- lib/solution.rb | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index be24d59..e62fdb9 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,11 +1,34 @@ # 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) - tens = { - - } - ones = { - + numerics = { + 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" } h = number / 100 From b266fcec4f615fcc569c7ebf7815b79af6dd395c Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:22:21 +0000 Subject: [PATCH 20/38] Finished defining #hundred_to_words --- lib/solution.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index e62fdb9..26d09bc 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,6 +1,8 @@ # 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) + result = "" + numerics = { 1 => "one", 2 => "two", @@ -30,11 +32,19 @@ def hundred_to_word(number) 80 => "eighty", 90 => "ninety" } - + h = number / 100 - t = (number / 100) % 10 - o = number % 10 + result += "#{numerics[h] unless h == 0} hundred " + + if numerics.keys.include?(number % 100) + result += "#{nummerics[h]}" + else + t = (number % 100) / 10 + o = number % 10 + result += "#{numerics[t] unless t == 0} #{numerics[o] unless o == 0}" + end + result end # The int_to_word method accepts an integer above 0 and below 1 billion From 21411cf328143cd7d9c1ca64f01ce2d094748d7c Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:22:54 +0000 Subject: [PATCH 21/38] Syntax correction in numerics hash --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index 26d09bc..6a5ce56 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -13,7 +13,7 @@ def hundred_to_word(number) 7 => "seven", 8 => "eight", 9 => "nine", - 10 => "ten" + 10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen", From b3976f5c0b8fc7d1b98325c69d55e3c5f2e85546 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:24:09 +0000 Subject: [PATCH 22/38] Added #strip to #hundred_to_word result --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index 6a5ce56..f224845 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -44,7 +44,7 @@ def hundred_to_word(number) result += "#{numerics[t] unless t == 0} #{numerics[o] unless o == 0}" end - result + result.trim end # The int_to_word method accepts an integer above 0 and below 1 billion From e98bbbc0934884cb16d5cc14dbd283d06e8360bd Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:24:17 +0000 Subject: [PATCH 23/38] Added #strip to #hundred_to_word result --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index f224845..701f30f 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -44,7 +44,7 @@ def hundred_to_word(number) result += "#{numerics[t] unless t == 0} #{numerics[o] unless o == 0}" end - result.trim + result.strip end # The int_to_word method accepts an integer above 0 and below 1 billion From 3e7cc370e08b6d0aefdbfba7bdfdd98fec3cbf7a Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:25:47 +0000 Subject: [PATCH 24/38] Placed unless outside of string interpolation --- lib/solution.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index 701f30f..7e34650 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -34,10 +34,10 @@ def hundred_to_word(number) } h = number / 100 - result += "#{numerics[h] unless h == 0} hundred " + result += "#{numerics[h]} hundred " unless h == 0 if numerics.keys.include?(number % 100) - result += "#{nummerics[h]}" + result += "#{numerics[h]}" else t = (number % 100) / 10 o = number % 10 From 7c80f0590f563eb37224ec82b203f6da646e7587 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:32:58 +0000 Subject: [PATCH 25/38] Corrected incorrect reference to hundreth instead of tenth --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index 7e34650..b73cbf9 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -37,7 +37,7 @@ def hundred_to_word(number) result += "#{numerics[h]} hundred " unless h == 0 if numerics.keys.include?(number % 100) - result += "#{numerics[h]}" + result += "#{numerics[number % 100]}" else t = (number % 100) / 10 o = number % 10 From bb8cc2c0f7af75770371ee281f537c95481e45be Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:33:51 +0000 Subject: [PATCH 26/38] Multiplying tenth digit by 10 when referencing --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index b73cbf9..d579314 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -39,7 +39,7 @@ def hundred_to_word(number) if numerics.keys.include?(number % 100) result += "#{numerics[number % 100]}" else - t = (number % 100) / 10 + t = ((number % 100) / 10) * 10 o = number % 10 result += "#{numerics[t] unless t == 0} #{numerics[o] unless o == 0}" end From 1b4f80e69f54c5f20fab7e7ca60a7ff37f699c0f Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 19:35:47 +0000 Subject: [PATCH 27/38] Added result string assembly and return --- lib/solution.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index d579314..4f37910 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -50,7 +50,8 @@ def hundred_to_word(number) # 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) - + result = "" + # Dividing the number by a million returns the three digits containing millions million = number / 1000000 @@ -63,4 +64,9 @@ def int_to_word(number) # the hundred values hundred = number % 1000 + result += "#{hundred_to_word(million)} million " unless million == 0 + result += "#{hundred_to_word(thousand)} thousand " unless thousand == 0 + result += "#{hundred_to_word(hundred)}" unless hundred == 0 + + result.strip end From eafc3f3f96b344363ef72b57be304ef5ed751e6c Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 20:26:01 +0000 Subject: [PATCH 28/38] DRY corrections --- lib/solution.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index 4f37910..2e80c4d 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -36,12 +36,12 @@ def hundred_to_word(number) h = number / 100 result += "#{numerics[h]} hundred " unless h == 0 - if numerics.keys.include?(number % 100) - result += "#{numerics[number % 100]}" + result += if numerics.keys.include?(number % 100) + "#{numerics[number % 100]}" else - t = ((number % 100) / 10) * 10 - o = number % 10 - result += "#{numerics[t] unless t == 0} #{numerics[o] unless o == 0}" + tens = ((number % 100) / 10) * 10 + ones = number % 10 + "#{numerics[tens] unless tens == 0} #{numerics[ones] unless ones == 0}" end result.strip From 429282cc07f6e47022d7df29d5685015c32eaeeb Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 20:30:51 +0000 Subject: [PATCH 29/38] Reduced the numerics hash spacing --- lib/solution.rb | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index 2e80c4d..be186a6 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,38 +1,17 @@ # 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) - result = "" - numerics = { - 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" + 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" } + result = "" + h = number / 100 result += "#{numerics[h]} hundred " unless h == 0 From 82ae5bd88da69535815ca773144654b2c0e78c2a Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 20:33:23 +0000 Subject: [PATCH 30/38] Refactored result generation as part of hundred addition --- lib/solution.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index be186a6..0986b6a 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -10,10 +10,7 @@ def hundred_to_word(number) 70 => "seventy", 80 => "eighty", 90 => "ninety" } - result = "" - - h = number / 100 - result += "#{numerics[h]} hundred " unless h == 0 + result = number/100 != 0 ? "#{numerics[number / 100]} hundred " : "" result += if numerics.keys.include?(number % 100) "#{numerics[number % 100]}" From 7d05966fc3fb64eae25ad7ddbaf9c8204315ad08 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 20:39:03 +0000 Subject: [PATCH 31/38] Improved documentation on #int_to_word --- lib/solution.rb | 11 ++++++++--- spec/solution_spec.rb | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index 0986b6a..d57036c 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -26,8 +26,6 @@ def hundred_to_word(number) # 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) - result = "" - # Dividing the number by a million returns the three digits containing millions million = number / 1000000 @@ -40,9 +38,16 @@ def int_to_word(number) # the hundred values hundred = number % 1000 - result += "#{hundred_to_word(million)} million " unless million == 0 + # 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 diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index fd40b51..8fc81d9 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -18,6 +18,7 @@ end + describe "#int_to_word" do it "returns 4527 as four thousand five hundred twenty seven" do From 94e12b397304c4d84bd005504e492e108edc6fa7 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 20:47:17 +0000 Subject: [PATCH 32/38] Improved documentation on #hundred_to_word --- lib/solution.rb | 34 ++++++++++++++++++++++++++++++---- spec/solution_spec.rb | 2 +- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index d57036c..b42f392 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,6 +1,8 @@ # 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 numerics = { 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten", 11 => "eleven", @@ -10,16 +12,40 @@ def hundred_to_word(number) 70 => "seventy", 80 => "eighty", 90 => "ninety" } - result = number/100 != 0 ? "#{numerics[number / 100]} hundred " : "" + # Divide the number by 100 to receive a single digit representing the + # hundreth value. + hundred = number / 100 - result += if numerics.keys.include?(number % 100) - "#{numerics[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 ? "#{numerics[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. + result += if numerics.keys.include?(tens) + "#{numerics[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 - tens = ((number % 100) / 10) * 10 + # 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 them if + # either value is equal to 0 "#{numerics[tens] unless tens == 0} #{numerics[ones] unless ones == 0}" end + # Return the result string after calling the #strip method which removes + # leading and trailing white spaces result.strip end diff --git a/spec/solution_spec.rb b/spec/solution_spec.rb index 8fc81d9..373ec24 100644 --- a/spec/solution_spec.rb +++ b/spec/solution_spec.rb @@ -18,7 +18,7 @@ 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 From 4e4e6a737f5424f2ceb90aadc3802dd1a9979641 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 20:47:56 +0000 Subject: [PATCH 33/38] Removed the 'unless tens == 0' check as it will never occur --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index b42f392..9d72869 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -41,7 +41,7 @@ def hundred_to_word(number) # Append the composite translation of tens and ones to and ommit them if # either value is equal to 0 - "#{numerics[tens] unless tens == 0} #{numerics[ones] unless ones == 0}" + "#{numerics[tens] unless tens == 0} #{numerics[ones]}" end # Return the result string after calling the #strip method which removes From 32c4297be07c1e3194e0640bc5aab96b1335fee2 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 21:53:55 +0000 Subject: [PATCH 34/38] Removed DRY option of result --- lib/solution.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index 9d72869..8c0e52d 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -27,21 +27,22 @@ def hundred_to_word(number) # 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. - result += if numerics.keys.include?(tens) - "#{numerics[tens]}" + if numerics.keys.include?(tens) + result += "#{numerics[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 them if - # either value is equal to 0 - "#{numerics[tens] unless tens == 0} #{numerics[ones]}" + # Append the composite translation of tens and ones to and ommit the tens + # value if it is equal to 0 + result += "#{numerics[tens] unless tens == 0} #{numerics[ones]}" end # Return the result string after calling the #strip method which removes From 7c573948c28f3275534d9d072a73a7a49002162a Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 21:54:34 +0000 Subject: [PATCH 35/38] Added spaces for clarity --- lib/solution.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/solution.rb b/lib/solution.rb index 8c0e52d..fac7720 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -1,6 +1,7 @@ # 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 numerics = { @@ -53,6 +54,7 @@ def hundred_to_word(number) # 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 From 1d08d5860924d2a863449eee96a6befe2dec8c0b Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 21:55:04 +0000 Subject: [PATCH 36/38] Renamed numerics hash to numeric --- lib/solution.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/solution.rb b/lib/solution.rb index fac7720..356c482 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -4,7 +4,7 @@ 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 - numerics = { + 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", @@ -19,7 +19,7 @@ def hundred_to_word(number) # 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 ? "#{numerics[hundred]} hundred " : "" + 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. @@ -29,7 +29,7 @@ def hundred_to_word(number) # string. If the remaining numbers exists as a key within the numeric hash, # then append the translation to the result. if numerics.keys.include?(tens) - result += "#{numerics[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. @@ -43,7 +43,7 @@ def hundred_to_word(number) # Append the composite translation of tens and ones to and ommit the tens # value if it is equal to 0 - result += "#{numerics[tens] unless tens == 0} #{numerics[ones]}" + result += "#{numeric[tens] unless tens == 0} #{numerics[ones]}" end # Return the result string after calling the #strip method which removes From c20a10b6221db03021d6793be60af6937dec793c Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 21:55:35 +0000 Subject: [PATCH 37/38] Syntax corrections --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index 356c482..3da2b64 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -43,7 +43,7 @@ def hundred_to_word(number) # 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} #{numerics[ones]}" + result += "#{numeric[tens] unless tens == 0} #{numeric[ones]}" end # Return the result string after calling the #strip method which removes From 1091409eb042b8c970cda5ae78a3eb549a9fd5a8 Mon Sep 17 00:00:00 2001 From: Esteban Hernandez Date: Mon, 30 Nov 2015 21:56:01 +0000 Subject: [PATCH 38/38] Syntax corrections --- lib/solution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solution.rb b/lib/solution.rb index 3da2b64..b2ee689 100644 --- a/lib/solution.rb +++ b/lib/solution.rb @@ -28,7 +28,7 @@ def hundred_to_word(number) # 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 numerics.keys.include?(tens) + if numeric.keys.include?(tens) result += "#{numeric[tens]}" # If the value of tens is not present in the numeric.keys then decompose the