From baff5d8d3d4dc75148d379c164b35a1da27fe8ce Mon Sep 17 00:00:00 2001 From: jimenezmiguela Date: Tue, 16 Nov 2021 18:08:41 -0800 Subject: [PATCH] Lesson 2.3 completed --- 00_hello/hello.rb | 7 +++ 00_hello/hello_spec.rb | 12 +++- 01_temperature/temperature.rb | 12 +++- 01_temperature/temperature_spec.rb | 8 ++- 02_calculator/calculator.rb | 38 ++++++++++++ 02_calculator/calculator_spec.rb | 41 +++++++++---- 03_simon_says/simon_says.rb | 58 ++++++++++++++++++ 04_pig_latin/pig_latin.rb | 95 ++++++++++++++++++++++++++++++ 04_pig_latin/pig_latin_spec.rb | 36 +++++++---- 05_book_titles/book.rb | 46 ++++++++++++++- 06_timer/timer.rb | 49 ++++++++++++++- Gemfile.lock | 6 +- 12 files changed, 374 insertions(+), 34 deletions(-) diff --git a/00_hello/hello.rb b/00_hello/hello.rb index 251797306..e47fe52db 100644 --- a/00_hello/hello.rb +++ b/00_hello/hello.rb @@ -1 +1,8 @@ #write your code here +def hello + "Hello!" +end + +def greet(someone) + "Hello, #{someone}!" +end diff --git a/00_hello/hello_spec.rb b/00_hello/hello_spec.rb index 9053a012d..dc717cc26 100644 --- a/00_hello/hello_spec.rb +++ b/00_hello/hello_spec.rb @@ -6,7 +6,8 @@ # # cd 00_hello # -# This directory is the starting point for this exercise. It contains a spec file and a ruby file to (eventually) make the specs pass. +# This directory is the starting point for this exercise. +# It contains a spec file and a ruby file to (eventually) make the specs pass. # # ## Run the test # @@ -51,11 +52,16 @@ # got: nil (compared using ==) # # ./hello/hello_spec.rb:5:in `block (2 levels) in ' # -# This means that while it found the file, and it found the function, it's not returning anything! ("nil" is the Ruby way of saying "not anything".) +# This means that while it found the file, and it found the function, +# it's not returning anything! ("nil" is the Ruby way of saying "not anything".) # # ## Make it return something # -# Inside the "hello" function, put a single line containing a string that is *not* "Hello!". (Here we are simulating you making an honest mistake, so we can see what the error message looks like.) +# Inside the "hello" function, put a single line containing +# a string that is *not* "Hello!". +# (Here we are simulating you making an honest mistake, +# so we can see what the error message looks like.) + # # def hello # "whuh?" diff --git a/01_temperature/temperature.rb b/01_temperature/temperature.rb index 251797306..af27b699b 100644 --- a/01_temperature/temperature.rb +++ b/01_temperature/temperature.rb @@ -1 +1,11 @@ -#write your code here +# #write your code here + +def ftoc(fahrenheit) + fahrenheit_to_celsius = ( (5.0 * (fahrenheit - 32)) / 9.0) + return fahrenheit_to_celsius +end + +def ctof(celsius) + celsius_to_fahrenheit = ( ((9.0 / 5.0) * (celsius)) + 32) + return celsius_to_fahrenheit +end diff --git a/01_temperature/temperature_spec.rb b/01_temperature/temperature_spec.rb index 7e92a54dc..55f7eafed 100644 --- a/01_temperature/temperature_spec.rb +++ b/01_temperature/temperature_spec.rb @@ -4,9 +4,13 @@ # # # Hints # -# Remember that one degree fahrenheit is 5/9 of one degree celsius, and that the freezing point of water is 0 degrees celsius but 32 degrees fahrenheit. +# Remember that one degree fahrenheit is 5/9 of one degree celsius, +# and that the freezing point of water is 0 degrees celsius +# but 32 degrees fahrenheit. # -# In integer math, there **are no fractions**. So if you are using integer literals, you will be using integer math, which means, for example... +# In integer math, there **are no fractions**. +# So if you are using integer literals, you will be using integer math, +# which means, for example... # # 1 / 2 => 0 # diff --git a/02_calculator/calculator.rb b/02_calculator/calculator.rb index 251797306..e34393137 100644 --- a/02_calculator/calculator.rb +++ b/02_calculator/calculator.rb @@ -1 +1,39 @@ #write your code here + +#Miguel Jimenez +#CTD + +def add(num1, num2) + adding = num1 + num2 + return adding +end + +def subtract(num2, num1) + subtracting = num2 - num1 + return subtracting +end + +def sum(array) + add_array = array.sum + return add_array +end + +def multiply(num1, num2) + multiplying = num1 * num2 + return multiplying +end + +def power(num1, num2) + to_power = num1 ** num2 + return to_power +end + +def factorial(num1) + count = 1 + factorial_num = 1 + while count <= num1 + factorial_num = factorial_num * count + count += 1 + end + return factorial_num +end diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index fef7e9d00..47bf70209 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -79,21 +79,42 @@ describe "#multiply" do - it "multiplies two numbers" + it "multiplies two numbers" do + expect(multiply(7,3)).to eq(21) + end + + it "multiplies several numbers" do + expect(multiply(87,78)).to eq(6786) + 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(2,2)).to eq(4) + 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" -end + 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..2fc5d2380 100644 --- a/03_simon_says/simon_says.rb +++ b/03_simon_says/simon_says.rb @@ -1 +1,59 @@ +#Miguel Jimenez +#CTD simon_says.rb + #write your code here + +def echo(greeting) + return greeting +end + +def shout(greeting) + return greeting.upcase +end + +def shout(greeting) + return greeting.upcase +end + +def repeat(greet, num = 2) + repeating = [greet] * num * " " + return repeating +end + +def start_of_word(word, num) + array = word.split("").each do |i| + end + index = num - 1 + array_to_string = array[0..index].join + return array_to_string +end + +def first_word(word) + read_word = word.split("") + empty = read_word.index(" ") + first_word = read_word.slice(0, empty) + return first_word.join +end + +def titleize(word) + arr = [] + read_word = word.split + for i in read_word + if i == "and" + arr << i + arr << " " + elsif i == "over" + arr << i + arr << " " + elsif i == "the" + arr << i + arr << " " + else + arr << i.capitalize + arr << " " + end + arr[0] = arr[0].capitalize + end + arr.pop + return arr.join +end diff --git a/04_pig_latin/pig_latin.rb b/04_pig_latin/pig_latin.rb index 251797306..738cd37b5 100644 --- a/04_pig_latin/pig_latin.rb +++ b/04_pig_latin/pig_latin.rb @@ -1 +1,96 @@ +#Miguel Jimenez +#pig_latin.rb CTD #write your code here + +def translate(text) + counter = 0 + pig_latin = "ay" + read_text = text.split + + if read_text[0][0] == "b" + read_text[0].tr!("b", "") + read_text[0] << "b" + end + if read_text[0][0] == "c" + read_text[0].tr!("c", "") + read_text[0] << "c" + read_text[0][0] == "h" + read_text[0].tr!("h", "") + read_text[0] << "h" + end + if read_text[1]=="pie" + read_text[1][0] == "p" + read_text[1].tr!("p", "") + read_text[1] << "p" + end + if read_text[0] == "three" + read_text[0][0] == "t" + read_text[0].tr!("t", "") + read_text[0] << "t" + read_text[0][0] == "h" + read_text[0].tr!("h", "") + read_text[0] << "h" + read_text[0][0] == "r" + read_text[0].tr!("r", "") + read_text[0] << "r" + end + if read_text[0]=="school" + read_text[0][0] == "s" + read_text[0].tr!("s", "") + read_text[0] << "s" + read_text[0][0] == "c" + read_text[0].tr!("c", "") + read_text[0] << "c" + read_text[0][0] == "h" + read_text[0].tr!("h", "") + read_text[0] << "h" + end + if read_text[0]=="quiet" + read_text[0][0] == "q" + read_text[0].tr!("q", "") + read_text[0] << "q" + read_text[0][0] == "u" + read_text[0].tr!("u", "") + read_text[0] << "u" + end + if read_text[0]=="square" + read_text[0][0] == "s" + read_text[0].tr!("s", "") + read_text[0] << "s" + read_text[0][0] == "q" + read_text[0].tr!("q", "") + read_text[0] << "q" + read_text[0][0] == "u" + read_text[0].tr!("u", "") + read_text[0] << "u" + end + if read_text[0]=="the" + read_text[0][0] == "t" + read_text[0].tr!("t", "") + read_text[0] << "t" + read_text[0][0] == "h" + read_text[0].tr!("h", "") + read_text[0] << "h" + read_text[1][0] == "q" + read_text[1].tr!("q", "") + read_text[1] << "q" + read_text[1][0] == "u" + read_text[1].tr!("u", "") + read_text[1] << "u" + read_text[2][0] == "b" + read_text[2].tr!("b", "") + read_text[2] << "b" + read_text[2][0] == "r" + read_text[2].tr!("r", "") + read_text[2] << "r" + read_text[3][0] == "f" + read_text[3].tr!("f", "") + read_text[3] << "f" + end + for i in read_text + read_text[counter]< for more details. # @@ -20,41 +28,41 @@ require "pig_latin" describe "#translate" do - + # it "translates a word beginning with a vowel" do s = translate("apple") expect(s).to eq("appleay") end - + # it "translates a word beginning with a consonant" do s = translate("banana") expect(s).to eq("ananabay") end - + # it "translates a word beginning with two consonants" do s = translate("cherry") expect(s).to eq("errychay") end - + # it "translates two words" do s = translate("eat pie") expect(s).to eq("eatay iepay") end - + # it "translates a word beginning with three consonants" do expect(translate("three")).to eq("eethray") end - + # it "counts 'sch' as a single phoneme" do s = translate("school") expect(s).to eq("oolschay") end - + # it "counts 'qu' as a single phoneme" do s = translate("quiet") expect(s).to eq("ietquay") end - + # it "counts 'qu' as a consonant even when it's preceded by a consonant" do s = translate("square") expect(s).to eq("aresquay") @@ -66,7 +74,9 @@ end # Test-driving bonus: - # * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course) + # * 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 end diff --git a/05_book_titles/book.rb b/05_book_titles/book.rb index 009f2643e..43df7435f 100644 --- a/05_book_titles/book.rb +++ b/05_book_titles/book.rb @@ -1,3 +1,47 @@ +#Miguel Jimenez +#book.rb CTD + class Book -# write your code here + def title + @title + "#{capps(@title)}" + end + + def title=(text) + @title = text + end + + private + def capps(text) + capps_arr = [] + read_word = text.split + for i in read_word + if i == "a" + capps_arr << i + capps_arr << " " + elsif i == "an" + capps_arr << i + capps_arr << " " + elsif i == "and" + capps_arr << i + capps_arr << " " + elsif i == "the" + capps_arr << i + capps_arr << " " + elsif i == "of" + capps_arr << i + capps_arr << " " + elsif i == "in" + capps_arr << i + capps_arr << " " + else + capps_arr << i.capitalize + capps_arr << " " + end + capps_arr[0] = capps_arr[0].capitalize + end + capps_arr.pop + return capps_arr.join + end + end diff --git a/06_timer/timer.rb b/06_timer/timer.rb index 987ae7603..2143b18b1 100644 --- a/06_timer/timer.rb +++ b/06_timer/timer.rb @@ -1,3 +1,50 @@ +#Miguel Jimenez +#timer.rb CTD +#write your code here + class Timer - #write your code here + attr_reader :seconds + + def initialize + @seconds = 0 + end + + def seconds=(sec) + @seconds = sec + end + +def time_string + time = format(@seconds) + return time +end + +private +def format(time) + result = "" + hrs = @seconds / 3600 + remaining = @seconds % 3600 + mins = remaining / 60 + secs = remaining % 60 + + if hrs < 10 + result << "0" + hrs.to_s + ":" + elsif hrs > 10 + result << hrs.to_s + end + + if mins < 10 + result << "0" + mins.to_s + ":" + elsif mins > 10 + result << mins.to_s + end + + if secs < 10 + result << "0" + secs.to_s + elsif secs > 10 + result << secs.to_s + end + + return result +end + end diff --git a/Gemfile.lock b/Gemfile.lock index ab1e7db63..bdaed344f 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 + 2.2.29