From 7357dff4492461f004e67ce51dae91433ebd2195 Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 27 Oct 2016 16:54:45 -0500 Subject: [PATCH 01/15] Pig Latin WIP --- 00_hello/hello.rb | 7 ++ 01_temperature/temperature.rb | 7 ++ 02_calculator/calculator.rb | 24 ++++++ 02_calculator/calculator_spec.rb | 34 ++++++-- 03_simon_says/simon_says.rb | 40 +++++++++ 04_pig_latin/pig_latin.rb | 25 ++++++ 13_xml_document/index.html | 123 --------------------------- 13_xml_document/xml_document_spec.rb | 77 ----------------- 8 files changed, 128 insertions(+), 209 deletions(-) create mode 100644 00_hello/hello.rb create mode 100644 01_temperature/temperature.rb create mode 100644 02_calculator/calculator.rb create mode 100644 03_simon_says/simon_says.rb create mode 100644 04_pig_latin/pig_latin.rb delete mode 100644 13_xml_document/index.html delete mode 100644 13_xml_document/xml_document_spec.rb diff --git a/00_hello/hello.rb b/00_hello/hello.rb new file mode 100644 index 000000000..61626e810 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(name) + "Hello, #{name}!" +end diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb new file mode 100644 index 000000000..160742143 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f) + (f.to_f - 32.0) * 5.0/9.0 +end + +def ctof(c) + c.to_f * 9.0/5.0 + 32 +end diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb new file mode 100644 index 000000000..bb5cea694 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,24 @@ +def add(first, second) + first + second +end + +def subtract(first, second) + first - second +end + +def sum(array) + array.reduce(0) { |a, b| a + b } +end + +def multiply(array) + array.reduce { |a, b| a * b } +end + +def power(base, factor) + base ** factor +end + +def factorial(n) + return 1 if n == 0 + (1..n).to_a.reduce { |a, b| a * b } +end diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..31d5b2838 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -79,21 +79,37 @@ 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,3,4])).to eq(24) + 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(2,3)).to eq(8) + 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 diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..5b4ffebc7 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,40 @@ +def echo(text) + text +end + +def shout(text) + text.upcase +end + +def repeat(text, times=2) + text + ((" "+text) * (times-1)) +end + +def start_of_word(text, letters) + text[0..letters-1] +end + +def first_word(text) + array = text.split(' ') + array[0] +end + +def capitalize(word) + word[0] = word[0].upcase + word +end + +def titleize(text) + array = text.split(' ') + little_words = ['a', 'an', 'the', 'and', 'or', 'but', 'for', 'by', 'near', 'under', 'over', 'of'] + output = array.map.with_index(0) do |word, index| + if index == 0 + capitalize(word) + elsif little_words.include?(word) + word + else + capitalize(word) + end + end + output = output.join(' ') +end diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..b42fe352c --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,25 @@ +def translate_word(text) + vowels = ['a', 'e', 'i', 'o', 'u', 'y'] + if vowels.include?(text[0]) + output = text + 'ay' + else + array = text.split('') + array.length.times do + current_letter = array.shift + if vowels.include?(current_letter) + array.unshift(current_letter) + break + else + array.push(current_letter) + end + end + output = array.join('') + 'ay' + end + output +end + +def translate(text) + words = text.split(' ') + output = words.map { |word| translate_word(word) } + output.join(' ') +end diff --git a/13_xml_document/index.html b/13_xml_document/index.html deleted file mode 100644 index 900ff72d5..000000000 --- a/13_xml_document/index.html +++ /dev/null @@ -1,123 +0,0 @@ - - - Test-First Teaching: learn_ruby: xml_document - - - -
-

TestFirst.org

-

the home of test-first teaching

-
- -

xml_document

-

Topics

- -
    -
  • method_missing
  • -
  • blocks
  • -
  • strings
  • -
  • hashes
  • -
- - -

Builder

- -

Here we will implement the so-called Builder Pattern: we will make an object, XmlDocument, that builds an XML file, which can contain arbitrary tags.

-
-
-

Tests

-xml_document_spec.rb -
-# Stuff you'll learn:
-# method_missing
-# nested closures
-# "builder pattern"
-
-require "xml_document"
-
-describe XmlDocument do
-  before do
-    @xml = XmlDocument.new
-  end
-
-  it "renders an empty tag" do
-    expect(@xml.hello).to eq("<hello/>")
-  end
-
-  it "renders a tag with attributes" do
-    expect(@xml.hello(:name => 'dolly')).to eq("<hello name='dolly'/>")
-  end
-
-  it "renders a randomly named tag" do
-    tag_name = (1..8).map{|i| ('a'..'z').to_a[rand(26)]}.join
-    expect(@xml.send(tag_name)).to eq("<#{tag_name}/>")
-  end
-
-  it "renders block with text inside" do
-    expect(@xml.hello do
-      "dolly"
-    end).to eq("<hello>dolly</hello>")
-  end
-
-  it "nests one level" do
-    expect(@xml.hello do
-      @xml.goodbye
-    end).to eq("<hello><goodbye/></hello>")
-  end
-
-  it "nests several levels" do
-    xml = XmlDocument.new
-    expect(xml.hello do
-      xml.goodbye do
-        xml.come_back do
-          xml.ok_fine(:be => "that_way")
-        end
-      end
-    end).to eq("<hello><goodbye><come_back><ok_fine be='that_way'/></come_back></goodbye></hello>")
-  end
-
-  it "indents" do
-    @xml = XmlDocument.new(true)
-    expect(@xml.hello do
-      @xml.goodbye do
-        @xml.come_back do
-          @xml.ok_fine(:be => "that_way")
-        end
-      end
-    end).to eq(
-    "<hello>\n" +
-    "  <goodbye>\n" +
-    "    <come_back>\n" +
-    "      <ok_fine be='that_way'/>\n" +
-    "    </come_back>\n" +
-    "  </goodbye>\n" +
-    "</hello>\n")
-  end
-end
-
-
-
- - - diff --git a/13_xml_document/xml_document_spec.rb b/13_xml_document/xml_document_spec.rb deleted file mode 100644 index fa1995818..000000000 --- a/13_xml_document/xml_document_spec.rb +++ /dev/null @@ -1,77 +0,0 @@ -# # Topics -# -# * method_missing -# * blocks -# * strings -# * hashes -# -# # Builder -# -# Here we will implement the so-called Builder Pattern: we will make an object, XmlDocument, that *builds* an XML file, which can contain arbitrary tags. - -# Stuff you'll learn: -# method_missing -# nested closures -# "builder pattern" - -require "xml_document" - -describe XmlDocument do - before do - @xml = XmlDocument.new - end - - it "renders an empty tag" do - expect(@xml.hello).to eq("") - end - - it "renders a tag with attributes" do - expect(@xml.hello(:name => 'dolly')).to eq("") - end - - it "renders a randomly named tag" do - tag_name = (1..8).map{|i| ('a'..'z').to_a[rand(26)]}.join - expect(@xml.send(tag_name)).to eq("<#{tag_name}/>") - end - - it "renders block with text inside" do - expect(@xml.hello do - "dolly" - end).to eq("dolly") - end - - it "nests one level" do - expect(@xml.hello do - @xml.goodbye - end).to eq("") - end - - it "nests several levels" do - xml = XmlDocument.new - expect(xml.hello do - xml.goodbye do - xml.come_back do - xml.ok_fine(:be => "that_way") - end - end - end).to eq("") - end - - it "indents" do - @xml = XmlDocument.new(true) - expect(@xml.hello do - @xml.goodbye do - @xml.come_back do - @xml.ok_fine(:be => "that_way") - end - end - end).to eq( - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "\n") - end -end From 48084a5c7b6ec989a264be5c5ebadea63f397842 Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 27 Oct 2016 16:58:49 -0500 Subject: [PATCH 02/15] finished Pig Latin --- 04_pig_latin/pig_latin.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb index b42fe352c..f9138c700 100644 --- a/04_pig_latin/pig_latin.rb +++ b/04_pig_latin/pig_latin.rb @@ -9,6 +9,9 @@ def translate_word(text) if vowels.include?(current_letter) array.unshift(current_letter) break + elsif current_letter == 'q' + current_letter = current_letter + array.shift + array.push(current_letter) else array.push(current_letter) end From 91cc1c0b764238da622937c6c8eab5a44c182ad7 Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 27 Oct 2016 17:08:27 -0500 Subject: [PATCH 03/15] word_reverser --- 05_silly_blocks/silly_blocks.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 05_silly_blocks/silly_blocks.rb diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..55b09cfd8 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,8 @@ +def word_reverser + input = yield.split('') + output = [] + while input.length > 0 + output.push(input.pop) + end + output.join('') +end From e63f741db26089fcf9f852e854bc846a8756470c Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 27 Oct 2016 20:58:09 -0500 Subject: [PATCH 04/15] temperature.rb WIP --- 05_silly_blocks/silly_blocks.rb | 14 +++++++++ 06_performance_monitor/performance_monitor.rb | 11 +++++++ 07_hello_friend/friend.rb | 9 ++++++ 08_book_titles/book.rb | 29 +++++++++++++++++++ 09_timer/timer.rb | 21 ++++++++++++++ 10_temperature_object/temperature.rb | 11 +++++++ 6 files changed, 95 insertions(+) create mode 100644 06_performance_monitor/performance_monitor.rb create mode 100644 07_hello_friend/friend.rb create mode 100644 08_book_titles/book.rb create mode 100644 09_timer/timer.rb create mode 100644 10_temperature_object/temperature.rb diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb index 55b09cfd8..b2ae784ad 100644 --- a/05_silly_blocks/silly_blocks.rb +++ b/05_silly_blocks/silly_blocks.rb @@ -6,3 +6,17 @@ def word_reverser end output.join('') end + +def reverser + words = yield.split(' ') + output = words.map { |word| word_reverser { word } } + output.join(' ') +end + +def adder(addend=1) + yield + addend +end + +def repeater(multiplicand=1) + multiplicand.times { yield } +end diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..57f3b7600 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,11 @@ +def measure(count=1) + recorded_times = [] + count.times do + start = Time.now + yield + finish = Time.now + recorded_times.push(finish - start) + end + sum_of_times = recorded_times.reduce(0){|sum, x| sum + x} + sum_of_times / count +end diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb new file mode 100644 index 000000000..5b2147a57 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(name="") + if name.length == 0 + "Hello!" + else + "Hello, #{name}!" + end + end +end diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb new file mode 100644 index 000000000..dd3e6dca3 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,29 @@ +class Book + def title=(title) + @title = titleize(title) + end + def title + @title + end + + def capitalize(word) + word[0] = word[0].upcase + word + end + + def titleize(text) + array = text.split(' ') + little_words = ['a', 'an', 'the', 'and', 'or', 'but', 'for', 'by', 'near', 'under', 'over', 'of', 'in'] + output = array.map.with_index(0) do |word, index| + if index == 0 + capitalize(word) + elsif little_words.include?(word) + word + else + capitalize(word) + end + end + output = output.join(' ') + end + +end diff --git a/09_timer/timer.rb b/09_timer/timer.rb new file mode 100644 index 000000000..b847c46a5 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,21 @@ +class Timer + def initialize + @seconds = 0 + end + def seconds=(seconds=0) + @seconds = seconds + end + def seconds + @seconds + end + def time_string + minutes = @seconds / 60 + seconds = @seconds % 60 + hours = minutes / 60 + minutes = minutes % 60 + "#{format_time_division(hours)}:#{format_time_division(minutes)}:#{format_time_division(seconds)}" + end + def format_time_division(number) + number.to_s.rjust(2, '0') + end +end diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..6f6572e29 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,11 @@ +class Temperature + def initialize(options={}) + + end + def in_fahrenheit(c) + c.to_f * 9.0/5.0 + 32 + end + def in_celsius(f) + (f.to_f - 32.0) * 5.0/9.0 + end +end From 2b37c604f99a894ebb69206d61da62b0be3c1793 Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 27 Oct 2016 21:38:47 -0500 Subject: [PATCH 05/15] More temperature.rb WIP --- 10_temperature_object/temperature.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb index 6f6572e29..93afa93d3 100644 --- a/10_temperature_object/temperature.rb +++ b/10_temperature_object/temperature.rb @@ -1,11 +1,12 @@ class Temperature def initialize(options={}) - + @f = options[:f] ||= options[:fahrenheit] + @c = options[:c] ||= options[:celsius] end - def in_fahrenheit(c) - c.to_f * 9.0/5.0 + 32 + def in_fahrenheit + @f || @c.to_f * 9.0/5.0 + 32 end - def in_celsius(f) - (f.to_f - 32.0) * 5.0/9.0 + def in_celsius + @c || (@f.to_f - 32.0) * 5.0/9.0 end end From 7a4754823145e5dd860fbaaec96ad3cdba059772 Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 27 Oct 2016 21:53:27 -0500 Subject: [PATCH 06/15] Factory method --- 10_temperature_object/temperature.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb index 93afa93d3..5ceea743f 100644 --- a/10_temperature_object/temperature.rb +++ b/10_temperature_object/temperature.rb @@ -9,4 +9,10 @@ def in_fahrenheit def in_celsius @c || (@f.to_f - 32.0) * 5.0/9.0 end + def self.from_celsius(c) + Temperature.new({:c => c}) + end + def self.from_fahrenheit(f) + Temperature.new({:f => f}) + end end From 9fa4c6ffe85f8c6c241d731f56f823a930029f38 Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 27 Oct 2016 21:56:21 -0500 Subject: [PATCH 07/15] Subclasses --- 10_temperature_object/temperature.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb index 5ceea743f..a83671a00 100644 --- a/10_temperature_object/temperature.rb +++ b/10_temperature_object/temperature.rb @@ -16,3 +16,14 @@ def self.from_fahrenheit(f) Temperature.new({:f => f}) end end + +class Celsius < Temperature + def initialize(c) + @c = c + end +end +class Fahrenheit < Temperature + def initialize(f) + @f = f + end +end From a9da8d46249c4b2f292c4c7ceef63e3a93657a34 Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 27 Oct 2016 22:07:14 -0500 Subject: [PATCH 08/15] Refactor, adding ftoc and ctof --- 10_temperature_object/temperature.rb | 10 ++++++++-- 10_temperature_object/temperature_object_spec.rb | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb index a83671a00..528d0cbef 100644 --- a/10_temperature_object/temperature.rb +++ b/10_temperature_object/temperature.rb @@ -3,11 +3,17 @@ def initialize(options={}) @f = options[:f] ||= options[:fahrenheit] @c = options[:c] ||= options[:celsius] end + def self.ftoc(f) + (f.to_f - 32.0) * 5.0/9.0 + end + def self.ctof(c) + c.to_f * 9.0/5.0 + 32 + end def in_fahrenheit - @f || @c.to_f * 9.0/5.0 + 32 + @f || self.class.ctof(@c) end def in_celsius - @c || (@f.to_f - 32.0) * 5.0/9.0 + @c || self.class.ftoc(@f) end def self.from_celsius(c) Temperature.new({:c => c}) diff --git a/10_temperature_object/temperature_object_spec.rb b/10_temperature_object/temperature_object_spec.rb index 5c0af8685..98802560b 100644 --- a/10_temperature_object/temperature_object_spec.rb +++ b/10_temperature_object/temperature_object_spec.rb @@ -97,7 +97,12 @@ # run *all* the tests during your refactoring, to make sure you did it right # describe "utility class methods" do - + it "ftoc method" do + expect(Temperature.ftoc(32)).to eq(0.0) + end + it "ctof method" do + expect(Temperature.ftoc(212)).to eq(100.0) + end end # Here's another way to solve the problem! From 3832d07bb59c0d626e9c2e7ae6ab6a0783f3a023 Mon Sep 17 00:00:00 2001 From: James Harris Date: Fri, 28 Oct 2016 18:15:59 -0500 Subject: [PATCH 09/15] rpn_calculator --- 11_dictionary/dictionary.rb | 37 +++++++++++++++++++++++++++++ 12_rpn_calculator/rpn_calculator.rb | 33 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 11_dictionary/dictionary.rb create mode 100644 12_rpn_calculator/rpn_calculator.rb diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..f7a952390 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,37 @@ +class Dictionary + def initialize + @dict = {} + @keywords = [] + end + def entries + @dict + end + def add(pair) + if pair.class == Hash + @dict.merge!(pair) + @keywords.push(pair.keys[0]) + elsif + @dict.merge!(pair => nil) + @keywords.push(pair) + end + end + def keywords + @keywords.sort + end + def include?(query) + @keywords.include?(query) + end + def find(query) + @dict.select { |key, value| key.start_with? query} + end + def printable + @keywords.sort! + output = "" + @keywords.each do |keyword| + output += "[#{keyword}] \"#{@dict[keyword]}\"\n" + end + output = output.slice(0, output.length-1) # remove trailing \n + end +end + +# "" diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..d203c2887 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,33 @@ +class RPNCalculator + def initialize + @stack = [] + end + def push(n) + @stack.push(n.to_f) + end + def value + @stack[-1] + end + def operation + begin + a = @stack.pop + b = @stack.pop + yield(a,b) + rescue + raise Exception, "calculator is empty" + end + end + def plus + operation { |a,b| @stack.push(a + b) } + end + def minus + operation { |a,b| @stack.push(b - a) } + end + def times + operation { |a,b| @stack.push(a * b) } + end + def divide + operation { |a,b| @stack.push(b / a) } + end + +end From 6dc5a9ddffc480f97e971a9617adb9c3e7aa5947 Mon Sep 17 00:00:00 2001 From: James Harris Date: Sat, 29 Oct 2016 17:43:14 -0500 Subject: [PATCH 10/15] Recursive solution to in_words, doesn't work for 0 --- 14_array_extensions/array_extensions.rb | 11 +++++ 15_in_words/in_words.rb | 61 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 14_array_extensions/array_extensions.rb create mode 100644 15_in_words/in_words.rb diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..1c30761ed --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,11 @@ +class Array + def sum + self.reduce(0) { |a, b| a + b } + end + def square + self.map { |item| item * item } + end + def square! + self.map! { |item| item * item } + end +end diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb new file mode 100644 index 000000000..8072cea1e --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,61 @@ +class Fixnum + def in_words + string = self.to_s + number_of_digits = string.length + dictionary = { + 1000000 => "million", + 1000 => "thousand", + 100 => "hundred", + 90 => "ninety", + 80 => "eighty", + 70 => "seventy", + 60 => "sixty", + 50 => "fifty", + 40 => "forty", + 30 => "thirty", + 20 => "twenty", + 19 => "nineteen", + 18 => "eighteen", + 17 => "seventeen", + 16 => "sixteen", + 15 => "fifteen", + 14 => "fourteen", + 13 => "thirteen", + 12 => "twelve", + 11 => "eleven", + 10 => "ten", + 9 => "nine", + 8 => "eight", + 7 => "seven", + 6 => "six", + 5 => "five", + 4 => "four", + 3 => "three", + 2 => "two", + 1 => "one" + } + string = "" + if self == 0 + return "zero" + else + dictionary.each do |int, word| + if self/int > 0 + string += word + ' ' + (self/10).in_words + string.strip! + break + end + end + end + string + # if self > 20 + # tens = self / 10 * 10 + # ones = self % 10 + # first_digit = string[0].to_i + # "#{}" + # elsif self >= 100 + # hundreds = self / 100 * 100 + # thousands = self / 1000 * 1000 + # end + + end +end From 99e4691be353bb4757a92414efbac32856bafff8 Mon Sep 17 00:00:00 2001 From: James Harris Date: Sat, 29 Oct 2016 23:38:53 -0500 Subject: [PATCH 11/15] in_words successful 0 to 9 --- 15_in_words/in_words.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb index 8072cea1e..2daffb836 100644 --- a/15_in_words/in_words.rb +++ b/15_in_words/in_words.rb @@ -35,15 +35,16 @@ def in_words 1 => "one" } string = "" - if self == 0 - return "zero" - else - dictionary.each do |int, word| - if self/int > 0 - string += word + ' ' + (self/10).in_words - string.strip! - break - end + dictionary.each do |int, word| + if self/int > 0 && self/10 > 0 + string += word + ' ' + (self/10).in_words + string.strip! + break + elsif self == int + string = word + string.strip! + elsif self/10 == 0 && self == 0 + string = "zero" end end string From d70a7c66e94f063d14f42ed32e27a7d25b029395 Mon Sep 17 00:00:00 2001 From: James Harris Date: Sun, 30 Oct 2016 20:14:53 -0500 Subject: [PATCH 12/15] 0-99 --- 15_in_words/in_words.rb | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb index 2daffb836..0a68afb8e 100644 --- a/15_in_words/in_words.rb +++ b/15_in_words/in_words.rb @@ -1,3 +1,6 @@ +require 'pry' +require 'pry-byebug' + class Fixnum def in_words string = self.to_s @@ -34,29 +37,25 @@ def in_words 2 => "two", 1 => "one" } - string = "" + output = "" dictionary.each do |int, word| - if self/int > 0 && self/10 > 0 - string += word + ' ' + (self/10).in_words - string.strip! + if self/int > 0 && self/10 > 0 && self > 20 + one_less_digit = string[1..string.length].to_i + if one_less_digit == 0 + output += word + else + output += word + ' ' + one_less_digit.in_words + end + output.strip! break elsif self == int - string = word - string.strip! + output = word + output.strip! elsif self/10 == 0 && self == 0 - string = "zero" + output = "zero" end end - string - # if self > 20 - # tens = self / 10 * 10 - # ones = self % 10 - # first_digit = string[0].to_i - # "#{}" - # elsif self >= 100 - # hundreds = self / 100 * 100 - # thousands = self / 1000 * 1000 - # end + output end end From ac1b398011c6e03ea46dffaedef16c36392766ff Mon Sep 17 00:00:00 2001 From: James Harris Date: Sun, 30 Oct 2016 20:22:35 -0500 Subject: [PATCH 13/15] hundreds --- 15_in_words/in_words.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb index 0a68afb8e..9c187e218 100644 --- a/15_in_words/in_words.rb +++ b/15_in_words/in_words.rb @@ -3,8 +3,6 @@ class Fixnum def in_words - string = self.to_s - number_of_digits = string.length dictionary = { 1000000 => "million", 1000 => "thousand", @@ -37,10 +35,16 @@ def in_words 2 => "two", 1 => "one" } + string = self.to_s + one_less_digit = string[1..string.length].to_i + first_digit = string[0].to_i output = "" + if self >= 100 + output += first_digit.in_words + ' ' + #binding.pry + end dictionary.each do |int, word| if self/int > 0 && self/10 > 0 && self > 20 - one_less_digit = string[1..string.length].to_i if one_less_digit == 0 output += word else From 820cb4d7e8faa912ae4b79b31863aeaa0c0a8aaa Mon Sep 17 00:00:00 2001 From: James Harris Date: Sun, 30 Oct 2016 20:36:10 -0500 Subject: [PATCH 14/15] thousands --- 15_in_words/in_words.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb index 9c187e218..d946139d1 100644 --- a/15_in_words/in_words.rb +++ b/15_in_words/in_words.rb @@ -39,12 +39,21 @@ def in_words one_less_digit = string[1..string.length].to_i first_digit = string[0].to_i output = "" - if self >= 100 - output += first_digit.in_words + ' ' + if self >= 1000 + thousands = self / 1000 + remainder = self - thousands * 1000 + if remainder > 0 + output += thousands.in_words + ' thousand ' + (self - thousands*1000).in_words + else + output += thousands.in_words + ' thousand' + end #binding.pry + return output + elsif self >= 100 + output += first_digit.in_words + ' ' end - dictionary.each do |int, word| - if self/int > 0 && self/10 > 0 && self > 20 + dictionary.each do |match, word| + if self/match > 0 && self > 20 if one_less_digit == 0 output += word else @@ -52,7 +61,7 @@ def in_words end output.strip! break - elsif self == int + elsif self == match output = word output.strip! elsif self/10 == 0 && self == 0 @@ -60,6 +69,5 @@ def in_words end end output - end end From 3f46e1e37e6f5d1d539d9c190e92bee3efba1ec8 Mon Sep 17 00:00:00 2001 From: James Harris Date: Sun, 30 Oct 2016 20:48:51 -0500 Subject: [PATCH 15/15] in_words complete --- 15_in_words/in_words.rb | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb index d946139d1..326b39bb3 100644 --- a/15_in_words/in_words.rb +++ b/15_in_words/in_words.rb @@ -3,10 +3,30 @@ class Fixnum def in_words - dictionary = { + string = self.to_s + one_less_digit = string[1..string.length].to_i + first_digit = string[0].to_i + output = "" + orders_of_magnitude = { + 1000000000000 => "trillion", + 1000000000 => "billion", 1000000 => "million", 1000 => "thousand", 100 => "hundred", + } + orders_of_magnitude.each do |magnitude, word| + if self >= magnitude + quantity = self / magnitude + remainder = self - quantity * magnitude + if remainder > 0 + output += quantity.in_words + ' ' + word + ' ' + remainder.in_words + else + output += quantity.in_words + ' ' + word + end + return output + end + end + dictionary = { 90 => "ninety", 80 => "eighty", 70 => "seventy", @@ -35,23 +55,6 @@ def in_words 2 => "two", 1 => "one" } - string = self.to_s - one_less_digit = string[1..string.length].to_i - first_digit = string[0].to_i - output = "" - if self >= 1000 - thousands = self / 1000 - remainder = self - thousands * 1000 - if remainder > 0 - output += thousands.in_words + ' thousand ' + (self - thousands*1000).in_words - else - output += thousands.in_words + ' thousand' - end - #binding.pry - return output - elsif self >= 100 - output += first_digit.in_words + ' ' - end dictionary.each do |match, word| if self/match > 0 && self > 20 if one_less_digit == 0