From 376becdbb3502672121bb1a62491db97ab335c20 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 27 Dec 2016 05:47:12 +0000 Subject: [PATCH 01/19] 00_hello done --- 00_hello/hello.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 00_hello/hello.rb diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..407219bd6 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(name="Stephen") + "Hello, #{name}!" +end From 647cd7cb4e851b13e7ec450769bfde6fa8ba3efb Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 27 Dec 2016 05:51:33 +0000 Subject: [PATCH 02/19] 01_temperature done --- 01_temperature/temperature.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 01_temperature/temperature.rb diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..77e0204f4 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f_temp) + ((f_temp - 32) * 5)/9 +end + +def ctof(c_temp) + ((c_temp * 9.0)/5.0)+32.0 +end From a1c792d6aa20f9da15c142f54d288375d7195211 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 27 Dec 2016 05:52:17 +0000 Subject: [PATCH 03/19] 01_temperature done --- 01_temperature/temperature.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb index 77e0204f4..1095a6223 100644 --- a/01_temperature/temperature.rb +++ b/01_temperature/temperature.rb @@ -1,5 +1,5 @@ def ftoc(f_temp) - ((f_temp - 32) * 5)/9 + ((f_temp - 32.0) * 5.0)/9.0 end def ctof(c_temp) From 5a57a8ecfd958533e7f6c0ec431f689e7e94110a Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 27 Dec 2016 06:10:35 +0000 Subject: [PATCH 04/19] calculator done --- 02_calculator/calculator.rb | 38 ++++++++++++++++++++++++++++++++ 02_calculator/calculator_spec.rb | 38 ++++++++++++++++++++++++-------- 2 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 02_calculator/calculator.rb diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..f152af6ab --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,38 @@ +def add(a, b) + a + b +end + +def subtract(a, b) + a - b +end + +def sum(nums_to_sum) + total = 0 + nums_to_sum.each do |num| + total += num + end + + return total +end + +def multiply(nums_to_multiply=[]) + total = 1 + nums_to_multiply.each do |num| + total *= num + end + + return total +end + +def power(a, b) + a**b +end + +def factorial(num) + total = 1 + 2.upto(num) do |product| + total *= product + end + + return total +end diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..f8ce2f497 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -79,21 +79,41 @@ describe "#multiply" do - it "multiplies two numbers" + it "multiplies two numbers" do + expect(multiply([2,3])).to eq(6) + end + + it "multiplies several numbers" do + expect(multiply([2,6,3,28])).to eq(1008) + end - it "multiplies several numbers" - end describe "#power" do - it "raises one number to the power of another number" + it "raises one number to the power of another number" do + expect(power(5,8)).to eq(390625) + end end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" - it "computes the factorial of 1" - it "computes the factorial of 2" - it "computes the factorial of 5" - it "computes the factorial of 10" + it "computes the factorial of 0" do + expect(factorial(0)).to eq(1) + end + + it "computes the factorial of 1" do + expect(factorial(1)).to eq(1) + end + + it "computes the factorial of 2" do + expect(factorial(2)).to eq(2) + end + + it "computes the factorial of 5" do + expect(factorial(5)).to eq(120) + end + + it "computes the factorial of 10" do + expect(factorial(10)).to eq(3628800) + end end From 0812987830edc87b30df1b2df01033b33806d27b Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 27 Dec 2016 06:46:28 +0000 Subject: [PATCH 05/19] 03_simon_says_done --- 03_simon_says/simon_says.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 03_simon_says/simon_says.rb diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..96d9c220f --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,33 @@ +def echo(statement) + statement +end + +def shout(statement) + statement.upcase +end + +def repeat(input, n=2) + ([input] * n).join(' ') +end + +def start_of_word(word, num) + word.slice(0..(num - 1)) +end + +def first_word(input) + input.split(' ')[0] +end + +def titleize(input) + captilized = [] + little_words = ['and', 'the', 'over'] + input.split(' ').each_with_index do |word, ind| + if ind != 0 && little_words.include?(word) + captilized.push(word) + else + captilized.push(word.capitalize) + end + end + + captilized.join(' ').to_s +end From 3968ad44a87fc18633bed7b58612fae0d5c9e474 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 27 Dec 2016 08:08:05 +0000 Subject: [PATCH 06/19] 04 pig_latin done --- 04_pig_latin/pig_latin.rb | 19 +++++++++++++++++++ 04_pig_latin/pig_latin_spec.rb | 5 +++++ 2 files changed, 24 insertions(+) create mode 100644 04_pig_latin/pig_latin.rb diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..aed21b123 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,19 @@ +def translate(input) + must_capitalize = true if input.capitalize == input + vowels = %w(a e i o u) + words_to_translate = input.split(' ') + translated_word = [] + words_to_translate.each do |word| + if vowels.include?(word[0]) + translated_word << word + 'ay' + else + ind = 0 + ind += 1 until vowels.include?(word[ind]) && (word[ind-1] + word[ind] != 'qu') + + translated_word << word.slice(ind...word.size) + word.slice(0...ind) + 'ay' + end + end + + return translated_word.join(' ').capitalize if must_capitalize + return translated_word.join(' ') +end diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index cc659edfd..4ea087a98 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -67,6 +67,11 @@ # Test-driving bonus: # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course) + it "must retain captilization after translation" do + s = translate("The quick brown fox") + expect(s).to eq("Ethay ickquay ownbray oxfay") + end + # * retain the punctuation from the original phrase end From 47947141d72b04a474f11ce47fba15c085fe72d6 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Wed, 28 Dec 2016 21:46:26 +0000 Subject: [PATCH 07/19] performance monitor done --- 05_silly_blocks/silly_blocks.rb | 13 +++++++++++++ 06_performance_monitor/performance_monitor.rb | 10 ++++++++++ 2 files changed, 23 insertions(+) create mode 100644 05_silly_blocks/silly_blocks.rb create mode 100644 06_performance_monitor/performance_monitor.rb diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..dce329aae --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,13 @@ +def reverser + yield.split.map { |word| word.reverse}.join(" ") +end + +def adder(num=1) + yield + num +end + +def repeater(loops=1) + loops.times do + yield + end +end diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..0275d953f --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,10 @@ +def measure(num_times = 1) + avg_time = [] + num_times.times do + beginning_time = Time.now + yield + avg_time << (Time.now - beginning_time) + end + + return avg_time.inject{ |sum, el| sum + el }.to_f / avg_time.size +end From 8d0c74822d6dc68c3808c7d318ad0d2ec142e8bd Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Wed, 28 Dec 2016 21:48:11 +0000 Subject: [PATCH 08/19] performance_monitor complete --- 06_performance_monitor/performance_monitor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb index 0275d953f..c3f545dc0 100644 --- a/06_performance_monitor/performance_monitor.rb +++ b/06_performance_monitor/performance_monitor.rb @@ -6,5 +6,5 @@ def measure(num_times = 1) avg_time << (Time.now - beginning_time) end - return avg_time.inject{ |sum, el| sum + el }.to_f / avg_time.size + return avg_time.inject{ |total_time, bench_time| total_time + bench_time }.to_f / avg_time.size end From 6a839f003e4c6ee778ddae40bfe57c1ed989c7a0 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 03:38:46 +0000 Subject: [PATCH 09/19] 07 Hello Friend complete --- 07_hello_friend/friend.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 07_hello_friend/friend.rb diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..858650aac --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,6 @@ +class Friend + def greeting(name=nil) + return "Hello, #{name}!" if name + "Hello!" + end +end From 69291b9d01885182d69cb4d5eb9dcbe7d0088d89 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 04:07:37 +0000 Subject: [PATCH 10/19] Done with 08_book_titles --- 08_book_titles/book.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 08_book_titles/book.rb diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..790b84f35 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,25 @@ +class Book + def initialize(title = nil) + @title = title + @exclusion_words = %w(the a an and in of) + end + + def title + capitalize_each(@title) + end + + def title=(title) + capitalize_each(@title = title) + end + + def capitalize_each(title) + capitalized_title = title.split.each_with_index.map do |v, i| + if i == 0 || !@exclusion_words.include?(v) + v.capitalize + else + v + end + end + capitalized_title.join(' ') + end +end From 3b6909a2e87bec8ad16806ab4e2931f41959d5ff Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 04:21:21 +0000 Subject: [PATCH 11/19] 09 timer done --- 09_timer/timer.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 09_timer/timer.rb diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..2fc6bf910 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,11 @@ +class Timer + attr_accessor :seconds + + def initialize + self.seconds = 0 + end + + def time_string + Time.at(self.seconds).utc.strftime("%H:%M:%S") + end +end From 53c8e8f48445c6874f5af90694e4820f3eb79b09 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 05:22:53 +0000 Subject: [PATCH 12/19] 10 temperature object done --- 10_temperature_object/temperature.rb | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 10_temperature_object/temperature.rb diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..3b391929f --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,63 @@ +class Temperature + attr_accessor :temp + + def initialize(options={}) + @temp = options + end + + def in_fahrenheit + if temp[:f] + return temp[:f] + else + return ((temp[:c] * 9.0)/5.0)+32.0 + end + end + + def in_celsius + if temp[:c] + return temp[:c] + else + return ((temp[:f] - 32.0) * 5.0)/9.0 + end + end + + def self.from_celsius(temp) + return Temperature.new(:c => temp) + end + + def self.from_fahrenheit(temp) + return Temperature.new(:f => temp) + end +end + +class Celsius < Temperature + attr_accessor :temp + + def initilize(temp) + @temp = temp + end + + def in_celsius + return temp + end + + def in_fahrenheit + return ((temp * 9.0)/5.0)+32.0 + end +end + +class Fahrenheit < Temperature + attr_accessor :temp + + def initilize(temp) + @temp = temp + end + + def in_celsius + return ((temp - 32.0) * 5.0)/9.0 + end + + def in_fahrenheit + return temp + end +end From c76c184787a29dc8aa5a5e07a2b4de7660dd3c02 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 06:03:34 +0000 Subject: [PATCH 13/19] 11 dictionary done --- 11_dictionary/dictionary.rb | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 11_dictionary/dictionary.rb diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..d89b15e65 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,38 @@ +class Dictionary + attr_accessor :entries + + def initialize (entries = {}) + @entries = entries + end + + def add(words={}) + if words.is_a? String + entries[words] = nil + else + words.each_pair do |word, definition| + entries[word] = definition + end + end + end + + def keywords + entries.keys.sort + end + + def include?(word) + entries.has_key?(word) + end + + def find(word) + return entries.select{|k,v| k.start_with? word} || {} + end + + def printable + printable_list = "" + entries.sort.each do |value| + printable_list << "[#{value[0]}] \"#{value[1]}\"\n" + end + + printable_list.strip + end +end From 786d676acb38e4241cda8812f1dd1eddeee88cc9 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 06:54:46 +0000 Subject: [PATCH 14/19] 12 rpn calculator done --- 12_rpn_calculator/rpn_calculator.rb | 87 +++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 12_rpn_calculator/rpn_calculator.rb diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..aaa4a7bce --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,87 @@ +class RPNCalculator + attr_accessor :stack + + def initialize + @stack = [] + end + + def push(num) + @stack.push(num) + end + + def plus + empty_array? + b, a = populate_numbers + result = a + b + @stack.push(result) + + nil + end + + def minus + empty_array? + b, a = populate_numbers + result = a - b + @stack.push(result) + + nil + end + + def times + empty_array? + b, a = populate_numbers + result = a * b + @stack.push(result) + + nil + end + + def divide + empty_array? + b, a = populate_numbers + result = a.to_f / b.to_f + @stack.push(result) + + nil + end + + def empty_array? + raise 'calculator is empty' if @stack == [] + end + + def populate_numbers + b = stack.pop + a = stack.pop + [b, a] + end + + def tokens(str) + str.split.map! { |t| t[/\d/] ? t.to_i : t.to_sym } + end + + def evaluate(str) + temp_array = str.split + temp_array.each do |t| + if t[/\d/] + @stack.push(t.to_i) + else + case t + when '*' + self.times + when '/' + self.divide + when '+' + self.plus + when '-' + self.minus + end + end + end + + value + end + + def value + @stack[-1] + end +end From 8a77bd7ad6e8fa6e7221f9d3df8fbbd6d3e68d03 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 07:44:55 +0000 Subject: [PATCH 15/19] 13 xml_document done --- 13_xml_document/xml_document.rb | 84 +++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 13_xml_document/xml_document.rb diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb new file mode 100644 index 000000000..172b19149 --- /dev/null +++ b/13_xml_document/xml_document.rb @@ -0,0 +1,84 @@ +class XmlDocument + attr_accessor :indent, :indent_count + + def initialize(indent = false) + @indent = indent + @indent_count = 0 + end + + def hello(options = {}) + builder = '' + builder = " name='#{options[:name]}'" if options[:name] + + if block_given? + built_xml = '' + if @indent + indent_space = increase_indent + built_xml = "#{indent_space}\n#{yield}#{indent_space}\n" + else + built_xml = "#{yield}" + end + + return built_xml + end + + "" + end + + def goodbye + builder = '' + if block_given? + built_xml = '' + if @indent + indent_space = increase_indent + built_xml = "#{indent_space}\n#{yield}#{indent_space}\n" + else + built_xml = "#{yield}" + end + + return built_xml + end + + "" + end + + def come_back + builder = '' + if block_given? + built_xml = '' + if @indent + indent_space = increase_indent + built_xml = "#{indent_space}\n#{yield}#{indent_space}\n" + else + built_xml = "#{yield}" + end + + return built_xml + end + + "" + end + + def ok_fine(options = {}) + builder = '' + builder = " be='#{options[:be]}'" if options[:be] + return "#{yield}" if block_given? + + if @indent + indent_space = increase_indent + return "#{indent_space}\n" + else + return "" + end + end + + def increase_indent + indent_space = ' ' * @indent_count + @indent_count = 1 + @indent_count + return indent_space + end + + def send(tag_name) + "<#{tag_name}/>" + end +end From 7c79c472ed9e82c9690405cd6f112e7b75c1608c Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 07:53:07 +0000 Subject: [PATCH 16/19] code changes --- 13_xml_document/xml_document.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb index 172b19149..cb59d0207 100644 --- a/13_xml_document/xml_document.rb +++ b/13_xml_document/xml_document.rb @@ -6,7 +6,7 @@ def initialize(indent = false) @indent_count = 0 end - def hello(options = {}) + def hello(options = {}, &block) builder = '' builder = " name='#{options[:name]}'" if options[:name] From 090845fbf2c6dc3bc04561948d2961d91064ff31 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 08:03:02 +0000 Subject: [PATCH 17/19] cleaned up xml_doc code --- 13_xml_document/xml_document.rb | 66 +++++++++++---------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb index cb59d0207..7891da45e 100644 --- a/13_xml_document/xml_document.rb +++ b/13_xml_document/xml_document.rb @@ -7,62 +7,28 @@ def initialize(indent = false) end def hello(options = {}, &block) - builder = '' builder = " name='#{options[:name]}'" if options[:name] - if block_given? - built_xml = '' - if @indent - indent_space = increase_indent - built_xml = "#{indent_space}\n#{yield}#{indent_space}\n" - else - built_xml = "#{yield}" - end - - return built_xml - end + return xml_builder('hello', &block) if block_given? "" end - def goodbye - builder = '' - if block_given? - built_xml = '' - if @indent - indent_space = increase_indent - built_xml = "#{indent_space}\n#{yield}#{indent_space}\n" - else - built_xml = "#{yield}" - end - - return built_xml - end + def goodbye(_options = {}, &block) + return xml_builder('goodbye', &block) if block_given? - "" + "" end - def come_back - builder = '' - if block_given? - built_xml = '' - if @indent - indent_space = increase_indent - built_xml = "#{indent_space}\n#{yield}#{indent_space}\n" - else - built_xml = "#{yield}" - end + def come_back(_options = {}, &block) + return xml_builder('come_back', &block) if block_given? - return built_xml - end - - "" + "" end - def ok_fine(options = {}) - builder = '' + def ok_fine(options = {}, &block) builder = " be='#{options[:be]}'" if options[:be] - return "#{yield}" if block_given? + return xml_builder('ok_fine', &block) if block_given? if @indent indent_space = increase_indent @@ -72,10 +38,22 @@ def ok_fine(options = {}) end end + def xml_builder(caller) + built_xml = '' + if @indent + indent_space = increase_indent + built_xml = "#{indent_space}<#{caller}>\n#{yield}#{indent_space}\n" + else + built_xml = "<#{caller}>#{yield}" + end + + built_xml + end + def increase_indent indent_space = ' ' * @indent_count @indent_count = 1 + @indent_count - return indent_space + indent_space end def send(tag_name) From 5efe67db861aaad9bf76bacda7615ea56495aeb5 Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 08:19:45 +0000 Subject: [PATCH 18/19] 14 array_extenstions done --- 13_xml_document/xml_document.rb | 4 ++-- 14_array_extensions/array_extensions.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 14_array_extensions/array_extensions.rb diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb index 7891da45e..9440b629b 100644 --- a/13_xml_document/xml_document.rb +++ b/13_xml_document/xml_document.rb @@ -17,13 +17,13 @@ def hello(options = {}, &block) def goodbye(_options = {}, &block) return xml_builder('goodbye', &block) if block_given? - "" + '' end def come_back(_options = {}, &block) return xml_builder('come_back', &block) if block_given? - "" + '' end def ok_fine(options = {}, &block) diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..d66363439 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,13 @@ +class Array + def sum + inject(0) { |sum, n| sum + n } + end + + def square + map { |x| x**2 } + end + + def square! + map! { |x| x**2 } + end +end From a8508e1e6ce880c737279d2c7c655fa7dfc42a1b Mon Sep 17 00:00:00 2001 From: Stephen Akuffo Date: Tue, 3 Jan 2017 08:36:19 +0000 Subject: [PATCH 19/19] done --- 15_in_words/in_words.rb | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 15_in_words/in_words.rb diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb new file mode 100644 index 000000000..db2c8be2c --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,61 @@ +class Integer + ONES = %w(#{nil} one two three four five six seven eight nine).freeze + TEENS = %w(ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen).freeze + TENS = %w(ten twenty thirty forty fifty sixty seventy eighty ninety).freeze + + def translate(number, str = '', last = false) + return str if number == 0 + if number / 100 > 0 + remainder = number % 100 + number /= 100 + str += ' ' + ONES[number] + ' hundred' + elsif number.between?(10, 19) + str += if last + ' ' + TEENS[number - 10] + else + ' ' + TEENS[number - 10] + end + return str + elsif number / 10 > 0 + remainder = number % 10 + number /= 10 + str += if last + ' ' + TENS[number - 1] + else + ' ' + TENS[number - 1] + end + else + str += ' ' + ONES[number] + remainder = 0 + end + translate(remainder, str, true) + end + + def in_words(str = '') + now_writing = self + return 'zero' if now_writing == 0 + case + when now_writing / 1_000_000_000_000 > 0 + to_write = now_writing % 1_000_000_000_000 + now_writing /= 1_000_000_000_000 + str += translate(now_writing) + ' trillion' + when now_writing / 1_000_000_000 > 0 + to_write = now_writing % 1_000_000_000 + now_writing /= 1_000_000_000 + str += translate(now_writing) + ' billion' + when now_writing / 1_000_000 > 0 + to_write = now_writing % 1_000_000 + now_writing /= 1_000_000 + str += translate(now_writing) + ' million' + when now_writing / 1000 > 0 + to_write = now_writing % 1000 + now_writing /= 1000 + str += translate(now_writing) + ' thousand' + else + str += translate(now_writing) + return str[1..-1] + end + return str[1..-1] if to_write == 0 + to_write.in_words(str) + end +end