From 9ee5b541794d4cb8c61ebbfd39abd4d13dff4c01 Mon Sep 17 00:00:00 2001 From: M King Date: Tue, 20 Apr 2021 20:31:08 -0700 Subject: [PATCH] submit week4 assignment --- 00_hello/hello.rb | 7 ++++ 01_temperature/temperature.rb | 7 ++++ 02_calculator/calculator.rb | 31 ++++++++++++++++++ 02_calculator/calculator_spec.rb | 35 +++++++++++++------- 03_simon_says/simon_says.rb | 35 ++++++++++++++++++++ 04_pig_latin/pig_latin.rb | 55 ++++++++++++++++++++++++++++++++ 04_pig_latin/pig_latin_spec.rb | 10 ++++++ 05_book_titles/book.rb | 35 +++++++++++++++++++- 06_timer/timer.rb | 18 +++++++++++ 06_timer/timer_spec.rb | 22 ++++++------- Gemfile.lock | 6 ++-- 11 files changed, 235 insertions(+), 26 deletions(-) diff --git a/00_hello/hello.rb b/00_hello/hello.rb index 251797306..0d9042d30 100644 --- a/00_hello/hello.rb +++ b/00_hello/hello.rb @@ -1 +1,8 @@ #write your code here +def hello + "Hello!" +end + +def greet(name) + "Hello, #{name}!" +end \ No newline at end of file diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb index 251797306..ada8c5768 100644 --- a/01_temperature/temperature.rb +++ b/01_temperature/temperature.rb @@ -1 +1,8 @@ #write your code here +def ftoc(temperature) + return (temperature - 32) * 5 / 9 +end + +def ctof(temperature) + return temperature * 9.0 / 5 + 32 +end \ No newline at end of file diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb index 251797306..926f966ae 100644 --- a/02_calculator/calculator.rb +++ b/02_calculator/calculator.rb @@ -1 +1,32 @@ #write your code here +def add(num1, num2) + return num1 + num2 +end + +def subtract(num1, num2) + return num1 - num2 +end + +def sum(array) + return array.sum +end + +def multiply(*nums) #ruby treats "*nums" as an array + multiply = 1 + nums.each do |n| + multiply *= n + end + return multiply +end + +def power(num1, num2) + num1**num2 +end + +def factorial(num) + result = 1 + num.times do |number| + result*= (number+1) + end + return result +end diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..e6145e458 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -78,22 +78,35 @@ # write tests and code for the following: describe "#multiply" do - - it "multiplies two numbers" - - it "multiplies several numbers" - + it "multiplies two numbers" do + expect(multiply(2,6)).to eq(12) + end + it "multiplies several numbers" do + expect(multiply(2,6,5)).to eq(60) + end end describe "#power" do - it "raises one number to the power of another number" + it "raises one number to the power of another number" do + expect(power(2,3)).to eq(8) + end end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" - it "computes the factorial of 1" - it "computes the factorial of 2" - it "computes the factorial of 5" - it "computes the factorial of 10" + it "computes the factorial of 0" do + expect(factorial(0)).to eq(1) + end + it "computes the factorial of 1" do + expect(factorial(1)).to eq(1) + end + it "computes the factorial of 2" do + expect(factorial(2)).to eq(2) + end + it "computes the factorial of 5" do + expect(factorial(5)).to eq(120) + end + it "computes the factorial of 10" do + expect(factorial(10)).to eq(3628800) + end end diff --git a/03_simon_says/simon_says.rb b/03_simon_says/simon_says.rb index 251797306..a84ec0d67 100644 --- a/03_simon_says/simon_says.rb +++ b/03_simon_says/simon_says.rb @@ -1 +1,36 @@ #write your code here +def echo(string) + return string +end + +def shout(string) + return string.upcase +end + +def repeat(string, num=2) + result = "" + result = (string + " ") * num + return result.strip +end + +def start_of_word(string, num) + return string[0, num] +end + +def first_word(string) + return string.split[0] +end + +def titleize(string) + little_words= ["and", "over", "the"] + result = "" + string.split.each do |word| + if !little_words.include?(word) + result += word.capitalize + " " + else + result += word + " " + end + end + result = result[0].upcase + result[1,result.length-1] + return result.strip +end \ No newline at end of file diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb index 251797306..c668dbf42 100644 --- a/04_pig_latin/pig_latin.rb +++ b/04_pig_latin/pig_latin.rb @@ -1 +1,56 @@ #write your code here +def translate(string) + result = [] + vowels = ["a", "e", "i", "o", "u"] + + string.split.each do |word| + word_copy = word + min = word.length + vowels.each do |vowel| + if word.downcase.index(vowel) != nil && word.downcase.index(vowel) <=min #find the first vowel's index + min = word.downcase.index(vowel) + end + end + temp = "" #to save the punctuation + if !("a".."z").include?(word[word.length-1]) && !("A".."Z").include?(word[word.length-1]) + temp = word[word.length-1] #save the punctuation + word = word.chop #remove the punctuation + end + new_word = word[min, word.length] + word[0, min] + if new_word[0].downcase == "u" && new_word[new_word.length-1].downcase == "q" #deal with "qu" case + new_word = new_word.delete_prefix("u") + "u" + end + if ("A".."Z").include?(word_copy[0]) #capitalized words were capitalized + new_word = new_word.downcase.capitalize + end + result.push(new_word + "ay" + temp) + end + return result.join(" ") +end + +# Below is the code shared by one student using regex, keep it as reference. +# vowels = ["a", "e", "i", "o", "u"] +# #vowels = 'aeiou'.split('') +# #vowels = %w[a e i o u] + +# def translate (string) +# vowels = %w[a e i o u] +# words = string.split +# new_words = [] + +# words.each do |word| +# new_word = +# if word.match /^.*qu/ +# first_part = word[/^.*qu/] +# last_part = word.delete first_part +# last_part + first_part + 'ay' + +# else !vowels.include?(word[0]) +# first_part = word[/[^aeiou]*/] +# last_part = word.delete first_part +# last_part + first_part + 'ay' +# end +# new_words.push new_word +# end +# new_words.join ' ' +# end \ No newline at end of file diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index cc659edfd..8fbe365a6 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -69,4 +69,14 @@ # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course) # * retain the punctuation from the original phrase + it "capitalize the words that capitalized before" do + s = translate("The Quick Brown Fox") + expect(s).to eq("Ethay Ickquay Ownbray Oxfay") + end + + it "retains the puctuation from the original phrase" do + s = translate("Ha! I got you!") + expect(s).to eq("Ahay! Iay otgay ouyay!") + end + end diff --git a/05_book_titles/book.rb b/05_book_titles/book.rb index 009f2643e..39cf1aa83 100644 --- a/05_book_titles/book.rb +++ b/05_book_titles/book.rb @@ -1,3 +1,36 @@ class Book -# write your code here +# write your code here + attr_accessor :title + def initialize + @title = "" + end + + def title + little_words= ["and", "over", "the", "an", "a", "in", "of"] + result = "" + @title.split.each do |word| + if !little_words.include?(word) + result += word.capitalize + " " + else + result += word + " " + end + end + result = result[0].upcase + result[1,result.length-1] + title = result.strip + end end + +#Another student's code using each_with_index method +# def title +# little_words = ["and","the","a","an","in","of","in"] +# c= "" +# @title.split.each_with_index do |string, index| +# if index == 0 +# c=string.capitalize +# elsif little_words.include? string +# c+= " " + string +# else c+= " " + string.capitalize +# end +# end +# return c +# end diff --git a/06_timer/timer.rb b/06_timer/timer.rb index 987ae7603..b44bcd2d9 100644 --- a/06_timer/timer.rb +++ b/06_timer/timer.rb @@ -1,3 +1,21 @@ class Timer #write your code here + attr_accessor :seconds + + def initialize + @seconds = 0 + end + + def time_string + hours = @seconds / 60 / 60 + seconds = @seconds % 60 + tmp = @seconds / 60 + minutes = tmp % 60 + + padded(hours) + ":" + padded(minutes) + ":" + padded(seconds) + end + + def padded(num) + return result = num >= 10 ? num.to_s : "0#{num}" + end end diff --git a/06_timer/timer_spec.rb b/06_timer/timer_spec.rb index 40b33f23f..0ae1dd31f 100644 --- a/06_timer/timer_spec.rb +++ b/06_timer/timer_spec.rb @@ -45,16 +45,16 @@ # 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 diff --git a/Gemfile.lock b/Gemfile.lock index ab1e7db63..bbb5dd832 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,7 @@ GEM remote: https://rubygems.org/ specs: diff-lcs (1.3) - rake (10.5.0) + rake (12.3.3) rspec (3.5.0) rspec-core (~> 3.5.0) rspec-expectations (~> 3.5.0) @@ -21,8 +21,8 @@ PLATFORMS ruby DEPENDENCIES - rake (< 11.0) + rake (~> 12.3) rspec (~> 3.4) BUNDLED WITH - 1.13.6 + 1.17.3