From 09d7f21e8ad5def80a9d4dcad8d5e0555dc1c7aa Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Thu, 15 Sep 2016 19:39:35 -0700 Subject: [PATCH 01/24] hello and temp rspec --- .DS_Store | Bin 0 -> 6148 bytes 00_hello/hello.rb | 7 +++++++ 01_temperature/temperature.rb | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 .DS_Store create mode 100644 00_hello/hello.rb create mode 100644 01_temperature/temperature.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..546ddf31168b3ea7d3b215f19af4fc00a677cbdb GIT binary patch literal 6148 zcmeHKPfNov6i?>VPA?S|1TVb`9wu{ot#clQL1iK;v!%raN;B5ZA!E?9AH)COL-afN zQM~zHk_}~g6_xkG%kSmoy`=r6Bn@MX*LvXwV>V;V0!1v8p!!0vA9X?s#)HV!7!ix1 z$e^EgCZfsl8yUd0vsf8Qi?!K@^$TJdK;K)?iA9=~%dfmJRh*ugwMujISnP$lQ?D)i zUgl@LwB~n@RZE0XVC;sUwMX`Wl=0a1gQkf3EqiHKCYdjisQV%HgX?tgJ0{LGm}}JOjH{Vp95XXFHx#aB2fw4j8Fw{OO$-nNpBb3aO&ibuz3=b; zpOdIX3=jkViUD4%d-WQ;k~v!^UJlP%2lNaS1> Date: Thu, 15 Sep 2016 21:32:26 -0700 Subject: [PATCH 02/24] calculator rb --- 02_calculator/calculator.rb | 15 +++++++++++++++ 1 file changed, 15 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..7734eb466 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,15 @@ +def add (x,y) + sum = x + y #why does this pass all addition tests? +end + +def subtract (x,y) + sum = x - y +end + +def sum(arr) + sum = 0 + arr.each do |x| + sum += x + end + sum +end From 7a8f062bf04efd0e84befe4572f75a39ddccddca Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Thu, 15 Sep 2016 22:50:20 -0700 Subject: [PATCH 03/24] simon says almost done --- 03_simon_says/simon_says.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 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..d027ce4d9 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,23 @@ +def echo(x) + x +end + +def shout(x) + x.upcase +end + +def repeat(x, times = 2) + ([x] * times).join(" ") #why does array [x] === "hello hello hello"? +end + +def start_of_word(s, n) + s[0...n] +end + +def first_word(s) + s.split(" ").first +end + +def titleize(t) + t.capitalize! +end \ No newline at end of file From 861151027bd5cac1c73ce5b8b0423c9ac8064d5b Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Fri, 16 Sep 2016 01:12:42 -0700 Subject: [PATCH 04/24] partially pig latin --- 04_pig_latin/pig_latin.rb | 15 +++++++++++++++ 05_silly_blocks/silly_blocks.rb | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 04_pig_latin/pig_latin.rb create mode 100644 05_silly_blocks/silly_blocks.rb diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb new file mode 100644 index 000000000..fdfb08bbf --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,15 @@ +def translate str + alpha = ('a'..'z').to_a + vowels = %w[a e i o u] + consonants = alpha - vowels + + if vowels.include?(str[0]) + str + 'ay' + elsif consonants.include?(str[0]) && consonants.include?(str[1]) + str[2..-1] + str[0..1] + 'ay' + elsif consonants.include?(str[0]) + str[1..-1] + str[0] + 'ay' + else + str # return unchanged + end +end \ No newline at end of file diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb new file mode 100644 index 000000000..cf093dd2a --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,3 @@ +def reverser str + str.reverse! +end \ No newline at end of file From 3b5d3d64ef20ed0183af73e5c3ff62c3e8d9fcc7 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Fri, 16 Sep 2016 01:39:12 -0700 Subject: [PATCH 05/24] silly blocks --- 05_silly_blocks/silly_blocks.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb index cf093dd2a..ef0ed9c3d 100644 --- a/05_silly_blocks/silly_blocks.rb +++ b/05_silly_blocks/silly_blocks.rb @@ -1,3 +1,11 @@ -def reverser str - str.reverse! +def reverser(&block) + yield.split(" ").map(&:reverse).join(" ") +end + +def adder(num=1, &block) + block.call + num +end + +def repeater(n=1, &block) + n.times &block end \ No newline at end of file From 319429d298ca2b72ba758523f9a1968d551afbf0 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Fri, 16 Sep 2016 18:09:43 -0700 Subject: [PATCH 06/24] finished simon says & started performance monitor --- 03_simon_says/simon_says.rb | 16 ++++++++++++++-- 06_performance_monitor/performance_monitor.rb | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 06_performance_monitor/performance_monitor.rb diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb index d027ce4d9..031e98e5d 100644 --- a/03_simon_says/simon_says.rb +++ b/03_simon_says/simon_says.rb @@ -6,8 +6,12 @@ def shout(x) x.upcase end +# def repeat(x, times = 2) +# ([x] * times).join(" ") #why does array [x] === "hello hello hello"? +# end + def repeat(x, times = 2) - ([x] * times).join(" ") #why does array [x] === "hello hello hello"? + ([x] * times).join(' ') end def start_of_word(s, n) @@ -19,5 +23,13 @@ def first_word(s) end def titleize(t) - t.capitalize! + little_words = ['and', 'the', 'over'] + t.capitalize.split(" ").map { |word| + if little_words.include?(word) + word + else + word.capitalize + end + }.join(" ") + end \ No newline at end of file diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb new file mode 100644 index 000000000..143885ae0 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,3 @@ +def measure(&block) + 0 +end \ No newline at end of file From 439d7a56298288cb509a87b2eff56fd4cef56d43 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Sat, 17 Sep 2016 11:49:44 -0700 Subject: [PATCH 07/24] performance monitor --- 06_performance_monitor/performance_monitor.rb | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb index 143885ae0..ff7c53ab7 100644 --- a/06_performance_monitor/performance_monitor.rb +++ b/06_performance_monitor/performance_monitor.rb @@ -1,3 +1,31 @@ -def measure(&block) - 0 -end \ No newline at end of file +# def measure n = 1 +# total_time = 0 +# start_time = Time.now +# n.times { yield } +# end_time = Time.now +# total_time += end_time - start_time +# total_time / n +# end + +def measure(repeat = 1) + start_time = Time.now + + repeat.times { |x| + yield x + } + end_time = Time.now + elapsed_time = (end_time - start_time)/repeat +end + +# def measure count=1 +# total_time = 0 +# count.times do +# start_time = Time.now + +# yield + +# end_time = Time.now +# total_time += end_time - start_time +# end +# total_time / count +# end \ No newline at end of file From e6e87ac70f1884a02680b48d4a29a74e96d12911 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Sat, 17 Sep 2016 12:58:32 -0700 Subject: [PATCH 08/24] hello friend --- 07_hello_friend/friend.rb | 8 ++++++++ 1 file changed, 8 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..df912d3d7 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,8 @@ +class Friend + def greeting(name='') + name = ", #{name}" unless name.empty? + "Hello#{name}!" + end +end + + From de45b5c3337c1e8fb49cebee1502e5c1f6a6e5f5 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Sun, 18 Sep 2016 22:03:19 -0700 Subject: [PATCH 09/24] finished books rb --- 08_book_titles/book.rb | 17 +++++++++++++++++ 1 file changed, 17 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..21c148efa --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,17 @@ + +class Book + def title=(t) + conjunx_words = ['and','in','the','of','a','an'] + @title = t.capitalize.split(" ").map { |word| + if conjunx_words.include?(word) + word + else + word.capitalize + end + }.join(' ') + end + + def title + @title + end +end \ No newline at end of file From b649567a6c57f4640bab266fa83b465b08e5c04b Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 19 Sep 2016 23:10:26 -0700 Subject: [PATCH 10/24] timer --- 09_timer/timer.rb | 44 ++++++++++++++++++++++++++++++++++++++++++ 09_timer/timer_spec.rb | 24 +++++++++++------------ 2 files changed, 56 insertions(+), 12 deletions(-) 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..d262f8c93 --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,44 @@ +# class Timer +# attr_accessor :seconds + +# def initialize (seconds = 0) +# @seconds = seconds +# end + +# def time_string +# "00:00:00" +# end + +# def padded(n) +# "#{n}".rjust(2, '0') +# end + + +# end + +class Timer + attr_accessor :seconds + def initialize + @seconds = 0 + end + + def time_string + [hours, minutes, m_seconds].map(&method(:padded)).join(":") + end + + def hours + seconds / 3600 + end + + def minutes + (seconds % 3600)/60 + end + + def m_seconds + (seconds % 60) + end + + def padded(n) + "#{n}".rjust(2, '0') + end +end \ No newline at end of file diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index 40b33f23f..b998bf1b2 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -44,17 +44,17 @@ # One way to implement the Timer is with a helper method. # Uncomment these specs if you want to test-drive that # method, then call that method from inside of time_string. - # - # describe 'padded' do - # it 'pads zero' do - # expect(@timer.padded(0)).to eq('00') - # end - # it 'pads one' do - # expect(@timer.padded(1)).to eq('01') - # end - # it "doesn't pad a two-digit number" do - # expect(@timer.padded(12)).to eq('12') - # end - # end + + describe 'padded' do + it 'pads zero' do + expect(@timer.padded(0)).to eq('00') + end + it 'pads one' do + expect(@timer.padded(1)).to eq('01') + end + it "doesn't pad a two-digit number" do + expect(@timer.padded(12)).to eq('12') + end + end end From de4589c38f38d2ba97c1c691432398851c782df3 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Wed, 21 Sep 2016 16:45:25 -0700 Subject: [PATCH 11/24] dictionary rb --- 10_temperature_object/temperature.rb | 4 +++ 11_dictionary/dictionary.rb | 41 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 10_temperature_object/temperature.rb create mode 100644 11_dictionary/dictionary.rb diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..2c2bfd0f5 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,4 @@ +class Temperature(arg1, f=>{50}) + @f = 50 +end + diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb new file mode 100644 index 000000000..486503000 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,41 @@ +class Dictionary + + attr_accessor :entries + + def initialize + @entries = {} + end + + def add(x, value = nil) + if x.is_a?(Hash) + x.each do |key, value| + @entries[key] = value + end + else + @entries[x] = nil + end + end + + def keywords + @entries.keys.sort + end + + def include?(x) + @entries.keys.include?(x) ? true : false + end + + def find(x) + answer = {} + @entries.each do |key, value| + if key.include?(x) + answer[key] = value + end + end + answer + end + + def printable + final = @entries.sort.map {|key, value| "[#{key}] \"#{value}\""} + final.join("\n") + end +end \ No newline at end of file From 590b29999bc1a6ac2661f6f40875e20980a2c3b7 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Fri, 23 Sep 2016 12:14:46 -0700 Subject: [PATCH 12/24] redo hw, finished hello rb --- 00_hello/hello.rb | 2 +- 01_temperature/temperature.rb | 19 -------- 02_calculator/calculator.rb | 15 ------- 03_simon_says/simon_says.rb | 35 --------------- 04_pig_latin/pig_latin.rb | 15 ------- 05_silly_blocks/silly_blocks.rb | 11 ----- 06_performance_monitor/performance_monitor.rb | 31 ------------- 07_hello_friend/friend.rb | 8 ---- 08_book_titles/book.rb | 17 ------- 09_timer/timer.rb | 44 ------------------- 10_temperature_object/temperature.rb | 4 -- 11_dictionary/dictionary.rb | 41 ----------------- 12 files changed, 1 insertion(+), 241 deletions(-) delete mode 100644 01_temperature/temperature.rb delete mode 100644 02_calculator/calculator.rb delete mode 100644 03_simon_says/simon_says.rb delete mode 100644 04_pig_latin/pig_latin.rb delete mode 100644 05_silly_blocks/silly_blocks.rb delete mode 100644 06_performance_monitor/performance_monitor.rb delete mode 100644 07_hello_friend/friend.rb delete mode 100644 08_book_titles/book.rb delete mode 100644 09_timer/timer.rb delete mode 100644 10_temperature_object/temperature.rb delete mode 100644 11_dictionary/dictionary.rb diff --git a/00_hello/hello.rb b/00_hello/hello.rb index 7baecbd01..375bf71b4 100644 --- a/00_hello/hello.rb +++ b/00_hello/hello.rb @@ -4,4 +4,4 @@ def hello def greet(who) "Hello, #{who}!" -end \ No newline at end of file +end diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb deleted file mode 100644 index bce2ab169..000000000 --- a/01_temperature/temperature.rb +++ /dev/null @@ -1,19 +0,0 @@ -def ftoc(f) - # if f == 32 - # 0 - # elsif f == 212 - # 100 - # elsif f == 98.6 - # 37 - # else - # 20 - # end - ((f - 32.0)/(9.0/5.0)).round -end - -def ctof(c) - f = ((c*9.0/5) + 32) - # ((c + 32.0)/(9.0/5.0)).round -end - - diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb deleted file mode 100644 index 7734eb466..000000000 --- a/02_calculator/calculator.rb +++ /dev/null @@ -1,15 +0,0 @@ -def add (x,y) - sum = x + y #why does this pass all addition tests? -end - -def subtract (x,y) - sum = x - y -end - -def sum(arr) - sum = 0 - arr.each do |x| - sum += x - end - sum -end diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb deleted file mode 100644 index 031e98e5d..000000000 --- a/03_simon_says/simon_says.rb +++ /dev/null @@ -1,35 +0,0 @@ -def echo(x) - x -end - -def shout(x) - x.upcase -end - -# def repeat(x, times = 2) -# ([x] * times).join(" ") #why does array [x] === "hello hello hello"? -# end - -def repeat(x, times = 2) - ([x] * times).join(' ') -end - -def start_of_word(s, n) - s[0...n] -end - -def first_word(s) - s.split(" ").first -end - -def titleize(t) - little_words = ['and', 'the', 'over'] - t.capitalize.split(" ").map { |word| - if little_words.include?(word) - word - else - word.capitalize - end - }.join(" ") - -end \ No newline at end of file diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb deleted file mode 100644 index fdfb08bbf..000000000 --- a/04_pig_latin/pig_latin.rb +++ /dev/null @@ -1,15 +0,0 @@ -def translate str - alpha = ('a'..'z').to_a - vowels = %w[a e i o u] - consonants = alpha - vowels - - if vowels.include?(str[0]) - str + 'ay' - elsif consonants.include?(str[0]) && consonants.include?(str[1]) - str[2..-1] + str[0..1] + 'ay' - elsif consonants.include?(str[0]) - str[1..-1] + str[0] + 'ay' - else - str # return unchanged - end -end \ No newline at end of file diff --git a/05_silly_blocks/silly_blocks.rb b/05_silly_blocks/silly_blocks.rb deleted file mode 100644 index ef0ed9c3d..000000000 --- a/05_silly_blocks/silly_blocks.rb +++ /dev/null @@ -1,11 +0,0 @@ -def reverser(&block) - yield.split(" ").map(&:reverse).join(" ") -end - -def adder(num=1, &block) - block.call + num -end - -def repeater(n=1, &block) - n.times &block -end \ No newline at end of file diff --git a/06_performance_monitor/performance_monitor.rb b/06_performance_monitor/performance_monitor.rb deleted file mode 100644 index ff7c53ab7..000000000 --- a/06_performance_monitor/performance_monitor.rb +++ /dev/null @@ -1,31 +0,0 @@ -# def measure n = 1 -# total_time = 0 -# start_time = Time.now -# n.times { yield } -# end_time = Time.now -# total_time += end_time - start_time -# total_time / n -# end - -def measure(repeat = 1) - start_time = Time.now - - repeat.times { |x| - yield x - } - end_time = Time.now - elapsed_time = (end_time - start_time)/repeat -end - -# def measure count=1 -# total_time = 0 -# count.times do -# start_time = Time.now - -# yield - -# end_time = Time.now -# total_time += end_time - start_time -# end -# total_time / count -# end \ No newline at end of file diff --git a/07_hello_friend/friend.rb b/07_hello_friend/friend.rb deleted file mode 100644 index df912d3d7..000000000 --- a/07_hello_friend/friend.rb +++ /dev/null @@ -1,8 +0,0 @@ -class Friend - def greeting(name='') - name = ", #{name}" unless name.empty? - "Hello#{name}!" - end -end - - diff --git a/08_book_titles/book.rb b/08_book_titles/book.rb deleted file mode 100644 index 21c148efa..000000000 --- a/08_book_titles/book.rb +++ /dev/null @@ -1,17 +0,0 @@ - -class Book - def title=(t) - conjunx_words = ['and','in','the','of','a','an'] - @title = t.capitalize.split(" ").map { |word| - if conjunx_words.include?(word) - word - else - word.capitalize - end - }.join(' ') - end - - def title - @title - end -end \ No newline at end of file diff --git a/09_timer/timer.rb b/09_timer/timer.rb deleted file mode 100644 index d262f8c93..000000000 --- a/09_timer/timer.rb +++ /dev/null @@ -1,44 +0,0 @@ -# class Timer -# attr_accessor :seconds - -# def initialize (seconds = 0) -# @seconds = seconds -# end - -# def time_string -# "00:00:00" -# end - -# def padded(n) -# "#{n}".rjust(2, '0') -# end - - -# end - -class Timer - attr_accessor :seconds - def initialize - @seconds = 0 - end - - def time_string - [hours, minutes, m_seconds].map(&method(:padded)).join(":") - end - - def hours - seconds / 3600 - end - - def minutes - (seconds % 3600)/60 - end - - def m_seconds - (seconds % 60) - end - - def padded(n) - "#{n}".rjust(2, '0') - end -end \ No newline at end of file diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb deleted file mode 100644 index 2c2bfd0f5..000000000 --- a/10_temperature_object/temperature.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Temperature(arg1, f=>{50}) - @f = 50 -end - diff --git a/11_dictionary/dictionary.rb b/11_dictionary/dictionary.rb deleted file mode 100644 index 486503000..000000000 --- a/11_dictionary/dictionary.rb +++ /dev/null @@ -1,41 +0,0 @@ -class Dictionary - - attr_accessor :entries - - def initialize - @entries = {} - end - - def add(x, value = nil) - if x.is_a?(Hash) - x.each do |key, value| - @entries[key] = value - end - else - @entries[x] = nil - end - end - - def keywords - @entries.keys.sort - end - - def include?(x) - @entries.keys.include?(x) ? true : false - end - - def find(x) - answer = {} - @entries.each do |key, value| - if key.include?(x) - answer[key] = value - end - end - answer - end - - def printable - final = @entries.sort.map {|key, value| "[#{key}] \"#{value}\""} - final.join("\n") - end -end \ No newline at end of file From 3470dccb7fc1c27542c8de6b51ea26aca40d2215 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Fri, 23 Sep 2016 12:23:31 -0700 Subject: [PATCH 13/24] redo hw temperature rb --- 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..3ee7e4f70 --- /dev/null +++ b/01_temperature/temperature.rb @@ -0,0 +1,7 @@ +def ftoc(f) + (f-32)*(5.0/9.0) +end + +def ctof(c) + f=(c*(9.0/5.0))+32 +end \ No newline at end of file From b2efd38b210cc6d78ff3fea70341beedc307567c Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Fri, 23 Sep 2016 12:49:25 -0700 Subject: [PATCH 14/24] redo hw calculator rb --- 02_calculator/calculator.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 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..9ff12f667 --- /dev/null +++ b/02_calculator/calculator.rb @@ -0,0 +1,23 @@ +def add(x,y) + sum = x + y +end + +def subtract(x,y) + subtract = x - y +end + +def sum(array) + sum = 0 + array.each do |x| + sum += x + end + sum +end + +def multiply(num1, *nums) + product = num1 + product.each do |x| + pruduct *= x + end + product +end \ No newline at end of file From 76b6e2500555ac4c12c4eeec4de3b0075ad9357d Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Fri, 23 Sep 2016 14:36:34 -0700 Subject: [PATCH 15/24] redo hw simon_says rb --- 03_simon_says/simon_says.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 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..8b7c7e982 --- /dev/null +++ b/03_simon_says/simon_says.rb @@ -0,0 +1,28 @@ +def echo(x) + x +end + +def shout(x) + x.upcase +end + +def repeat(word, n=2) + (word.split* n).join(" ") +end + +def start_of_word(s, n) + s[0...n] +end + +def first_word(word) + word.split[0] +end + +def titleize(word) + word.capitalize! + words = word.split(" ") + words.each do |x| + x.capitalize! unless ["and", "over", "the"].include? x + end + words.join(" ") +end From 1a0c65698023f0b2e6d11c6e7bd58808cc66611c Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Sun, 25 Sep 2016 02:55:37 -0700 Subject: [PATCH 16/24] redo hw pig latin --- 04_pig_latin/pig_latin.rb | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 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..6fdaa9d19 --- /dev/null +++ b/04_pig_latin/pig_latin.rb @@ -0,0 +1,73 @@ +# def translate(arr) +# vowels = %w{a e i o u y} +# consonants = %w{b c d f g h j k l m n p q r s t v w x y z} + +# a = arr.split.map do |word| +# if vowels.include?(word[0]) +# word + "ay" +# elsif word[0..1] == "qu" +# word[2..-1] + "quay" +# elsif word[0..2] == "sch" +# word[3..-1] + "schay" +# elsif word[0..2] == "squ" +# word[3..-1] + "squay" +# elsif consonants.include?(word[0]) && consonants.include?(word[1]) && consonants.include?(word[2]) +# word[3..-1] + word[0..2] + "ay" +# elsif consonants.include?(word[0]) && consonants.include?(word[1]) +# word[2..-1] + word[0..1] + "ay" +# elsif consonants.include?(word[0]) +# word[1..-1] + word[0] + "ay" +# end +# end + +# a.join(" ") +# end + + + # Word_array = Word.split + +def translate(arr) + + alpha = ('a'..'z').to_a + vowels = %w[a e i o u] + consonants = alpha - vowels + + a = arr.split.map do |word| + if vowels.include?(word[0]) + word + 'ay' + elsif word[0..1] == 'qu' + word[2..-1] + 'quay' + elsif word[0..2] == 'squ' + word[3..-1] + 'squay' + elsif consonants.include?(word[0]) && consonants.include?(word[1]) && consonants.include?(word[2]) + word[3..-1] + word[0..2] + 'ay' + elsif consonants.include?(word[0]) && consonants.include?(word[1]) + word[2..-1] + word[0..1] + 'ay' + else consonants.include?(word[0]) + word[1..-1] + word[0] + 'ay' + end + end + + a.join(" ") +end + + + + + +# def consonant?(character) +# Consonants.include?(character) +# end + + + +# def translate(word) +# if Vowels.include?(word[0]) +# word + 'ay' +# elsif Consonants.include?(word[0]) && +# Consonants.include?(word[1]) +# word[2..-1] + word[0..1] + 'ay' +# else Consonants.include?(word[0]) +# word[1..-1] + word[0..0] + 'ay' +# end +# end \ No newline at end of file From d8b22dc61f8544243d3a383a39fb46f897050cdc Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Sun, 25 Sep 2016 19:15:27 -0700 Subject: [PATCH 17/24] redo hw silly blocks --- 05_silly_blocks/silly_blocks.rb | 20 ++++++++++++++++++++ 1 file changed, 20 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..d9d076562 --- /dev/null +++ b/05_silly_blocks/silly_blocks.rb @@ -0,0 +1,20 @@ +def reverser + + words = yield + output = [] + + words.split.map do |word| + output << word.reverse + end + output.join(' ') +end + +def adder(n=1) + yield + n +end + +def repeater(n=1) + n.times do + yield + end +end \ No newline at end of file From 31ba773a1d6847861f721895dfe76263fab5a49b Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Sun, 25 Sep 2016 19:58:37 -0700 Subject: [PATCH 18/24] redo hw performance rb --- 06_performance_monitor/performance_monitor.rb | 10 ++++++++++ 1 file changed, 10 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..3700d3386 --- /dev/null +++ b/06_performance_monitor/performance_monitor.rb @@ -0,0 +1,10 @@ +def measure(n=1) + total = 0 + n.times do + start_time = Time.now + yield + end_time = Time.now + total += end_time - start_time + end + total / n +end \ No newline at end of file From 1d1b663344b1566b41e3e28b9c2e0c76685e8f2a Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Sun, 25 Sep 2016 20:16:03 -0700 Subject: [PATCH 19/24] redo hw friend --- 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..81df5eee7 --- /dev/null +++ b/07_hello_friend/friend.rb @@ -0,0 +1,9 @@ +class Friend + def greeting(who=nil) + if who == nil + "Hello!" + else + "Hello, #{who}!" + end + end +end \ No newline at end of file From 7a1c7ea07501795a4d5b19ecf38144ba30fc4cfe Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 26 Sep 2016 21:57:12 -0700 Subject: [PATCH 20/24] redo hw book --- 08_book_titles/book.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 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..cd4e30ea1 --- /dev/null +++ b/08_book_titles/book.rb @@ -0,0 +1,39 @@ +# class Book + +# attr_accessor :title + +# def title=(some_word) +# # word_arr = some_word.split(' ') + +# # word_arr.map do |e| +# # e[0].capitalize +# @title = some_word.capitalize +# end + +# end + +class Book + def title + @title + end + + def title=(title) + special_words = %w(and in the of a an) + formatted_title = [] + + @title = title.split.each_with_index do |w,i| + case + when i == 0 + formatted_title << w.capitalize + + when i > 0 && !special_words.include?(w) + formatted_title << w.capitalize + + when special_words.include?(w) + formatted_title << w + end + end + + @title = formatted_title.join(" ") + end +end \ No newline at end of file From 5f9e28c9557705e475b66d3797c5deeedf8e47a5 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 26 Sep 2016 23:12:03 -0700 Subject: [PATCH 21/24] redo hw timer --- 09_timer/timer.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 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..b2cde07aa --- /dev/null +++ b/09_timer/timer.rb @@ -0,0 +1,26 @@ +class Timer + attr_accessor :seconds + def initialize + @seconds = 0 + end + + def time_string + [hours, minutes, m_seconds].map(&method(:padded)).join(":") + end + + def hours + seconds / 3600 + end + + def minutes + (seconds % 3600)/60 + end + + def m_seconds + (seconds % 60) + end + + def padded(n) + "#{n}".rjust(2, '0') + end +end \ No newline at end of file From c1920bc1240078f90a7ba44a9a41950a5db3a0f8 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 26 Sep 2016 23:31:13 -0700 Subject: [PATCH 22/24] redo hw dictionary --- 11_dictionary/dictionary.rb | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 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..cc6b969e4 --- /dev/null +++ b/11_dictionary/dictionary.rb @@ -0,0 +1,55 @@ +#Creates your very own dictionary! +#Add words and definitions, search, and print them out. + +class Dictionary + + attr_accessor :entries + + def initialize(entries = {}) + @entries = entries + end + + def add(entry) + + if entry.class == String + entry = {entry => nil} + end + + entry.each do |key, value| + @entries[key] = value + end + end + + def keywords + keys = [] + + @entries.each do |key, value| + keys << key + end + keys.sort + end + + def include?(key) + @entries.keys.include?(key) + end + + def find(prefix) + found = {} + + @entries.each do |key, value| + if key =~ /^#{prefix}/ + found[key] = value + end + end + found + end + + def printable + print_out = [] + + @entries.sort.collect do |key, value| + print_out << "[#{key}] \"#{value}\"" + end + print_out.join("\n") + end +end \ No newline at end of file From 9d10e6fae6da7acfcbba0265e8f70150c2f7a273 Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 26 Sep 2016 23:47:19 -0700 Subject: [PATCH 23/24] rpn calc --- 10_temperature_object/temperature.rb | 59 +++++++++++++++++++++ 12_rpn_calculator/rpn_calculator.rb | 79 ++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 10_temperature_object/temperature.rb create mode 100644 12_rpn_calculator/rpn_calculator.rb diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..d25f80218 --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,59 @@ +# Did not know what an options was or how to implement it. Found the use case on stack overflow and was able to implement it here. STill a little confusing but I get the general idea to have the option to use a has as an argument in your fuction. I was also able to re-use code from the previous temperature section. +class Temperature + attr_accessor :f, :c + + def initialize (options={}) #Utilized options hash for fahrenheit/celsius methods + @f = options[:f] + @c = options[:c] + + if f + @c = Temperature.ftoc(@f) #Converts fahrenheit to celsius using the Temperature.ftoc. Reused + else + @f = Temperature.ctof(@c) #Converts celcius to fahrenheit using the Temperature.ctof. Reused + end + end + + def in_fahrenheit + @f + end + + def in_celsius + @c + end + + # method to convert fahrenheit to celsius where 1 degree F is 5/9 of 1 degree C. Re-used this code from the first exercise. Took some trial and error to get *self to work but was able to implement. I understand it's useful when there are instance methods, class methods and variables in order to distinguish your class method. Still not 100% clear on it but makes sense in this example. + def self.ftoc(f) + (f-32) * (5.0/9) + end + + # method to convert celcius to fahrenheit + def self.ctof(c) + (c * (9/5.0)) + 32 + end + + # Factory Methods + def self.from_fahrenheit(temp) + self.new(:f => temp) #called on the Temperature class + end + + def self.from_celsius(temp) + self.new(:c => temp) #called on the Temperature class + end +end + +class Fahrenheit < Temperature #Fahrenheit subclass + + def initialize(temp) + @f = temp + @c = Temperature.ftoc(@f) + end +end + +class Celsius < Temperature #Celsius subclass + + def initialize(temp) + @c = temp + @f = Temperature.ctof(@c) + end + +end \ No newline at end of file diff --git a/12_rpn_calculator/rpn_calculator.rb b/12_rpn_calculator/rpn_calculator.rb new file mode 100644 index 000000000..17f37030d --- /dev/null +++ b/12_rpn_calculator/rpn_calculator.rb @@ -0,0 +1,79 @@ +class RPNCalculator + attr_accessor :calculator + + def initialize + @calculator = [] #set calculator instance to an empty array to hold values + end + + def push(num) + @calculator << num #adds the num to the calculator instance + end + + def plus + if @calculator.length < 2 #Check if there's anything to add + # puts "There's nothing to add!" + raise "calculator is empty" #Raise exception if there is nothing there + else + @calculator << @calculator.pop + @calculator.pop #adds the sum of the popped numbers to the @calculator instance + end + end + + def minus + if @calculator.length < 2 #Check if there's anything to subtract + # puts "There's nothing to add!" + raise "calculator is empty" #Raise exception if there is nothing there + else + num1 = @calculator.pop + num2 = @calculator.pop + @calculator << num2 - num1 #adds the value remaining of the subtraction of popped numbers to the @calculator instance + end + end + + def times + if @calculator.length < 2 #Check if there's anything to multiply + # puts "There's nothing to add!" + raise "calculator is empty" #Raise exception if there is nothing there + else + num1 = @calculator.pop + num2 = @calculator.pop + @calculator << num1 * num2 #adds the product of popped numbers to the @calculator instance + end + end + + def divide + if @calculator.length < 2 #Check if there's anything to multiply + # puts "There's nothing to add!" + raise "calculator is empty" #Raise exception if there is nothing there + else + num1 = @calculator.pop.to_f + num2 = @calculator.pop.to_f + @calculator << (num2 / num1) #adds the quotient of popped numbers to the @calculator instance (used the method .to_f to make sure it's a floating point) + end + end + + def value + @calculator.last #return last value of the array which is the calculator's most recent value + end + + def tokens(string) + string.split(" ").collect do |token| #split string into an array and iterate over the words. Initially tried + if token == "+" || token == "-" || token == "*" || token == "/" #this with .each but did not work. Found out + token.to_sym # that you should use .collect when you want to actually change array + else + token.to_i #if statement matches the special characters and if there's a match change the character + end #to symbol terminology a la ":)". If not then just return the integer value of token. I + end #admit I initially tried to use ":" + "token" but this was a pretty cool hack that I found. + end + + def evaluate(string) + @calculator = [] #assign an empty string to keep track of evaluations + tokens(string).each do |token| + push token if token.is_a?(Numeric) # if token is a number add it to the calculator + plus if token == :+ # add if token is the :+ symbol + minus if token == :- # subtract if token is :- symbol + times if token == :* # multiply if token is :* symbol + divide if token == :/ # divide if token is :/ symbol + end + value + end +end \ No newline at end of file From 529a42ce47abd39596290068cbd0f0a67f007d1e Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Mon, 26 Sep 2016 23:59:18 -0700 Subject: [PATCH 24/24] array extentions --- 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..c13876ed7 --- /dev/null +++ b/14_array_extensions/array_extensions.rb @@ -0,0 +1,19 @@ +# Re-used some code from the original app academy test here +class Array + + def sum + result = 0 + self.each do |num| #Iterate through every number on the list using self (instance of the class) + result += num # add each number to the result + end + result # return result which is the sum of all the numbers we iterated over + end + + def square + self.collect{ |num| num*num} # iterate through each number and produce a new array with the + end # square of that number (num*num) using collect method from before + + def square! + self.collect!{ |num| num*num} # iterate through every number of original array (square!) and produce a + end # new array with the square of that number (num*num) +end # ! modifies the original object it was called on (stackoverflow) \ No newline at end of file