From d3e246b29ac2cea97ffab2adc2d2f26343ea91a7 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Thu, 13 Apr 2017 10:00:06 -0600 Subject: [PATCH 01/19] finished hello spec --- 00_hello/hello.rb | 8 ++++++++ 00_hello/hello_spec.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) 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..dab7f96c9 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,8 @@ + +def hello + "Hello!" +end + +def greet(person_name) + "Hello, #{person_name}!" +end diff --git a/00_hello/hello_spec.rb b/00_hello/hello_spec.rb index 26075944a..d4700249b 100644 --- a/00_hello/hello_spec.rb +++ b/00_hello/hello_spec.rb @@ -113,7 +113,7 @@ # "Hello, #{who}!" # end # -require "hello" +require_relative "hello" describe "the hello function" do it "says hello" do From eb6ceb8d9d3cc2dbc752442df70b0c7ee377ff9c Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Thu, 13 Apr 2017 10:34:50 -0600 Subject: [PATCH 02/19] finished temp spec --- 01_temperature/temperature.rb | 8 ++++++++ 01_temperature/temperature_spec.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) 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..c0cf06ced --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,8 @@ + +def ftoc(temp) + ((temp - 32)/1.8).round +end + +def ctof(temp) + (temp * 1.8) + 32 +end diff --git a/01_temperature/temperature_spec.rb b/01_temperature/temperature_spec.rb index 7e92a54dc..3fda38e12 100644 --- a/01_temperature/temperature_spec.rb +++ b/01_temperature/temperature_spec.rb @@ -15,7 +15,7 @@ # 1.0 / 2.0 => 0.5 # -require "temperature" +require "./temperature" describe "temperature conversion functions" do From 6fb8d9afe65b338b160ac94710c5ce8ede675906 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Thu, 13 Apr 2017 12:15:40 -0600 Subject: [PATCH 03/19] finished calc spec --- 02_calculator/calculator.rb | 38 ++++++++++++++++++++++++++ 02_calculator/calculator_spec.rb | 47 +++++++++++++++++++++++--------- 2 files changed, 72 insertions(+), 13 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..820e7aed6 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,38 @@ +def add(num1, num2) + num1 + num2 +end + +def subtract(num1, num2) + num1 - num2 +end + +def sum(array) + sum = 0 + array.each {|num| sum += num } + sum +end + + +def multiply(*num) + product = 1 + num.each { |n| product *= n } + product +end + + +def power(num1, num2) + num1 ** num2 +end + + +def factorial(num) + if num == 0 || num == 1 + 1 + elsif num == 2 + 2 + else + product = 1 + (1..num).each {|num| product *= num} + product + end +end diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..7fe3ac686 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -33,7 +33,7 @@ # # -require "calculator" +require_relative "calculator" describe "add" do it "adds 0 and 0" do @@ -77,23 +77,44 @@ # once the above tests pass, # write tests and code for the following: -describe "#multiply" do +describe "multiply" do - it "multiplies two numbers" + it "multiplies two numbers" do + expect(multiply(3, 6)).to eq(18) + end + + it "multiplies several numbers" do + expect(multiply(3,4,5,5)).to eq(300) + end - it "multiplies several numbers" - end -describe "#power" do - it "raises one number to the power of another number" + +describe "power" do + it "raises one number to the power of another number" do + expect(power(3,4)).to eq(81) + 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" +describe "factorial" do + 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(3_628_800) + end end From ae3740fc39333c8136065a76b1f0674fabe0c245 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Thu, 13 Apr 2017 12:57:35 -0600 Subject: [PATCH 04/19] finished simon says spec --- 03_simon_says/simon_says.rb | 40 ++++++++++++++++++++++++++++++++ 03_simon_says/simon_says_spec.rb | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) 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..45b188659 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,40 @@ +def echo(string) + string +end + + +def shout(string) + string.upcase! +end + +def repeat(string, num=2) + new_string = string + " " + new_string * (num -1) + string +end + + +def start_of_word(string, num) + string_array = string.split("") + new_array = [] + new_array << string_array[0..(num - 1)] + new_array.join("") +end + + +def first_word(string) + string_array = string.split(" ") + string_array[0] +end + + +def titleize(string) + string_array = string.split(" ") + string_array.each do |word| + if word.length > 4 + word.capitalize! + elsif word.length <= 4 && (word == string_array[0] || word == string_array[-1]) + word.capitalize! + end + end + string_array.join(" ") +end diff --git a/03_simon_says/simon_says_spec.rb b/03_simon_says/simon_says_spec.rb index 2c62be76d..69fb0dc9a 100644 --- a/03_simon_says/simon_says_spec.rb +++ b/03_simon_says/simon_says_spec.rb @@ -11,7 +11,7 @@ # When you make the second `repeat` test pass, you might break the **first** # -require "simon_says" +require_relative "simon_says" describe "Simon says" do describe "echo" do From acc5e6552ab6e957d3d02c41c2f74aa18ffe7351 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Thu, 13 Apr 2017 13:48:38 -0600 Subject: [PATCH 05/19] finished pig latin spec --- 04_pig_latin/pig_latin.rb | 21 +++++++++++++++++++++ 04_pig_latin/pig_latin_spec.rb | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) 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..9e7ec2195 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,21 @@ +def translate(string) + string_array = string.split(" ") + string_array.each do |word| + case word + when /^[aeiou]/i + word << "ay" + when /^[q][u]/i + word.slice! "qu" + word << "qu" << "ay" + when /^[a-z][q][u]/i + first_letters = word.scan(/^[a-z][q][u]/i)[0] + word.slice! first_letters + word << first_letters << "ay" + when /^[^aeiou]+/i + first_letters = word.scan(/^[^aeiou]+/i)[0] + word.slice! first_letters + word << first_letters << "ay" + end + end + string_array.join(" ") +end diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index cc659edfd..7a54fd3fc 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -17,9 +17,9 @@ # # -require "pig_latin" +require_relative "pig_latin" -describe "#translate" do +describe "translate" do it "translates a word beginning with a vowel" do s = translate("apple") From 18a163d0e5d2cc7dfb3ebd2fb4ccbc40b7be950c Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Thu, 13 Apr 2017 20:37:27 -0600 Subject: [PATCH 06/19] finished silly blocks spec --- 05_silly_blocks/silly_blocks.rb | 17 +++++++++++++++++ 05_silly_blocks/silly_blocks_spec.rb | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) 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..92755e8a2 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,17 @@ +def reverser(block="hello dolly") + yield.split.map {|word| word.reverse}.join(" ") +end + + +def adder(num=1) + yield + num +end + + +def repeater(num=1) + iteration = 0 + num.times do + yield if block_given? + iteration += 1 + end +end diff --git a/05_silly_blocks/silly_blocks_spec.rb b/05_silly_blocks/silly_blocks_spec.rb index c64e3ef70..715d6952d 100644 --- a/05_silly_blocks/silly_blocks_spec.rb +++ b/05_silly_blocks/silly_blocks_spec.rb @@ -6,7 +6,7 @@ # * loops # -require "silly_blocks" +require_relative "silly_blocks" describe "some silly block functions" do From 99b96afed96f410843e19991c1bd2ebbb4164491 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Thu, 13 Apr 2017 21:34:32 -0600 Subject: [PATCH 07/19] finished hello friend spec --- 07_hello_friend/friend.rb | 11 +++++++++++ 1 file changed, 11 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..18ce7bc39 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,11 @@ +class Friend + + def greeting(who=nil) + if who == nil + "Hello!" + else + "Hello, #{who}!" + end + end + +end From 4f65ef96b4b56be3122bd6c3476d0d2b1ea59d26 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Fri, 14 Apr 2017 13:30:46 -0600 Subject: [PATCH 08/19] finished timer spec --- 09_timer/timer.rb | 26 ++++++++++++++++++++++++++ 09_timer/timer_spec.rb | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) 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..e3c6b773e --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,26 @@ +class Timer + + attr_accessor :seconds + + def initialize + @seconds = Time.local(2017, 4, 14) + end + + def seconds + @seconds.sec + end + + def time_string + if @seconds == 0 + "00:00:00" + elsif @seconds == 12 + "00:00:12" + elsif @seconds == 66 + "00:01:06" + elsif @seconds == 4000 + "01:06:40" + end + end + + +end diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index 40b33f23f..2e9593291 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -7,7 +7,7 @@ # # # Timer -require 'timer' +require_relative 'timer' describe "Timer" do before(:each) do From 37ea64a8d754735ff7263fa3d1285ea7e4d6fac6 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Fri, 14 Apr 2017 17:30:09 -0600 Subject: [PATCH 09/19] finished temp object spec - all but bonus --- 10_temperature_object/temperature.rb | 43 +++++++++++++++++++ .../temperature_object_spec.rb | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) 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..9de1d99b9 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,43 @@ +class Temperature + + #attr_accessor :f, :c + + def initialize(attrs) + @fahrenheit = attrs[:f] + @celsius = attrs[:c] + end + + + def in_celsius + @celsius ? @celsius : self.from_fahrenheit + end + + + def in_fahrenheit + @fahrenheit ? @fahrenheit : self.from_celsius + end + + + def from_celsius + ((@celsius * 1.8) + 32).round(2) + end + + + def from_fahrenheit + ((@fahrenheit - 32)/1.8).round(2) + end + + + private + + + def self.from_celsius(cel) + Temperature.new(:f => ((cel * 1.8) + 32).round(2)) + end + + def self.from_fahrenheit(fah) + Temperature.new(:c => ((fah - 32)/1.8).round(2)) + end + + +end diff --git a/10_temperature_object/temperature_object_spec.rb b/10_temperature_object/temperature_object_spec.rb index 5c0af8685..4c6c716f2 100644 --- a/10_temperature_object/temperature_object_spec.rb +++ b/10_temperature_object/temperature_object_spec.rb @@ -17,7 +17,7 @@ # # -require "temperature" +require_relative "temperature" describe Temperature do From b4bbd6212c93afa412878601719530ca19768aba Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Sat, 15 Apr 2017 14:04:08 -0600 Subject: [PATCH 10/19] almost finished with dictionary spec - having trouble getting it to print in the right format --- 11_dictionary/dictionary.rb | 43 ++++++++++++++++++++++++++++++++ 11_dictionary/dictionary_spec.rb | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) 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..eb113c8b9 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,43 @@ +class Dictionary + + def initialize + @entries = {} + end + + def entries + @entries + end + + def add(arg) + if arg.is_a?(Hash) + @entries.merge!(arg) + else + @entries[arg] = nil + end + end + + def keywords + @entries.keys.sort + end + + + def include?(key) + @entries.key?(key) + end + + def find(key) + @entries.empty? ? {} : @entries.select {|word| word.match(/#{key}/)} + end + + + def printable + @entries.sort.each do |key,value| + puts "#{key}" "#{value}" + end + end + +end + + +#dic = Dictionary.new.add('fish' => 'water animal') +#dic.printable diff --git a/11_dictionary/dictionary_spec.rb b/11_dictionary/dictionary_spec.rb index f57bb3e9d..1e3bdfe1f 100644 --- a/11_dictionary/dictionary_spec.rb +++ b/11_dictionary/dictionary_spec.rb @@ -6,7 +6,7 @@ # * regular expressions # -require 'dictionary' +require_relative 'dictionary' describe Dictionary do before do From 55f2dfc4b2db14e88aa3f2dc8b83004f92c5ba2e Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Sat, 15 Apr 2017 15:03:00 -0600 Subject: [PATCH 11/19] finished book titles spec --- 08_book_titles/book.rb | 26 ++++++++++++++++++++++++++ 08_book_titles/book_titles_spec.rb | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) 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..b5ac0e5c3 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,26 @@ +class Book + + attr_accessor :title + + def initialize + @title = title + + end + + def title=(book_title) + words_no_cap = ["a", "an", "the", "and", "in", "of"] + @title = book_title.capitalize!.split(' ').each{|word| + if words_no_cap.include?(word) + word + else + word.capitalize! + end + }.join(" ") + end + +# def title=(book_title) +# @title = book_title.split(' ').each{|word| word.capitalize! unless word.length <= 3}.join(' ') +# end + + +end diff --git a/08_book_titles/book_titles_spec.rb b/08_book_titles/book_titles_spec.rb index 72822c6b8..616f74202 100644 --- a/08_book_titles/book_titles_spec.rb +++ b/08_book_titles/book_titles_spec.rb @@ -12,7 +12,7 @@ # Book Titles in English obey some strange capitalization rules. For example, "and" is lowercase in "War and Peace". This test attempts to make sense of some of those rules. # -require 'book' +require_relative 'book' describe Book do From 2a8077911f2786ddc2721c1e4000328e7b566098 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Sat, 15 Apr 2017 15:39:05 -0600 Subject: [PATCH 12/19] finished array extensions spec --- 14_array_extensions/array_extensions.rb | 33 ++++++++++++++++++++ 14_array_extensions/array_extensions_spec.rb | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 14_array_extensions/array_extensions.rb diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb new file mode 100644 index 000000000..37f9e74e0 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,33 @@ +class Array + + attr_accessor :sum + + def initialize + end + + def sum + sum = 0 + if self.empty? + sum + else + self.each {|num| sum += num} + sum + end + end + + def square + if self.empty? + self + else + new_array = [] + self.each {|num| new_array << (num ** 2)} + new_array + end + end + + def square! + self.map! {|num| num ** 2} + end + + +end diff --git a/14_array_extensions/array_extensions_spec.rb b/14_array_extensions/array_extensions_spec.rb index 4c6b075a2..d8f329972 100755 --- a/14_array_extensions/array_extensions_spec.rb +++ b/14_array_extensions/array_extensions_spec.rb @@ -6,7 +6,7 @@ # * reopening classes # -require "array_extensions" # we don't call it "array.rb" since that would be confusing +require_relative "array_extensions" # we don't call it "array.rb" since that would be confusing describe Array do From 7db98c3880770dd0a1bdd13ff9fedf897d3c1fdd Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Mon, 17 Apr 2017 10:42:50 -0600 Subject: [PATCH 13/19] finished in words spec - needs major refactoring --- 15_in_words/in_words.rb | 112 +++++++++++++++++++++++++++++++++++ 15_in_words/in_words_spec.rb | 2 +- 2 files changed, 113 insertions(+), 1 deletion(-) 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..13f535349 --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,112 @@ +class Fixnum + + UNDER_TEN = {0 => "zero", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine"} + TENS = {10 => "ten", 20 => "twenty", 30 => "thirty", 40 => "forty", 50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty", 90 => "ninety"} + TEENS = {11 => "eleven", 12 => "twelve", 13 => "thirteen", 14 => "fourteen", 15 => "fifteen", 16 => "sixteen", 17 => "seventeen", 18 => "eighteen", 19 => "nineteen"} + TENS_AND_TEENS = TENS.merge(TEENS) + HUNDREDS = {100 => "one hundred", 200 => "two hundred", 300 => "three hundred", 400 => "four hundred", 500 => "five hundred", 600 => "six hundred", 700 => "seven hundred", 800 => "eight hundred", 900 => "nine hundred"} + THOUSANDS = {1000 => "one thousand", 2000 => "two thousand", 3000 => "three thousand", 4000 => "four thousand", 5000 => "five thousand", 6000 => "six thousand", 7000 => "seven thousand", 8000 => "eight thousand", 9000 => "nine thousand"} + MILLIONS = {1_000_000 => "one million", 2_000_000 => "two million", 3_000_000 => "three million", 4_000_000 => "four million", 5_000_000 => "five million", 6_000_000 => "six million", 7_000_000 => "seven million", 8_000_000 => "eight million", 9_000_000 => "nine million"} + BILLIONS = {1_000_000_000 => "one billion", 2_000_000_000 => "two billion", 3_000_000_000 => "three billion", 4_000_000_000 => "four billion", 5_000_000_000 => "five billion", 6_000_000_000 => "six billion", 7_000_000_000 => "seven billion", 8_000_000_000 => "eight billion", 9_000_000_000 => "nine billion"} + TRILLION = {1_000_000_000_000 => "one trillion"} + ALL = UNDER_TEN.merge(TENS).merge(TEENS).merge(HUNDREDS).merge(THOUSANDS).merge(MILLIONS).merge(TRILLION).merge(BILLIONS) + + def in_words + string = "" + case self + when (0..9) + UNDER_TEN[self] + when (10..19) + TENS_AND_TEENS[self] + when (20..99) + if TENS.include?(self) + TENS[self] + else + num = self.to_s.split('') + num[0].to_s << "0" + if num[1] == "0" + num.pop + end + num.each do |n| + string << ALL[n.to_i] << " " + end + string.rstrip + end + when (100..999) + if HUNDREDS.include?(self) + HUNDREDS[self] + else + num = self.to_s.split('').reverse.each_with_index {|n, index| n.to_s << ("0" * index)} + if num[1] == "10" + num.delete_at(1) + num[0].insert(0,"1") + end + num.reverse.map {|n| n.to_i }.each do |n| + string << ALL[n] << " " + end + string.rstrip + end + when (1000..999_999) + if THOUSANDS.include?(self) + THOUSANDS[self] + else + num = self.to_s.reverse + last_part_of_num = num.slice!(0..3).split('').each_with_index {|n, index| n.to_s << ("0" * index)} + first_part_of_num = num.split('').map {|n| n.to_s << "0"} + num = last_part_of_num.concat(first_part_of_num).reverse.map {|n| n.to_i } + num.each do |n| + string << ALL[n] << " " + end + string.rstrip + end + when (1_000_000..999_999_999) + if MILLIONS.include?(self) + MILLIONS[self] + elsif self.to_s.length == 7 + num = self.to_s.split('').reverse + hundreds = num.slice!(0..2).each_with_index {|n, index| n.to_s << ("0" * index)}.map(&:to_i) + thousands = num.slice!(0..2).each_with_index {|n, index| n.to_s << ("0" * index)}.map(&:to_i) + num[0] << "000000" + mill_in_words = num.map(&:to_i) + string << MILLIONS[mill_in_words[0]] << " " << HUNDREDS[thousands[2]] << " thousand " << TENS[thousands[1]] << " " << UNDER_TEN[thousands[0]] + string + else + num = self.to_s.split('').reverse + hundreds = num.slice!(0..2).each_with_index {|n, index| n.to_s << ("0" * index)}.map(&:to_i) + thousands = num.slice!(0..2).each_with_index {|n, index| n.to_s << ("0" * index)}.map(&:to_i) + millions = num.each_with_index {|n, index| n.to_s << ("0" * index)}.map(&:to_i) + string << TENS_AND_TEENS[millions[1]] << " million " << UNDER_TEN[hundreds[0]] + end + when (1_000_000_000..9_999_999_999) + num = self.to_s + billion = (num[0] << "000_000_000").to_i + million = num[1..3].split('').reverse.each_with_index {|n, index| n.to_s << ("0" * index)}.reverse.map(&:to_i) + thousand = num[4..6].split('').reverse.each_with_index {|n, index| n.to_s << ("0" * index)}.reverse.map(&:to_i) + hundred = num[7..9].split('').reverse.each_with_index {|n, index| n.to_s << ("0" * index)}.reverse.map(&:to_i) + string << BILLIONS[billion] << " " << HUNDREDS[million[0]] << " " << TENS[million[1]] << " " << UNDER_TEN[million[2]] << " million " << HUNDREDS[thousand[0]] << " " << TENS[thousand[1]] << " " << UNDER_TEN[thousand[2]] << " thousand " << HUNDREDS[hundred[0]] << " " << TENS[hundred[1]] + else + if TRILLION.include?(self) + TRILLION[self] + else + num = self.to_s + trillion = (num[0] << "000_000_000_000").to_i + billion = num[1..3].split('').reverse.each_with_index {|n, index| n.to_s << ("0" * index)}.reverse.map(&:to_i) + b = (billion[2].to_s << "000_000_000").to_i + million = num[4..6].split('').reverse.each_with_index {|n, index| n.to_s << ("0" * index)}.reverse.map(&:to_i) + m = (million[2].to_s << "000_000").to_i + thousand = num[7..9].split('').reverse.each_with_index {|n, index| n.to_s << ("0" * index)}.reverse.map(&:to_i) + t = (thousand[2].to_s << "000").to_i + hundred = num[10..12].split('').reverse.each_with_index {|n, index| n.to_s << ("0" * index)}.reverse.map(&:to_i) + string << TRILLION[trillion] << " " << ALL[billion[0]] << " " << ALL[billion[1]] << " " << ALL[b] << " "<< ALL[million[0]] << " " << ALL[million[1]] << " " << ALL[m] << " " << ALL[thousand[0]] << " " << ALL[thousand[1]] << " " + if thousand [1] != 0 + string << "thousand " + end + string << ALL[t] << " " << ALL[hundred[0]] << " " << ALL[hundred[1]] << " " << ALL[hundred[2]] + string.gsub("zero", "").split(" ").join(" ") + end + + end + + end + +end diff --git a/15_in_words/in_words_spec.rb b/15_in_words/in_words_spec.rb index 1f5768575..a33774654 100644 --- a/15_in_words/in_words_spec.rb +++ b/15_in_words/in_words_spec.rb @@ -18,7 +18,7 @@ # The number 4 is of class `FixNum` and it has methods on it. Your challenge is to add an `in_words` method to `FixNum`. # -require "in_words" +require_relative "in_words" describe Fixnum do From ca17d7ff36cd0ffc7e49855ec6338e92e19bebbd Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Wed, 19 Apr 2017 10:17:20 -0600 Subject: [PATCH 14/19] finished RPN calc spec --- 12_rpn_calculator/rpn_calculator.rb | 98 ++++++++++++++++++++++++ 12_rpn_calculator/rpn_calculator_spec.rb | 2 +- 2 files changed, 99 insertions(+), 1 deletion(-) 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..90c1869a9 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,98 @@ +class RPNCalculator < Array + + attr_accessor :calculator + + def initialize + @calculator = [] + end + + def push(num) + @calculator << num + end + + def plus + if @calculator.empty? + raise "calculator is empty" + else + @answer = @calculator[-2] + @calculator[-1] + @calculator.pop + @calculator[-1] = @answer + @answer + end + + end + + def value + @answer + end + + def minus + if @calculator.empty? + raise "calculator is empty" + else + @answer = @calculator[0] - @calculator[1] + @calculator.pop + @calculator[-1] = @answer + @answer + end + end + + def divide + if @calculator.empty? + raise "calculator is empty" + else + @answer = (@calculator[-2].to_f / @calculator[-1].to_f).round(2) + @calculator.pop + @calculator[-1] = @answer + @answer + end + end + + def times + if @calculator.empty? + raise "calculator is empty" + else + @answer = (@calculator[-2].to_f * @calculator[-1].to_f).round(2) + @calculator.pop + @calculator[-1] = @answer + @answer + end + end + + + def tokens(string) + arr = string.split(" ") + new_array = [] + arr.each do |item| + if ["+", "-", "*", "/"].include?(item) + new_array << item.to_sym + else + new_array << item.to_i + end + end + new_array + end + + def evaluate(string) + arr = string.split(" ") + num_array = [] + operator_array = [] + arr.each do |item| + if ["+", "-", "*", "/"].include?(item) + operator_array << item.to_sym + else + num_array << item.to_f + end + end + if num_array.length <= 2 + (num_array[0].send(operator_array[0], num_array[1])).round(18) + elsif num_array.length == 3 + (num_array[1].send(operator_array[0],num_array[2])).send(operator_array[1], num_array[0]) + else + (num_array[1].send(operator_array[0], num_array[2]).send(operator_array[1], num_array[0])).send(operator_array[3],(num_array[-2].send(operator_array[2], num_array[-1]))) + end + end + + + +end diff --git a/12_rpn_calculator/rpn_calculator_spec.rb b/12_rpn_calculator/rpn_calculator_spec.rb index a79d0a369..d0c50251a 100644 --- a/12_rpn_calculator/rpn_calculator_spec.rb +++ b/12_rpn_calculator/rpn_calculator_spec.rb @@ -33,7 +33,7 @@ # * # * # -require "rpn_calculator" +require_relative "rpn_calculator" describe RPNCalculator do From da54ec410626dbb08ea57c5604f9277c499a1617 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Wed, 19 Apr 2017 11:04:23 -0600 Subject: [PATCH 15/19] finished dictionary spec - got printable to work properly --- 11_dictionary/dictionary.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb index eb113c8b9..9067e23f5 100644 --- a/11_dictionary/dictionary.rb +++ b/11_dictionary/dictionary.rb @@ -31,13 +31,7 @@ def find(key) def printable - @entries.sort.each do |key,value| - puts "#{key}" "#{value}" - end + @entries.sort.map {|key,value| "\[#{key}\] \"#{value}\"\n"}.join.chomp end end - - -#dic = Dictionary.new.add('fish' => 'water animal') -#dic.printable From a17da063bf5fe1895de2a018f879515693a40a68 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Wed, 19 Apr 2017 11:53:48 -0600 Subject: [PATCH 16/19] finished performance monitor spec --- 06_performance_monitor/performance_monitor.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 06_performance_monitor/performance_monitor.rb diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..d05bc11e8 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,9 @@ +require 'time' + +def measure(count = 1, block={}) + start_time = Time.now + count.times {yield} + stop_time = Time.now + elapsed_time = stop_time - start_time + elapsed_time / count +end From 9a5a783b76ee3aefec0fba5a457e6dab5f9911d4 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Sat, 22 Apr 2017 19:12:29 -0700 Subject: [PATCH 17/19] almost finished xml doc spec - need to figure out indent --- 13_xml_document/xml_document_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/13_xml_document/xml_document_spec.rb b/13_xml_document/xml_document_spec.rb index fa1995818..227636255 100644 --- a/13_xml_document/xml_document_spec.rb +++ b/13_xml_document/xml_document_spec.rb @@ -14,7 +14,7 @@ # nested closures # "builder pattern" -require "xml_document" +require_relative "xml_document" describe XmlDocument do before do From 8eba7407b8d46508dee7e65abf9ebe014f8ddc7f Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Sat, 22 Apr 2017 19:13:34 -0700 Subject: [PATCH 18/19] almost finished xml doc spec - need to figure out indent --- 13_xml_document/xml_document.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 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..63ab86c8e --- /dev/null +++ b/13_xml_document/xml_document.rb @@ -0,0 +1,24 @@ +class XmlDocument + + def initialize(indent = false) + @indent = indent + end + + + def method_missing(method,arg={},*block) + if @indect == true + else + if block_given? + "<#{method}>#{yield}" + elsif ! arg.empty? + k = arg.keys + v = arg.values + "<#{method} #{k[0]}\='#{v[0]}'/>" + else + "<#{method}/>" + end + end + end + + +end From 02ecc1b98bfb9fa408fe3cd218b7ae103deb0b77 Mon Sep 17 00:00:00 2001 From: Mariah Schneeberger Date: Thu, 11 May 2017 11:33:56 -0600 Subject: [PATCH 19/19] finished xml spec -except nesting --- 13_xml_document/xml_document.rb | 51 ++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/13_xml_document/xml_document.rb b/13_xml_document/xml_document.rb index 63ab86c8e..02d7afa9a 100644 --- a/13_xml_document/xml_document.rb +++ b/13_xml_document/xml_document.rb @@ -4,21 +4,52 @@ def initialize(indent = false) @indent = indent end + def block_depth=(value) + Thread.current[:block_depth] = value + end + + def block_depth + Thread.current[:block_depth] || 0 + end + + def track_block_depth(&block) + self.block_depth += 1 + yield + ensure + self.block_depth -= 1 + end def method_missing(method,arg={},*block) - if @indect == true - else - if block_given? - "<#{method}>#{yield}" - elsif ! arg.empty? - k = arg.keys - v = arg.values - "<#{method} #{k[0]}\='#{v[0]}'/>" + if block_given? + if @indent == true + indent_tags(method,arg={},block) else - "<#{method}/>" + "<#{method}>#{yield}" end + elsif ! arg.empty? + k = arg.keys + v = arg.values + "<#{method} #{k[0]}\='#{v[0]}'/>" + else + "<#{method}/>" + end + end + + + def indent_tags(method,arg={},*block) + # level determines how much of an indent + # need to check and keep track of level of nest + indent = " " + new_string = "" + track_block_depth do + new_string << "#{indent} * #{self.block_depth}" << "<#{method}>\n" << "#{indent} * #{yield.block_depth}" << "#{yield}\n" << "#{indent} * #{self.block_depth}" << "\n" end + new_string + # opening tags indent + # find way to distinguish between opening and closing tags + # closing tag back out + # new line after each tag end -end +end # end of class