From 96f4b94d8a8e8d2ed0edda1c7df2e6423be20672 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 13:08:29 -0500 Subject: [PATCH 01/16] Solution to problem 0 --- 00_hello/hello.rb | 7 +++++++ 00_hello/hello_spec.rb | 2 +- 2 files changed, 8 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..3623e57f4 --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(who) + "Hello, #{who}!" +end \ No newline at end of file diff --git a/00_hello/hello_spec.rb b/00_hello/hello_spec.rb index 26075944a..3eed35159 100644 --- a/00_hello/hello_spec.rb +++ b/00_hello/hello_spec.rb @@ -25,7 +25,7 @@ # Open up `hello.rb` in a text editor. Save it. Run the test again. # # rake -# +#ra # ## Watch it fail # # Now you should see an error like this: From 1976abed8a51dbd82a84a4c1b419eaf375990b6a Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 13:14:10 -0500 Subject: [PATCH 02/16] Solution to problem 1 --- 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..7e06948db --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f_temp) + c_temp = (f_temp - 32) * 5 / 9 +end + +def ctof(c_temp) + f_temp = c_temp.to_f * 9 / 5 + 32.0 +end \ No newline at end of file From a69832114ebcd308e1d40f474cc1a603b16f31fd Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 13:14:52 -0500 Subject: [PATCH 03/16] Solution to problem 2 --- 02_calculator/calculator.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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..fffb59dea --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,17 @@ +def add(a, b) + a + b +end + +def subtract(a, b) + a - b +end + +def sum(arr) + sum = 0 + + arr.each do |i| + sum += i + end + + sum +end \ No newline at end of file From 917d24ae6cadbb6b5986f152dfc8ad92ce4b0eaa Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 13:16:34 -0500 Subject: [PATCH 04/16] Solution to problem 3 --- 03_simon_says/simon_says.rb | 57 +++++++++++++++++++++++++++++++++++++ 04_pig_latin/pig_latin.rb | 3 ++ 2 files changed, 60 insertions(+) create mode 100644 03_simon_says/simon_says.rb create mode 100644 04_pig_latin/pig_latin.rb diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb new file mode 100644 index 000000000..94080b5f3 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,57 @@ +def echo(s) + s +end + +def shout(s) + s.upcase +end + +def repeat(s, n_times = 2) + repeat_s = s + repeat_s += " #{s}" * (n_times - 1) +end + +def start_of_word(word, characters) + partial_word = "" + + characters.times do |c| + partial_word += word[c] + end + + partial_word +end + +def first_word(words) + first_word = "" + + i = 0 + until words[i] == " " do + first_word += words[i] + i += 1 + end + + first_word +end + + +def titleize(phrase) + word = "" + words = [] + + phrase.length.times do |i| + if phrase[i] == " " + words.push(word.downcase) + word = "" + else i == phrase.length - 1 + word += phrase[i] + end + words.push(word.downcase) if i == phrase.length - 1 + end + + words.each_with_index do |word, i| + words[i][0] = word[0].upcase unless word == "the" || word == "and" || word == "over" + words[i][0] = word[0].upcase if i == 0 + end + + words = words.join(" ") +end \ No newline at end of file diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..43353f53f --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,3 @@ + +class +end \ No newline at end of file From ea37e3f7bf63d53d26056dc1815f3fe45f7defaa Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 14:33:33 -0500 Subject: [PATCH 05/16] Solution to problem 3 - added "over" to uncapitalized words --- 03_simon_says/simon_says.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb index 94080b5f3..6005d2c7f 100644 --- a/03_simon_says/simon_says.rb +++ b/03_simon_says/simon_says.rb @@ -54,4 +54,4 @@ def titleize(phrase) end words = words.join(" ") -end \ No newline at end of file +end From 80ec88a124a07f66e50179822576daacab874392 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 14:34:09 -0500 Subject: [PATCH 06/16] Solution to problem 4 --- 04_pig_latin/pig_latin.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb index 43353f53f..b98753d27 100644 --- a/04_pig_latin/pig_latin.rb +++ b/04_pig_latin/pig_latin.rb @@ -1,3 +1,26 @@ +def translate(phrase) + words = phrase.split(' ') -class + words.each_with_index do |word, i| + translated_word = word.downcase + + case translated_word[0..2] + when /thr/, /sch/, /[^aeiou]qu/ + translated_word += translated_word[0..2] + translated_word = translated_word[3..-1] + when /ch./, /qu./, /th./, /br./ + translated_word += translated_word[0..1] + translated_word = translated_word[2..-1] + when /[^aeiou]../ + translated_word += translated_word[0] + translated_word = translated_word[1..-1] + else + + end + + translated_word += "ay" + words[i] = translated_word + end + + words = words.join(" ") end \ No newline at end of file From 4ef79103a08743db3c6bdbc55895a87daead3acb Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 14:52:10 -0500 Subject: [PATCH 07/16] Solution to problem 5 --- 05_silly_blocks/silly_blocks.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 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..d64eb0547 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,29 @@ +def word_reverse(string) + reverse_string = "" + + i = -1 + string.length.times do + reverse_string += string[i] + i -= 1 + end + + reverse_string +end + +def reverser + text = yield.to_s.split + + text.each_with_index do |word, i| + text[i] = word_reverse(word).downcase + end + + text = text.join(" ") +end + +def adder(n = 1) + yield + n +end + +def repeater(n = 1) + n.times { yield } +end \ No newline at end of file From ee20c97eef1c090f73e5206ea35cd16dda202203 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 14:57:16 -0500 Subject: [PATCH 08/16] Solution to problem 6 --- 06_performance_monitor/performance_monitor.rb | 7 +++++++ 1 file changed, 7 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..659cd8072 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,7 @@ +def measure(number_of_times = 1) + start = Time.now + number_of_times.times { yield } + finish = Time.now + duration = finish - start + average_duration = duration / number_of_times +end \ No newline at end of file From 2ed67c859ce4deb3e36458410a34e2e0a9f8fd32 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 15:01:08 -0500 Subject: [PATCH 09/16] Solution to problem 7 --- 07_hello_friend/friend.rb | 5 +++++ 1 file changed, 5 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..e8e132afa --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,5 @@ +class Friend + def greeting(someone = false) + someone ? "Hello, #{someone}!" : "Hello!" + end +end \ No newline at end of file From 6c5c9fd17ab61accdca0aa7911eded8cd55af7a9 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 15:36:03 -0500 Subject: [PATCH 10/16] Solution to problem 8 --- 08_book_titles/book.rb | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 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..ef147f70e --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,44 @@ +class Book + def initialize + @title = "" + end + + def title + @title + end + + def title=(new_title) + @title = titleize(new_title) + end + + def titleize(phrase) + word = "" + words = [] + + phrase.length.times do |i| + if phrase[i] == " " + words.push(word.downcase) + word = "" + else i == phrase.length - 1 + word += phrase[i] + end + words.push(word.downcase) if i == phrase.length - 1 + end + + words.each_with_index do |word, i| + words[i][0] = word[0].upcase unless lowercase_word?(word) + words[i][0] = word[0].upcase if i == 0 + end + + words = words.join(" ") + end + + def lowercase_word?(word) + lowercase_words = ["the", "and", "over", "under", "of", "in", "a", "an"] + lowercase_words.each do |lowercase_word| + return true if word.downcase == lowercase_word + end + + return false + end +end \ No newline at end of file From 1c92417eaf2dcdc74d878b2209f95b004b7e5efa Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 16:01:29 -0500 Subject: [PATCH 11/16] Solution to problem 9 --- 09_timer/timer.rb | 19 +++++++++++++++++++ 1 file changed, 19 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..b9ef41807 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,19 @@ +class Timer + def initialize + @seconds = 0 + end + + attr_accessor :seconds + + def time_string + hours = @seconds / 60 / 60 + minutes = (@seconds / 60) - (hours * 60) + seconds = @seconds - (hours * 60 * 60) - (minutes * 60) + + hours_string = hours < 10 ? "0#{hours}" : hours.to_s + minutes_string = minutes < 10 ? "0#{minutes}" : minutes.to_s + seconds_string = seconds < 10 ? "0#{seconds}" : seconds.to_s + + "#{hours_string}:#{minutes_string}:#{seconds_string}" + end +end From 4e89137f0ab13476dc40a15b8ae92890d1a26d8d Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 16:49:49 -0500 Subject: [PATCH 12/16] Solution to problem 10 --- 10_temperature_object/temperature.rb | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 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..e3e3f93be --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,41 @@ +class Temperature + def initialize(options) + @in_celsius = options[:c] ? options[:c] : ftoc(options[:f]) + @in_fahrenheit = options[:f] ? options[:f] : ctof(options[:c]) + end + + attr_reader :in_celsius, :in_fahrenheit + + def self.from_celsius(in_celsius) + Temperature.new( { c: in_celsius } ) + end + + def self.from_fahrenheit(in_fahrenheit) + Temperature.new( { f: in_fahrenheit } ) + end + + def ftoc(in_fahrenheit) + in_celsius = (in_fahrenheit - 32) * 5 / 9 + end + + def ctof(in_celsius) + in_fahrenheit = in_celsius.to_f * 9 / 5 + 32.0 + end +end + +class Celsius < Temperature + def initialize(in_celsius) + @in_celsius = in_celsius + @in_fahrenheit = ctof(in_celsius) + end +end + +class Fahrenheit < Temperature + def initialize(in_fahrenheit) + @in_celsius = ftoc(in_fahrenheit) + @in_fahrenheit = in_fahrenheit + end +end + + + From 39c8aaf435cdb525468fbb41ec711e6c4076d9e5 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 17:58:35 -0500 Subject: [PATCH 13/16] Solution to problem 11 --- 11_dictionary/dictionary.rb | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 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..01adfa287 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,49 @@ +class Dictionary + attr_reader :entries + + def initialize + @entries = {} + end + + def add(entry) + @entries[entry] = nil if entry.is_a?(String) + + if entry.is_a?(Hash) + entry.each_key { |key| @entries[key.to_s] = entry[key].to_s } + end + end + + def include?(keyword) + @entries.each_key do |entry_key| + return true if entry_key == keyword + end + + return false + end + + def find(keyword) + entry = {} + + @entries.each_key do |key| + entry[key] = @entries[key] if key.include?(keyword) + end + + return entry + end + + def keywords + keywords = @entries.keys.to_a + keywords.sort + end + + def printable + printable_text = [] + + keywords.each do |key| + printable_text.push("[#{key}] \"#{@entries[key]}\"") + end + + return printable_text.join("\n") + end + +end \ No newline at end of file From bc50c2e6cc25c13bfd892621f1dd297edc73445b Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 21:24:00 -0500 Subject: [PATCH 14/16] Solution to problem 12 --- 12_rpn_calculator/rpn_calculator.rb | 89 +++++++++++++++++++++++++++++ 1 file changed, 89 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..241b3560b --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,89 @@ +class RPNCalculator + attr_reader :value + + def initialize + @stack = [] + end + + def push(val) + @stack.push(val) + end + + def value + @stack[-1] + end + + def plus + if @stack.count >= 2 + b = @stack.pop + a = @stack.pop + push(a + b) + else + raise "calculator is empty" + end + end + + def minus + if @stack.count >= 2 + b = @stack.pop + a = @stack.pop + push(a - b) + else + raise "calculator is empty" + end + end + + def divide + if @stack.count >= 2 + b = @stack.pop.to_f + a = @stack.pop.to_f + push(a / b) + else + raise "calculator is empty" + end + end + + def times + if @stack.count >= 2 + b = @stack.pop + a = @stack.pop + push(a * b) + else + raise "calculator is empty" + end + end + + def tokens(s) + tokenized_s = [] + + s.length.times do |i| + next if s[i] == " " + /[0-9]/.match(s[i]) ? tokenized_s.push(s[i].to_i) : tokenized_s.push(:"#{s[i]}") + end + + return tokenized_s + end + + def evaluate(s) + s.length.times do |i| + case s[i] + when /[0-9]/ + push(s[i].to_i) + when "+" + plus + when "-" + minus + when "/" + divide + when "*" + times + else + next + end + end + + value + end + +end + From 8fe473be0d8dbc62fbe0422b0ff57e7bcf90f444 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Sun, 5 Feb 2017 21:41:03 -0500 Subject: [PATCH 15/16] Solution to problem 14 --- 14_array_extensions/array_extensions.rb | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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..e0e464c0f --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,28 @@ +class Array + def sum + sum = 0 + return sum unless self.count > 0 + + self.each { |i| sum += i } + + return sum + end + + def square + return [] unless self.count > 0 + + squared_arr = [] + self.each { |i| squared_arr.push(i**2) } + + return squared_arr + end + + def square! + return self unless self.count > 0 + + self.each_with_index { |val, i| self[i] = val**2 } + + return self + end + +end \ No newline at end of file From 244bd3aadefeec18e41551b666c51cb6f4903c53 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Mon, 6 Feb 2017 01:19:27 -0500 Subject: [PATCH 16/16] Solution to problem 15 --- 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..3ad770ff6 --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,61 @@ +class Fixnum + def in_words + numbers_in_words = {} + numbers_in_words[0] = 'zero' + numbers_in_words[1] = 'one' + numbers_in_words[2] = 'two' + numbers_in_words[3] = 'three' + numbers_in_words[4] = 'four' + numbers_in_words[5] = 'five' + numbers_in_words[6] = 'six' + numbers_in_words[7] = 'seven' + numbers_in_words[8] = 'eight' + numbers_in_words[9] = 'nine' + numbers_in_words[10] = 'ten' + numbers_in_words[11] = 'eleven' + numbers_in_words[12] = 'twelve' + numbers_in_words[13] = 'thirteen' + numbers_in_words[14] = 'fourteen' + numbers_in_words[15] = 'fifteen' + numbers_in_words[16] = 'sixteen' + numbers_in_words[17] = 'seventeen' + numbers_in_words[18] = 'eighteen' + numbers_in_words[19] = 'nineteen' + numbers_in_words[20] = 'twenty' + numbers_in_words[30] = 'thirty' + numbers_in_words[40] = 'forty' + numbers_in_words[50] = 'fifty' + numbers_in_words[60] = 'sixty' + numbers_in_words[70] = 'seventy' + numbers_in_words[80] = 'eighty' + numbers_in_words[90] = 'ninety' + numbers_in_words[100] = 'hundred' + numbers_in_words[1_000] = 'thousand' + numbers_in_words[1_000_000] = 'million' + numbers_in_words[1_000_000_000] = 'billion' + numbers_in_words[1_000_000_000_000] = 'trillion' + + ones = self % 10 + tens = self % 100 - ones + under_20 = self % 20 + hundreds = self % 1_000 / 100 + thousands = self % 1_000_000 / 1_000 + millions = self % 1_000_000_000 / 1_000_000 + billions = self % 1_000_000_000_000 / 1_000_000_000 + trillions = self % 1_000_000_000_000_000 / 1_000_000_000_000 + + return numbers_in_words[self] if self < 20 && self >= 0 + return numbers_in_words[self] if self < 100 && self % 10 == 0 && self > 0 + + words = [] + words.push("#{trillions.in_words} trillion") if trillions > 0 + words.push("#{billions.in_words} billion") if billions > 0 + words.push("#{millions.in_words} million") if millions > 0 + words.push("#{thousands.in_words} thousand") if thousands > 0 + words.push("#{hundreds.in_words} hundred") if hundreds > 0 + words.push("#{tens.in_words}") if tens >= 20 + words.push("#{ones.in_words}") if tens >= 20 && ones > 0 + words.push("#{under_20.in_words}") if tens < 20 && under_20 > 0 + words.join(" ") + end +end \ No newline at end of file