From 714f59a600c719017b0d693bf1548983f6a70c17 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Thu, 12 Jan 2017 12:44:10 -0600 Subject: [PATCH 01/17] Completed problem 00 --- 00_hello/hello.rb | 7 +++++++ README.md | 1 + 2 files changed, 8 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..5a5ad056e --- /dev/null +++ b/00_hello/hello.rb @@ -0,0 +1,7 @@ +def hello + "Hello!" +end + +def greet(who) + "Hello, #{who}!" +end diff --git a/README.md b/README.md index 706b785e9..dc616a8d3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ Test First Ruby -- RSpec 3 Edition ========== +# Chris Reed See [testfirst.org/learn_ruby](http://testfirst.org/learn_ruby#install) for more information about how this will work. These test-first Ruby challenges have been forked from [https://github.com/alexch/learn_ruby](https://github.com/alexch/learn_ruby) and updated to use RSpec 3 instead of RSpec 2. From c02340a839c0309d25b37280c82f18ff3f5d68eb Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Thu, 12 Jan 2017 19:29:01 -0600 Subject: [PATCH 02/17] Completed problem 1 --- 01_temperature/temperature.rb | 9 +++++++++ rspec_config.rb | 1 + 2 files changed, 10 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..161790616 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,9 @@ +#convert fahrenheit to celsius +def ftoc(temp_f) + (temp_f - 32) * (5.0/9.0) +end + +#convert celsius to fahrenheit +def ctof(temp_c) + (temp_c) * (9.0/5.0) + 32 +end diff --git a/rspec_config.rb b/rspec_config.rb index d3b116831..33677ddac 100644 --- a/rspec_config.rb +++ b/rspec_config.rb @@ -4,4 +4,5 @@ RSpec.configure do |c| c.fail_fast = true c.color = true + end From 41ea1cc0932d91e0c871add0d6de4921d5007dd0 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Thu, 12 Jan 2017 19:50:41 -0600 Subject: [PATCH 03/17] Completed problem 2 --- 02_calculator/calculator.rb | 32 ++++++++++++++++++++++++++++++ 02_calculator/calculator_spec.rb | 34 +++++++++++++++++++++++--------- 2 files changed, 57 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..13417efc0 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,32 @@ +def add(num_1, num_2) + num_1 + num_2 +end + +def subtract(num_1, num_2) + num_1 - num_2 +end + +def sum(array) + sum = 0 + array.each {|num| sum += num} + sum +end + +def multiply(*args) + product = 1 + args.each {|num| product *= num} + product +end + +def power(base, exponent) + result = 1 + exponent.times {result*=base} + result +end + +def factorial(num) + return 1 if num == 0 #by definition + result = 1 + (1..num).each {|x| result *= x} + return result +end diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..01ce34b39 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(10,4)).to eq(40) + 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(3,2)).to eq(9) + 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 f8895355bd7853267a6f455bed51562dd3b987f6 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Thu, 12 Jan 2017 20:46:37 -0600 Subject: [PATCH 04/17] Completed problem 3 --- 03_simon_says/simon_says.rb | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 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..94ce2289f --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,38 @@ +def echo(str) + str +end + +def shout(str) + str.upcase +end + +def repeat(str, num=2) + result = (str + ' ') * num + result[0..-2] #remove trailing space +end + +def start_of_word(str, num=1) + str[0..num-1] +end + +def first_word(str) + str.split(' ')[0] +end + + +def titleize(str) + result = str.split(' ').each_with_index do |word, index| + if index == 0 + word.capitalize! + elsif (word != 'and') && (word != 'over') && (word != 'the') + word.capitalize! + end + + end + result.join(' ') +end + +# # p first_word('hello there') +# z = '' +# z= 'david copperfield'.split(' ').each {|x| x.capitalize!} +# p z.join(' ') From 3c6efeece8cbf31cacdecf0517e05e0a39a24ee3 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sat, 14 Jan 2017 10:46:44 -0600 Subject: [PATCH 05/17] Completed problem 4 --- 04_pig_latin/pig_latin.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 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..94f7fc93e --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,39 @@ +def translate (words) + result = words.split(' ').each do |word| + + #check for 'qu' + if word.index('qu') + word.insert(-1, word[0..word.index('qu')+1]) + word.slice!(0..word.index('qu')+1) + word << 'ay' + next + + #check if first 3 letters are consonants + elsif word[0] =~ (/[^aeiou]/) && word[1] =~ (/[^aeiou]/) && word[2] =~ (/[^aeiou]/) + word.insert(-1, word[0..2]) + word.slice!(0..2) + word << 'ay' + next + + #check if first 2 letters are consonants or word starts with 'qu' + elsif word[0] =~ (/[^aeiou]/) && word[1] =~ (/[^aeiou]/) + word.insert(-1, word[0..1]) + word.slice!(0..1) + word << 'ay' + next + + #check if first letter is a consonant + elsif word[0] =~ (/[^aeiou]/) + word.insert(-1, word[0]) + word.slice!(0) + word << 'ay' + next + #first letter will be a vowel + else + word << 'ay' + end + end + result.join(' ') +end + +# p translate('apple pie') From de1468f9157301e9a33ad434fe33596aeea0e1d4 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sat, 14 Jan 2017 11:58:38 -0600 Subject: [PATCH 06/17] Completed problem 5 --- 05_silly_blocks/silly_blocks.rb | 16 ++++++++++++++++ 1 file changed, 16 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..de6947ae2 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,16 @@ +def reverser + + yield.split(' ').each {|word| word.to_s.reverse!}.join(' ') + +end + +def adder(num=1) + yield + num +end + + +def repeater(numtimes=1) + + numtimes.times {yield} + +end From b22cce23fc4096ba2fec4412ccb24ca029c77a2b Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 09:59:31 -0600 Subject: [PATCH 07/17] Completed problem 6 --- 06_performance_monitor/performance_monitor.rb | 11 +++++++++++ 1 file changed, 11 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..9abd2885e --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,11 @@ +require "time" + +def measure(num_times=1) + + start_time = Time.now + num_times.times {yield} + (Time.now-start_time)/num_times #average time + +end + +puts Time.now() From 66cbc494b7be168b3be360631600785ef561c880 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 10:10:09 -0600 Subject: [PATCH 08/17] Completed problem 7 --- 07_hello_friend/friend.rb | 9 +++++++++ 1 file changed, 9 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..036f41807 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(who=nil) + if who + "Hello, #{who}!" + else + "Hello!" + end + end +end From 10eff6b08c09ae2ff0125c3ab075ca724c846731 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 10:55:04 -0600 Subject: [PATCH 09/17] Completed problem 8 --- 08_book_titles/book.rb | 19 +++++++++++++++++++ 1 file changed, 19 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..962f4d0d0 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,19 @@ +class Book + attr_reader :title + + def title=(title) + #words to not capitalize + wordslist = ["and", "over", "the", "a", "in", "of", "an"] + + @title = title + result= @title.split(' ').each_with_index do |word, index| + if index == 0 + word.capitalize! + elsif wordslist.index(word) == nil + word.capitalize! + end + end + @title = result.join(' ') + end + +end From bfae53961e5fa0fa295ba8b1034516b0318dd846 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 11:27:45 -0600 Subject: [PATCH 10/17] Completed problem 9 --- 09_timer/timer.rb | 20 ++++++++++++++++++++ 1 file changed, 20 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..717288649 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,20 @@ +class Timer + def initialize + @seconds = 0 + end + + attr_reader :seconds + + def seconds=(seconds) + @seconds = seconds + end + + def time_string + hours = @seconds / 3600 + mins = (@seconds % 3600) / 60 + secs = (@seconds % 3600) % 60 + format("%02d:%02d:%02d", hours, mins, secs) + + end + +end From 7fef2bf1e108f2dcf7efeeaee6d809dd809e3aa0 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 13:35:48 -0600 Subject: [PATCH 11/17] Completed problem 10 --- 10_temperature_object/temperature.rb | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 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..e2d2a601e --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,45 @@ +class Temperature + + + def initialize(temp = {}) + @temp = temp + + end + + def Temperature.from_celsius(temp_c) + Temperature.new(:c => temp_c) + end + + def Temperature.from_fahrenheit(temp_f) + Temperature.new(:f => temp_f) + end + + def in_fahrenheit + if @temp[:f] + @temp[:f] + elsif @temp[:c] + (@temp[:c]) * (9.0/5.0) + 32 + end + end + + def in_celsius + if @temp[:c] + @temp[:c] + elsif @temp[:f] + (@temp[:f] - 32) * (5.0/9.0) + end + end +end + +class Celsius < Temperature + def initialize(temp) + super(:c => temp) + end +end + +class Fahrenheit < Temperature + def initialize(temp) + super(:f => temp) + end + +end From 1a3c5f8340971a41afb497e16c30177154b2ab19 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 19:18:48 -0600 Subject: [PATCH 12/17] Completed problem 11 --- 11_dictionary/dictionary.rb | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 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..4d5dd5e05 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,50 @@ +class Dictionary + + def initialize + @dic = {} + end + + def entries + @dic + end + + def add(entry) + if entry.is_a?(Hash) + @dic.merge!(entry) + #add key only + elsif entry.is_a?(String) + @dic.merge!({entry => nil}) + end + end + + def keywords + @dic.keys.sort! + end + + def include?(key) + @dic.key?(key) + end + + def find(string) + results = {} + @dic.each do |key,val| + if key.index(string) + #if the key contains the search string, add k-v pair to results + results.merge!({key => val}) + end + end + results + end + + def printable + hash = entries.sort + result = "" + hash.each do |key,val| + result << "[#{key}] \"#{val}\"\n" + + end + result.strip #remove last \n + + end + +end From baf7ae16da86f38811f9b22a203dfe98eb5dbdeb Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 19:20:14 -0600 Subject: [PATCH 13/17] removed strip on result of #printable --- 11_dictionary/dictionary.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb index 4d5dd5e05..bd29dfb1a 100644 --- a/11_dictionary/dictionary.rb +++ b/11_dictionary/dictionary.rb @@ -43,7 +43,7 @@ def printable result << "[#{key}] \"#{val}\"\n" end - result.strip #remove last \n + result end From cc6a08c497d1c2273f87ab8081f01c059ebeaea1 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 21:23:09 -0600 Subject: [PATCH 14/17] Completed problem 12 except for extra credit --- 12_rpn_calculator/rpn_calculator.rb | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 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..01b1361d8 --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,58 @@ +class RPNCalculator + + + def initialize + @stack = [] + end + + def push(val) + @stack.push(val) + end + + def plus + if @stack.length ==0 + empty_error + end + sum = @stack.pop + sum += @stack.pop + @stack.push(sum) + end + def minus + if @stack.length ==0 + empty_error + end + num_1 = @stack.pop + num_2 = @stack.pop + @stack.push(num_2-num_1) + end + + def divide + if @stack.length ==0 + empty_error + end + num_1 = @stack.pop.to_f + num_2 = @stack.pop + @stack.push(num_2/num_1) + end + + def times + if @stack.length ==0 + empty_error + end + num_1 = @stack.pop + num_2 = @stack.pop + @stack.push(num_2*num_1) + end + + def empty_error + raise("calculator is empty") + end + + + def value + @stack[-1] + end + + + +end From 635dcbb6dcca25d7ccc7c16c7addfd3be3ab7a88 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 21:34:18 -0600 Subject: [PATCH 15/17] Completed problem 14 --- 14_array_extensions/array_extensions.rb | 19 +++++++++++++++++++ 1 file changed, 19 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..e4db86083 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,19 @@ +class Array + + def sum + result = 0 + self.each {|num| result +=num} + result + end + + def square + result = [] + self.each {|num| result << num*num} + result + end + + def square! + self.each_with_index {|num,index| self[index] = num*num} + end + +end From 71702b35b0982d5a2f4fc42269e6e7ae19f857e3 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Sun, 15 Jan 2017 21:35:25 -0600 Subject: [PATCH 16/17] Completed problem 11 --- 11_dictionary/dictionary.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb index bd29dfb1a..82e5d0034 100644 --- a/11_dictionary/dictionary.rb +++ b/11_dictionary/dictionary.rb @@ -41,7 +41,6 @@ def printable result = "" hash.each do |key,val| result << "[#{key}] \"#{val}\"\n" - end result From 463a0fcb24048b8e69a17704f94d613fbfff6357 Mon Sep 17 00:00:00 2001 From: Chris Reed Date: Mon, 16 Jan 2017 20:50:31 -0600 Subject: [PATCH 17/17] Completed problem 15 --- 15_in_words/in_words.rb | 108 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 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..da6db15be --- /dev/null +++ b/15_in_words/in_words.rb @@ -0,0 +1,108 @@ +class Fixnum + + + + def in_words + @@result = '' + tril = 1_000_000_000_000 + bil = 1_000_000_000 + mil = 1_000_000 + + #get values for each group of 1000 + trillions = self / tril + billions = self % tril / bil + millions = self % tril % bil / mil + thousands = self % tril % bil % mil / 1000 + ones = self % tril % bil % mil % 1000 + + + if self ==0 + #special case for zero + return 'zero' + #append numerical label to values + else + if trillions > 0 + parse(trillions) + @@result << ' trillion ' + + end + if billions > 0 + parse(billions) + @@result << ' billion ' + + end + if millions > 0 + parse(millions) + @@result << ' million ' + + end + if thousands > 0 + parse(thousands) + @@result << ' thousand ' + end + if ones > 0 + parse(ones) + end + end + + @@result.strip + + end + + #break apart numbers for conversion + def parse(num) + num_hundreds = num / 100 + ones = num % 100 + tens_place = (ones/10)*10 + + if num_hundreds > 0 + @@result << convert(num_hundreds) + @@result << " hundred " + end + + if ones > 0 + if (ones <=20) || (ones % 10 ==0) + @@result << convert(ones) + else + @@result << convert(tens_place) + @@result << " " + @@result << convert(ones%10) + end + end + end + + #convert the number to values + def convert(num) + + case num + when 1 then 'one' + when 2 then 'two' + when 3 then 'three' + when 4 then 'four' + when 5 then 'five' + when 6 then 'six' + when 7 then 'seven' + when 8 then 'eight' + when 9 then 'nine' + when 10 then 'ten' + when 11 then 'eleven' + when 12 then 'twelve' + when 13 then 'thirteen' + when 14 then 'fourteen' + when 15 then 'fifteen' + when 16 then 'sixteen' + when 17 then 'seventeen' + when 18 then 'eighteen' + when 19 then 'nineteen' + when 20 then 'twenty' + when 30 then 'thirty' + when 40 then 'forty' + when 50 then 'fifty' + when 60 then 'sixty' + when 70 then 'seventy' + when 80 then 'eighty' + when 90 then 'ninety' + end + end + +end