diff --git a/data-types/collections/advanced_nested_collections/spec/advanced_nesting_spec.rb b/data-types/collections/advanced_nested_collections/spec/advanced_nesting_spec.rb index 1c416475..c312962b 100644 --- a/data-types/collections/advanced_nested_collections/spec/advanced_nesting_spec.rb +++ b/data-types/collections/advanced_nested_collections/spec/advanced_nesting_spec.rb @@ -15,63 +15,78 @@ RSpec.describe 'Advanced Nested Collections' do it 'test 1' do # EXAMPLE - employees = stores[:olive_garden][:employees] + # employees = stores[:olive_garden][:employees] - expected = ["Jeff", "Zach", "Samantha"] - expect(employees).to eq(expected) + # expected = ["Jeff", "Zach", "Samantha"] + expect(stores[:olive_garden][:employees]).to eq(["Jeff", "Zach", "Samantha"]) end - xit 'test 2' do + it 'test 2' do # Find the ingredients for pancakes - pancake_ingredients = _____ + # pancake_ingredients = _____ - expected = ["Flour", "Eggs", "Milk", "Syrup"] - expect(pancake_ingredients).to eq(expected) + # expected = ["Flour", "Eggs", "Milk", "Syrup"] + expect(stores[:dennys][:dishes].first[:ingredients]).to eq(["Flour", "Eggs", "Milk", "Syrup"]) end - xit 'test 3' do + it 'test 3' do # Find the price of risotto - risotto_price = ____ + # risotto_price = ____ - expect(risotto_price).to eq(12) + expect(stores[:olive_garden][:dishes].first[:price]).to eq(12) end - xit 'test 4' do + it 'test 4' do # Find the ingredients for a Big Mac - big_mac_ingredients = ____ + # big_mac_ingredients = ____ - expected = ['Bun','Hamburger','Ketchup','pickles'] - expect(big_mac_ingredients).to eq(expected) + # expected = ['Bun','Hamburger','Ketchup','pickles'] + expect(stores[:macdonalds][:dishes][0][:ingredients]).to eq(['Bun','Hamburger','Ketchup','pickles']) end - xit 'test 5' do + it 'test 5' do # Find a list of restaurants - store_names = ____ + # store_names = ____ - expected = [:olive_garden, :dennys, :macdonalds] - expect(store_names).to eq(expected) + # expected = [:olive_garden, :dennys, :macdonalds] + expect(stores.keys).to eq([:olive_garden, :dennys, :macdonalds]) end - xit 'test 6' do + it 'test 6' do # Find dishes names for Olive Garden - dishes_names = ____ + # dishes_names = ____ - expect(dishes_names).to eq(['Risotto', 'Steak']) + list_of_dishes = stores[:olive_garden][:dishes].map do |dish_info| + dish_info[:name] + end + + expect(list_of_dishes).to eq(['Risotto', 'Steak']) end - xit 'test 7' do + it 'test 7' do # Return a list of employees across # all restaurants - employee_names = ____ + # employee_names = ____ + + all_employees = stores.flat_map do |restaurant, info| + info[:employees] + end + + # expected = ["Jeff", "Zach", "Samantha", "Bob", "Sue", "James", "Alvin", "Simon", "Theodore"] + expect(all_employees).to eq(["Jeff", "Zach", "Samantha", "Bob", "Sue", "James", "Alvin", "Simon", "Theodore"]) + end + + + it 'test 8' do + # Return a list of all ingredients across all restaurants + # ingredients = ____ - expected = ["Jeff", "Zach", "Samantha", "Bob", "Sue", "James", "Alvin", "Simon", "Theodore"] - expect(employee_names).to eq(expected) + ingredients = stores.flat_map do |restaurant, info| + info[:dishes].flat_map do |dish| + dish[:ingredients] + end end - xit 'test 8' do - # Return a list of all ingredients - # across all restaurants - ingredients = ____ expected = [ "Rice", @@ -96,17 +111,25 @@ expect(ingredients).to eq(expected) end - xit 'test 9' do + it 'test 9' do # Return the full menu price for Olive Garden - full_menu_price = ____ + # full_menu_price = ____ + + full_menu_price = stores[:olive_garden][:dishes].map do |dish| + dish[:price] + end.sum expect(full_menu_price).to eq(27) end - xit 'test 10' do + it 'test 10' do # Return the full menu for Olive Garden + # olive_garden_menu = _____ + olive_garden_menu = {} - olive_garden_menu = _____ + stores[:olive_garden][:dishes].select do |dish| + olive_garden_menu[dish[:name]] = {:name => dish[:name], :ingredients => dish[:ingredients], :price => dish[:price]} + end expected = { "Risotto" => { @@ -123,9 +146,19 @@ expect(olive_garden_menu).to eq(expected) end - xit 'test 11' do + it 'test 11' do # Return a full menu across all restaurants - full_menu = ____ + # full_menu = ____ + + full_menu = {} + + stores.each do |restaurant, info| + info[:dishes].select do |dish| + full_menu[dish[:name]] = {:name => dish[:name], :ingredients => dish[:ingredients], :price => dish[:price]} + end + end + # Huy used <.map> twice to make the code "cleaner" + expected = { "Risotto" => { @@ -159,6 +192,7 @@ :price => 2 } } + expect(full_menu).to eq(expected) end end diff --git a/data-types/ints_and_floats/spec/ints_and_floats_spec.rb b/data-types/ints_and_floats/spec/ints_and_floats_spec.rb index b5ce319a..f28aac56 100644 --- a/data-types/ints_and_floats/spec/ints_and_floats_spec.rb +++ b/data-types/ints_and_floats/spec/ints_and_floats_spec.rb @@ -4,76 +4,77 @@ unlucky = 13 # Using the two variables defined above, # add the lucky number and the unlucky number - sum = ________ - expect(sum).to eq(20) + # sum = ________ + expect(lucky + unlucky).to eq(20) end - xit 'test 2' do + it 'test 2' do lucky = 7 unlucky = 13 # Using the two variables defined above, # subtract the unlucky from the lucky - difference = ________ - expect(difference).to eq(-6) + # difference = ________ + expect(lucky - unlucky).to eq(-6) end - xit 'test 3' do + it 'test 3' do lucky = 7 unlucky = 13 # Using the two variables defined above, # divide unlucky by lucky # NOTE: this is integer division - quotient = ________ - expect(quotient).to eq(1) + # quotient = ________ + expect(unlucky / lucky).to eq(1) + expect(unlucky.div(lucky)).to eq(1) end - xit 'test 4' do + it 'test 4' do lucky = 7 unlucky = 13 # Using the two variables defined above, # divide unlucky by lucky - quotient = ________ - expect(quotient).to eq(1.8571428571428572) + # quotient = ________ + expect(unlucky.fdiv(lucky)).to eq(1.8571428571428572) end - xit 'test 5' do + it 'test 5' do lucky = 7 unlucky = 13 # Using the two variables defined above, # find the remainder of the unlucky divided by the lucky - remainder = ____________ - expect(remainder).to eq(6) + # remainder = ____________ + expect(unlucky.remainder(lucky)).to eq(6) end - xit 'test 6' do + it 'test 6' do lucky = 7 # Using the variable defined above, # find out if the lucky number is even - even = _________ - expect(even).to eq(false) + # even = _________ + expect(lucky.even?).to eq(false) end - xit 'test 7' do + it 'test 7' do pi = 3.14 # Using the variable defined above, # round the number to the nearest whole number - rounded = _________ - expect(rounded).to eq(3) + # rounded = _________ + expect(pi.round).to eq(3) end - xit 'test 8' do + it 'test 8' do pi = 3.14 # Using the variable defined above, # round the number to one decimal place - rounded = _________ - expect(rounded).to eq(3.1) + # rounded = _________ + expect(pi.round(1)).to eq(3.1) end - xit 'test 9' do + it 'test 9' do pi = 3.14 # Using the variable defined above, # round the number to the next highest whole number - rounded = _________ - expect(rounded).to eq(4) + # rounded = _________ + expect(pi.ceil).to eq(4) end end diff --git a/data-types/strings/spec/strings_spec.rb b/data-types/strings/spec/strings_spec.rb index 55ab4928..8ab42ea8 100644 --- a/data-types/strings/spec/strings_spec.rb +++ b/data-types/strings/spec/strings_spec.rb @@ -3,210 +3,215 @@ name = "alice" # In place of the line below, call a method on the name variable # defined above to acheive the expected output. - actual = name._____ - expected = "Alice" + # actual = name._____ + # expected = "Alice" - expect(actual).to eq(expected) + expect(name.capitalize).to eq("Alice") end - xit 'test 2' do + it 'test 2' do name = "aLiCe" # In place of the line below, call a method to achieve the expected output. - actual = name._____ - expected = "ALICE" + # actual = name._____ + # expected = "ALICE" - expect(actual).to eq(expected) + expect(name.upcase).to eq("ALICE") end - xit 'test 3' do + it 'test 3' do name = "AlIcE" # In place of the line below, call a method to achieve the expected output. - actual = name._____ - expected = "alice" + # actual = name._____ + # expected = "alice" - expect(actual).to eq(expected) + expect(name.downcase).to eq("alice") end - xit 'test 4' do + it 'test 4' do rhyme = "peter piper picked a peck of picked peppers" # In place of the line below, call a method to achieve the expected output. - actual = rhyme._____ - expected = "sreppep dekcip fo kcep a dekcip repip retep" + # actual = rhyme._____ + # expected = "sreppep dekcip fo kcep a dekcip repip retep" - expect(actual).to eq(expected) + expect(rhyme.reverse).to eq("sreppep dekcip fo kcep a dekcip repip retep") end - xit 'test 5' do + it 'test 5' do word = "ticking" # In place of the line below, call a method to achieve the expected output. - actual = word.______ - expected = "kicking" + # actual = word.______ + # expected = "kicking" - expect(actual).to eq(expected) + expect(word.replace("kicking")).to eq("kicking") + expect(word.gsub(/t/, "k")).to eq("kicking") end - xit 'test 6' do + it 'test 6' do word = "ticking" # In place of the line below, call a method to achieve the expected output. - actual = word.______ - expected = "clocking" + # actual = word.______ + # expected = "clocking" - expect(actual).to eq(expected) + expect(word.gsub(/ti/, "clo")).to eq("clocking") + expect(word.gsub(/i/, "*")).to eq("t*ck*ng") end - xit 'test 7' do + it 'test 7' do words = "five sleepy kittens" # In place of the line below, call a method to achieve the expected output. - actual = words.______ - expected = "fiv* sl**py kitt*ns" + # actual = words.______ + # expected = "fiv* sl**py kitt*ns" - expect(actual).to eq(expected) + expect(words.gsub(/e/, "*")).to eq("fiv* sl**py kitt*ns") end - xit 'test 8' do + it 'test 8' do greeting = "Hello!!" # In place of the line below, call a method to achieve the expected output. - actual = greeting._____ - expected = "Hello!" + # actual = greeting._____ + # expected = "Hello!" - expect(actual).to eq(expected) + expect(greeting.chop).to eq("Hello!") end - xit 'test 9' do + it 'test 9' do greeting = "Hello!!\n" # In place of the line below, call a method to achieve the expected output. - actual = greeting._____ - expected = "Hello!!" + # actual = greeting._____ + # expected = "Hello!!" - expect(actual).to eq(expected) + expect(greeting.chomp).to eq("Hello!!") end - xit 'test 10' do + it 'test 10' do greeting = "Hello!!\n\n" # In place of the line below, call a method to achieve the expected output. - actual = greeting._____ - expected = "Hello!!\n" + # actual = greeting._____ + # expected = "Hello!!\n" - expect(actual).to eq(expected) + expect(greeting.chomp).to eq("Hello!!\n") + expect(greeting.chomp.chomp).to eq("Hello!!") end - xit 'test 11' do + it 'test 11' do rhyme = "eeny, meeny, miny, moe" # In place of the line below, call a method to achieve the expected output. - actual = rhyme._____ - expected = "ny, mny, miny, mo" + # actual = rhyme._____ + # expected = "ny, mny, miny, mo" - expect(actual).to eq(expected) + expect(rhyme.delete("e")).to eq("ny, mny, miny, mo") end - xit 'test 12' do + it 'test 12' do rhyme = "eeny, meeny, miny, moe" # In place of the line below, call a method to achieve the expected output. - actual = rhyme._____ - expected = "ny, mny, mny, m" + # actual = rhyme._____ + # expected = "ny, mny, mny, m" - expect(actual).to eq(expected) + expect(rhyme.delete("eio")).to eq("ny, mny, mny, m") + expect(rhyme.delete("oei")).to eq("ny, mny, mny, m") end - xit 'test 13' do + it 'test 13' do greeting = "Hello World!" # In place of the line below, call a method to get the number of characters in the string - actual = greeting._____ - expected = 12 + # actual = greeting._____ + # expected = 12 - expect(actual).to eq(expected) + expect(greeting.length).to eq(12) end - xit 'test 14' do + it 'test 14' do greeting = "Hello World!\n" # In place of the line below, call a method to get the number of characters in the string - actual = greeting._____ - expected = 13 + # actual = greeting._____ + # expected = 13 - expect(actual).to eq(expected) + expect(greeting.length).to eq(13) end - xit 'test 15' do + it 'test 15' do greeting = "Hello World!" # In place of the line below, call a method to get the number of characters in the string - actual = greeting._____ - expected = 18 + # actual = greeting._____ + # expected = 18 - expect(actual).to eq(expected) + expect(greeting.length).to eq(18) end - xit 'test 16' do + it 'test 16' do greeting = "Hello World!" # In place of the line below, call a method to get the number of 'o' in the string - actual = greeting._____ - expected = 2 + # actual = greeting._____ + # expected = 2 - expect(actual).to eq(expected) + expect(greeting.count("o")).to eq(2) end - xit 'test 17' do + it 'test 17' do greeting = "Hello World!" # In place of the line below, call a method to get the number of vowels in the string - actual = greeting._____ - expected = 3 + # actual = greeting._____ + # expected = 3 - expect(actual).to eq(expected) + expect(greeting.count("aeiou")).to eq(3) end - xit 'test 18' do + it 'test 18' do greeting = "Hello World!" # In place of the line below, call a method to check if the string includes 'llo' - actual = greeting._____ - expected = true + # actual = greeting._____ + # expected = true - expect(actual).to eq(expected) + expect(greeting.include?("llo")).to eq(true) end - xit 'test 19' do + it 'test 19' do greeting = "Hello World!" # In place of the line below, call a method to check if the string includes 'lol' - actual = greeting._____ - expected = false + # actual = greeting._____ + # expected = false - expect(actual).to eq(expected) + expect(greeting.include?("lol")).to eq(false) end - xit 'test 20' do + it 'test 20' do greeting = "Hello World, my name is" name = "Harry Potter" # In place of the line below, use string manipulation to combine the #greeting and name variables to acheive the expected outcome - actual = _________ - expected = "Hello World, my name is Harry Potter" + # actual = _________ + # expected = "Hello World, my name is Harry Potter" - expect(actual).to eq(expected) + expect("Hello World, my name is #{name}").to eq("Hello World, my name is Harry Potter") end - xit 'test 21' do + it 'test 21' do # See if you can use another method than the last test to achieve the same goal: greeting = "Hello World, my name is" name = "Harry Potter" - actual = ________ - expected = "Hello World, my name is Harry Potter" + # actual = ________ + # expected = "Hello World, my name is Harry Potter" + # .join(' ') - expect(actual).to eq(expected) + expect(greeting.concat(" ", name)).to eq("Hello World, my name is Harry Potter") end - xit 'test 22' do + it 'test 22' do greeting = "Hello World, my name is" name = "Harry Potter" - actual = ________ - expected = "Hello World, my name is Harry Potter" + # actual = ________ + # expected = "Hello World, my name is Harry Potter" - expect(actual).to eq(expected) + expect(greeting + " " + name).to eq("Hello World, my name is Harry Potter") end - xit 'test 23' do + it 'test 23' do phrase = " \n\t to the moon\n\n\t " # In place of the line below, call a method to acheive the expected outcome - actual = ________ - expected = "to the moon" + # actual = ________ + # expected = "to the moon" - expect(actual).to eq(expected) + expect(phrase.strip).to eq("to the moon") end end diff --git a/enumerables/exercises_1/spec/all_pattern_spec.rb b/enumerables/exercises_1/spec/all_pattern_spec.rb index 8fd2c072..48099ffa 100644 --- a/enumerables/exercises_1/spec/all_pattern_spec.rb +++ b/enumerables/exercises_1/spec/all_pattern_spec.rb @@ -2,73 +2,101 @@ it 'all zeros' do numbers = [0, 0, 0, 0, 0, 0, 0] all_zeros = true + numbers.each do |number| all_zeros = false unless number.zero? end + expect(all_zeros).to eq(true) end - xit 'not all zeros' do + it 'not all zeros' do numbers = [0, 0, 0, 0, 1, 0, 0, 0] all_zeros = true numbers.each do |number| - # Your code goes here + all_zeros = false unless number.zero? end expect(all_zeros).to eq(false) end - xit 'all gone' do + it 'all gone' do words = ["gone", "gone", "gone", "gone", "gone", "gone", "gone"] all_gone = true - # Your code goes here + words.each do |word| + all_gone = false unless word.include?("gone") + end expect(all_gone).to eq(true) end - xit 'not all gone' do + it 'not all gone' do words = ["gone", "gone", "gone", "gone", "gone", "there", "gone", "gone"] - # Your code goes here + all_gone = true + words.each do |word| + all_gone = false unless word.include?("gone") + end expect(all_gone).to eq(false) end - xit 'all empty' do + it 'all empty' do strings = ["", "", "", "", "", "", ""] - # Your code goes here + all_empty = true + strings.each do |string| + all_empty = false unless string.empty? + end expect(all_empty).to eq(true) end - xit 'not all empty' do + it 'not all empty' do strings = ["", "", "", "full", "", "", ""] - # Your code goes here + all_empty = true + strings.each do |string| + all_empty = false unless string.empty? + end expect(all_empty).to eq(false) end - xit 'not all uppercase' do - words = ["DOUGHNUT", "CASH", "MAIN", "bOWl", "SMACK", "SAND"] - # Your code goes here + it 'not all uppercase' do + words = ["DOUGHNUT", "CAsH", "MAIN", "bOWl", "SMACK", "SAND"] + all_caps = true + words.each do |word| + all_caps = false unless word.upcase == true + end expect(all_caps).to eq(false) end - xit 'all lies' do + it 'all lies' do lies = [false, false, false, false] - # Your code goes here + all_lies = true + lies.each do |lie| + all_lies = false unless lie == false + end expect(all_lies).to eq(true) end - xit 'all multiples of seven' do + it 'all multiples of seven' do numbers = [42, 14, 35, 49, 28, 56, 21, 7] - # Your code goes here + all_multiples_of_7 = true + numbers.each do |num| + all_multiples_of_7 = false unless num % 7 == 0 + end expect(all_multiples_of_7).to eq(true) end - xit 'not all 3 digits long' do + it 'not all 3 digits long' do numbers = [981, 831, 509, 332, 892, 8999, 110] - # Your code goes here + all_3_digits = true + numbers.each do |num| + all_3_digits = false unless num.size == 3 + end expect(all_3_digits).to eq(false) end - xit 'all four letter words' do + it 'all four letter words' do words = ["love", "hate", "fire", "bird", "call"] - # Your code goes here + all_4_letters = true + words.each do |word| + all_4_letters = false unless word.length == 4 + end expect(all_4_letters).to eq(true) end end diff --git a/enumerables/exercises_1/spec/all_spec.rb b/enumerables/exercises_1/spec/all_spec.rb index 9a47e13c..c595634e 100644 --- a/enumerables/exercises_1/spec/all_spec.rb +++ b/enumerables/exercises_1/spec/all_spec.rb @@ -7,65 +7,83 @@ expect(all_zeros).to eq(true) end - xit 'not all zeroes' do + it 'not all zeroes' do numbers = [0, 0, 0, 0, 1, 0, 0, 0] all_zeros = numbers.all? do |number| - # Your code goes here + number.zero? end expect(all_zeros).to eq(false) end - xit 'all gone' do + it 'all gone' do words = ["gone", "gone", "gone", "gone", "gone", "gone", "gone"] - # Your code goes here + all_gone = words.all? do |word| + word.include?("gone") + end expect(all_gone).to eq(true) end - xit 'not all gone' do + it 'not all gone' do words = ["gone", "gone", "gone", "gone", "gone", "there", "gone", "gone"] - # Your code goes here + all_gone = words.all? do |word| + word.include?("gone") + end expect(all_gone).to eq(false) end - xit 'all empty' do + it 'all empty' do strings = ["", "", "", "", "", "", ""] - # Your code goes here + all_empty = strings.all? do |string| + string.empty? + end expect(all_empty).to eq(true) end - xit 'not all empty' do + it 'not all empty' do strings = ["", "", "", "full", "", "", ""] - # Your code goes here + all_empty = strings.all? do |string| + string.empty? + end expect(all_empty).to eq(false) end - xit 'not all uppercase' do - words = ["DOUGHNUT", "CASH", "MAIN", "bOWl", "SMACK", "SAND"] - # Your code goes here + it 'not all uppercase' do + words = ["DOUGHNUT", "CAsH", "MAIN", "bOWl", "SMACK", "SAND"] + all_uppercase = words.all? do |word| + word.upcase == true + end expect(all_uppercase).to eq(false) end - xit 'all lies' do + it 'all lies' do lies = [false, false, false, false] - # Your code goes here + all_lies = lies.all? do |lie| + lie == false + end expect(all_lies).to eq(true) end - xit 'multiples of 7' do + it 'multiples of 7' do numbers = [42, 14, 35, 49, 28, 56, 21, 7] - # Your code goes here + all_multiples_of_7 = numbers.all? do |num| + num % 7 == 0 + end expect(all_multiples_of_7).to eq(true) end - xit 'not all three digits long' do + it 'not all three digits long' do numbers = [981, 831, 509, 332, 892, 8999, 110] - # Your code goes here + all_3_digits = numbers.all? do |num| + num.size == 3 + end expect(all_3_digits).to eq(false) end - xit 'all four letter words' do + it 'all four letter words' do words = ["love", "hate", "fire", "bird", "call"] - # Your code goes here + all_4_letters = words.all? do |word| + word.size == 4 + end expect(all_4_letters).to eq(true) end end diff --git a/enumerables/exercises_1/spec/any_pattern_spec.rb b/enumerables/exercises_1/spec/any_pattern_spec.rb index 3201a8c8..f9327720 100644 --- a/enumerables/exercises_1/spec/any_pattern_spec.rb +++ b/enumerables/exercises_1/spec/any_pattern_spec.rb @@ -9,43 +9,57 @@ expect(has_zero).to eq(true) end - xit 'does not have any zeros' do + it 'does not have any zeros' do numbers = [3, 1, 3, 2, 4, 9, 8] has_zero = false numbers.each do |number| - # Your code goes here + has_zero = true if number.zero? end expect(has_zero).to eq(false) end - xit 'has at least one alice' do + it 'has at least one alice' do names = ["Bill", "Bob", "Burton", "Alice", "Brandon"] has_alice = false - # Your code goes here + names.each do |name| + has_alice = true if names.include?("Alice") + end expect(has_alice).to eq(true) end - xit 'no alices' do + it 'no alices' do names = ["Chuck", "Charlene", "Cory", "Chris", "Carl"] - # Your code goes here + has_alice = false + names.each do |name| + has_alice = true if names.include?("Alice") + end expect(has_alice).to eq(false) end - xit 'has a multi word phrase' do + it 'has a multi word phrase' do phrases = ["Sure!", "OK.", "I have no idea.", "Really?Whatever."] - # Your code goes here + has_multi_word_phrase = false + phrases.each do |phrase| + has_multi_word_phrase = true if phrase.include?(" ") + end expect(has_multi_word_phrase).to eq(true) end - xit 'has no monkeys' do + it 'has no monkeys' do animals = ["elephant", "hippo", "jaguar", "python"] - # Your code goes here + has_monkeys = false + animals.each do |animal| + has_monkeys = true if animal.include?("monkeys") + end expect(has_monkeys).to eq(false) end - xit 'has no multiples of five' do + it 'has no multiples of five' do numbers = [3, 1, 3, 2, 4, 9, 8] - # Your code goes here + multiples_of_5 = false + numbers.each do |num| + multiples_of_5 = true if num % 5 == 0 + end expect(multiples_of_5).to eq(false) end end diff --git a/enumerables/exercises_1/spec/any_spec.rb b/enumerables/exercises_1/spec/any_spec.rb index a57f0243..5240bd88 100644 --- a/enumerables/exercises_1/spec/any_spec.rb +++ b/enumerables/exercises_1/spec/any_spec.rb @@ -7,41 +7,51 @@ expect(has_zero).to eq(true) end - xit 'does not have zeroes' do + it 'does not have zeroes' do numbers = [3, 1, 3, 2, 4, 9, 8] has_zero = numbers.any? do |number| - # Your code goes here + number.zero? end expect(has_zero).to eq(false) end - xit 'has at least one alice' do + it 'has at least one alice' do names = ["Bill", "Bob", "Burton", "Alice", "Brandon"] - # Your code goes here + has_alice = names.any? do |name| + name.include?("Alice") + end expect(has_alice).to eq(true) end - xit 'no alices' do + it 'no alices' do names = ["Chuck", "Charlene", "Cory", "Chris", "Carl"] - # Your code goes here + has_alice = names.any? do |name| + names.include?("Alice") + end expect(has_alice).to eq(false) end - xit 'has a multi word phrase' do + it 'has a multi word phrase' do phrases = ["Sure!", "OK.", "I have no idea.", "Really?Whatever."] - # Your code goes here + multi_word_phrase = phrases.any? do |phrase| + phrase.include?(" ") + end expect(multi_word_phrase).to eq(true) end - xit 'no monkeys' do + it 'has no monkeys' do animals = ["elephant", "hippo", "jaguar", "python"] - # Your code goes here + has_monkeys = animals.any? do |animal| + animal.include?("monkeys") + end expect(has_monkeys).to eq(false) end - xit 'no multiples of five' do + it 'has no multiples of five' do numbers = [3, 1, 3, 2, 4, 9, 8] - # Your code goes here + multiples_of_5 = numbers.any? do |num| + num % 5 == 0 + end expect(multiples_of_5).to eq(false) end end diff --git a/enumerables/exercises_1/spec/count_pattern_spec.rb b/enumerables/exercises_1/spec/count_pattern_spec.rb index 9bfb006b..f387b9f1 100644 --- a/enumerables/exercises_1/spec/count_pattern_spec.rb +++ b/enumerables/exercises_1/spec/count_pattern_spec.rb @@ -9,49 +9,77 @@ expect(tally).to eq(3) end - xit 'counts numbers greater than 17' do + it 'counts numbers greater than 17' do numbers = [9, 18, 12, 17, 1, 3, 99] tally = 0 numbers.each do |number| - # Your code goes here + tally += 1 if number > 17 end expect(tally).to eq(2) end - xit 'words that are uppercase' do + it 'words that are uppercase' do words = ["trousers", "SOCKS", "sweater", "Cap", "SHOE", "TIE"] tally = 0 - # Your code goes here + cap_letters = ["A".."Z"] + + words.each do |word| + tally += 1 if word.upcase == word + end + expect(tally).to eq(3) end - xit 'words ending in ing' do + it 'words ending in ing' do words = ["thought", "brake", "shin", "juice", "trash"] - # Your code goes here + tally = 0 + + words.each do |word| + tally +=1 if word.include?("ing") + end + expect(tally).to eq(0) end - xit 'even numbers' do + it 'even numbers' do numbers = [9, 2, 1, 3, 18, 39, 71, 4, 6] - # Your code goes here + tally = 0 + numbers.each do |num| + tally += 1 if num.even? + end expect(tally).to eq(4) end - xit 'multiples of 5' do + it 'multiples of 5' do numbers = [2, 5, 19, 25, 35, 67] - # Your code goes here + tally = 0 + + numbers.each do |num| + tally += 1 if num % 5 == 0 + end + expect(tally).to eq(3) end - xit 'round prices' do + it 'round prices' do prices = [1.0, 3.9, 5.99, 18.5, 20.0] - # Your code goes here + tally = 0 + + prices.each do |price| + tally += 1 if price.round == price + end + expect(tally).to eq(2) end - xit 'four letter words' do + it 'four letter words' do words = ["bake", "bark", "corn", "apple", "wart", "bird", "umbrella", "fart"] - # Your code goes here + tally = 0 + + words.each do |word| + tally += 1 if word.length == 4 + end + expect(tally).to eq(6) end diff --git a/enumerables/exercises_1/spec/count_spec.rb b/enumerables/exercises_1/spec/count_spec.rb index be22cf8c..d3f3b64b 100644 --- a/enumerables/exercises_1/spec/count_spec.rb +++ b/enumerables/exercises_1/spec/count_spec.rb @@ -8,47 +8,61 @@ expect(tally).to eq(3) end - xit 'numbers greater than 17' do + it 'numbers greater than 17' do numbers = [9, 18, 12, 17, 1, 3, 99] tally = numbers.count do |number| - # Your code goes here + number > 17 end expect(tally).to eq(2) end - xit 'words that are uppercase' do + it 'words that are uppercase' do words = ["trousers", "SOCKS", "sweater", "Cap", "SHOE", "TIE"] - # Your code goes here + + tally = words.count do |word| + word.upcase == word + end + expect(tally).to eq(3) end - xit 'words ending in ing' do + it 'words ending in ing' do words = ["thought", "brake", "shin", "juice", "trash"] - # Your code goes here + tally = words.count do |word| + word.include?("ing") + end expect(tally).to eq(0) end - xit 'even numbers' do + it 'even numbers' do numbers = [9, 2, 1, 3, 18, 39, 71, 4, 6] - # Your code goes here + tally = numbers.count do |num| + num.even? + end expect(tally).to eq(4) end - xit 'multiples of 5' do + it 'multiples of 5' do numbers = [2, 5, 19, 25, 35, 67] - # Your code goes here + tally = numbers.count do |num| + num % 5 == 0 + end expect(tally).to eq(3) end - xit 'round prices' do + it 'round prices' do prices = [1.0, 3.9, 5.99, 18.5, 20.0] - # Your code goes here + tally = prices.count do |price| + price.round == price + end expect(tally).to eq(2) end - xit 'four letter words' do + it 'four letter words' do words = ["bake", "bark", "corn", "apple", "wart", "bird", "umbrella", "fart"] - # Your code goes here + tally = words.count do |word| + word.length == 4 + end expect(tally).to eq(6) end end diff --git a/enumerables/exercises_1/spec/find_pattern_spec.rb b/enumerables/exercises_1/spec/find_pattern_spec.rb index 513ea7c7..72661f42 100644 --- a/enumerables/exercises_1/spec/find_pattern_spec.rb +++ b/enumerables/exercises_1/spec/find_pattern_spec.rb @@ -12,61 +12,121 @@ expect(found).to eq("unicorn") end - xit 'no waldo' do + it 'no waldo' do words = ["scarf", "sandcastle", "flag", "pretzel", "crow", "key"] found = nil words.each do |word| - # Your code goes here + if word == "waldo" + found = word + end end expect(found).to eq(nil) end - xit 'found waldo' do + it 'found waldo' do words = ["noise", "dog", "fair", "house", "waldo", "bucket", "fish"] found = nil - # Your code goes here + words.each do |word| + if word == "waldo" + found = word + end + end expect(found).to eq("waldo") end - xit 'no three letter words' do + it 'no three letter words' do words = ["piglet", "porridge", "bear", "blueberry"] - # Your code goes here + found = nil + + words.each do |word| + if word.length == 3 + found = word + end + end + expect(found).to eq(nil) end - xit 'finds 13' do + it 'finds 13' do numbers = [2, 13, 19, 8, 3, 27] - # Your code goes here + found = nil + + numbers.each do |num| + if num == 13 + found = num + end + end + expect(found).to eq(13) end - xit 'first even number' do + it 'first even number' do numbers = [3, 7, 13, 11, 10, 2, 17] - # Your code goes here + found = nil + + numbers.each do |num| + if num.even? == true + found = num + break + end + end + expect(found).to eq(10) end - xit 'first multiple of 3' do + it 'first multiple of 3' do numbers = [2, 8, 9, 27, 24, 5] - # Your code goes here + found = nil + + numbers.each do |num| + if num % 3 == 0 + found = num + break + end + end + expect(found).to eq(9) end - xit 'first word starting with q' do + it 'first word starting with q' do words = ["weirdo", "quill", "fast", "quaint", "quitter", "koala"] - # Your code goes here + found = nil + + words.each do |word| + if word.start_with?("q") + found = word + break + end + end + expect(found).to eq("quill") end - xit 'first word ending with er' do + it 'first word ending with er' do words = ["biggest", "pour", "blight", "finger", "pie", "border"] - # Your code goes here + found = nil + + words.each do |word| + if word.end_with?("er") + found = word + break + end + end + expect(found).to eq("finger") end - xit 'first number greater than 20' do + it 'first number greater than 20' do numbers = [1, 8, 19, 21, 29, 31, 34] - # Your code goes here + found = nil + + numbers.each do |num| + if num > 20 + found = num + break + end + end + expect(found).to eq(21) end end diff --git a/enumerables/exercises_1/spec/find_spec.rb b/enumerables/exercises_1/spec/find_spec.rb index 8745de8c..b97841b5 100644 --- a/enumerables/exercises_1/spec/find_spec.rb +++ b/enumerables/exercises_1/spec/find_spec.rb @@ -2,65 +2,101 @@ it 'first seven letter word' do words = ["capricious", "berry", "unicorn", "bag", "apple", "festering", "pretzel", "pencil"] + found = words.find do |word| word.length == 7 end + expect(found).to eq("unicorn") end - xit 'no waldo' do + it 'no waldo' do words = ["scarf", "sandcastle", "flag", "pretzel", "crow", "key"] + found = words.find do |word| - # Your code goes here + word == "waldo" end + expect(found).to eq(nil) end - xit 'found waldo' do + it 'found waldo' do words = ["noise", "dog", "fair", "house", "waldo", "bucket", "fish"] - # Your code goes here + + found = words.find do |word| + word == "waldo" + end + expect(found).to eq("waldo") end - xit 'no three letter words' do + it 'no three letter words' do words = ["piglet", "porridge", "bear", "blueberry"] - # Your code goes here + + found = words.find do |word| + word.length == 3 + end + expect(found).to eq(nil) end - xit 'find 13' do + it 'find 13' do numbers = [2, 13, 19, 8, 3, 27] - # Your code goes here + + found = numbers.find do |num| + num == 13 + end + expect(found).to eq(13) end - xit 'find first even number' do + it 'find first even number' do numbers = [3, 7, 13, 11, 10, 2, 17] - # Your code goes here + + found = numbers.find do |num| + num.even? + end + expect(found).to eq(10) end - xit 'first multiple of 3' do + it 'first multiple of 3' do numbers = [2, 8, 9, 27, 24, 5] - # Your code goes here + + found = numbers.find do |num| + num % 3 == 0 + end + expect(found).to eq(9) end - xit 'first word starting with q' do + it 'first word starting with q' do words = ["weirdo", "quill", "fast", "quaint", "quitter", "koala"] - # Your code goes here + + found = words.find do |word| + word.start_with?("q") + end + expect(found).to eq("quill") end - xit 'first word ending with er' do + it 'first word ending with er' do words = ["biggest", "pour", "blight", "finger", "pie", "border"] - # Your code goes here + + found = words.find do |word| + word.end_with?("er") + end + expect(found).to eq("finger") end - xit 'first number greater than 20' do + it 'first number greater than 20' do numbers = [1, 8, 19, 21, 29, 31, 34] - # Your code goes here + + found = numbers.find do |num| + num > 20 + end + expect(found).to eq(21) end end diff --git a/enumerables/exercises_1/spec/group_by_pattern_spec.rb b/enumerables/exercises_1/spec/group_by_pattern_spec.rb index 07d39a89..4c60dad7 100644 --- a/enumerables/exercises_1/spec/group_by_pattern_spec.rb +++ b/enumerables/exercises_1/spec/group_by_pattern_spec.rb @@ -3,28 +3,41 @@ it 'group words by length' do words = ["sue", "alice", "steve", "sally", "adam", "fort", "tops", "dog", "cat"] grouped = Hash.new {|hash, key| hash[key] = []} + words.each do |word| grouped[word.length] << word end + expected = {3=>["sue", "dog", "cat"], 4=>["adam", "fort", "tops"], 5=>["alice", "steve", "sally"]} expect(grouped).to eq(expected) end - xit 'groups by odds and evens' do + it 'groups by odds and evens' do numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] - odd_and_even = Hash.new {|hash, key| hash[key] = []} + odd_and_even = Hash.new {|key, value| key[value] = []} + # everytime you assign a new key the value will be an array + numbers.each do |number| - # Your code goes here + if number.odd? + odd_and_even[1] << number + else + odd_and_even[0] << number + end end + expected = {1=>[1, 1, 3, 5, 13, 21, 55], 0=>[2, 8, 34]} expect(odd_and_even).to eq(expected) end - xit 'groups by first letter' do - words = ["ant", "axis", "albatross", "bolt", "badge", "butter", "car", "cdr", "column"] + it 'groups by first letter' do + words = ["ant", "axis", "albatross", "bolt", "badge", "butter", "car", "cdr", "column", "1zebra"] words_by_first_letter = Hash.new {|hash, key| hash[key] = []} - # Your code goes here - expected = {"a"=>["ant", "axis", "albatross"], "b"=>["bolt", "badge", "butter"], "c"=>["car", "cdr", "column"]} + + words.each do |word| + words_by_first_letter[word.chr] << word + end + + expected = {"a"=>["ant", "axis", "albatross"], "b"=>["bolt", "badge", "butter"], "c"=>["car", "cdr", "column"], "1"=>["1zebra"]} expect(words_by_first_letter).to eq(expected) end diff --git a/enumerables/exercises_1/spec/group_by_spec.rb b/enumerables/exercises_1/spec/group_by_spec.rb index 841917df..8de837b0 100644 --- a/enumerables/exercises_1/spec/group_by_spec.rb +++ b/enumerables/exercises_1/spec/group_by_spec.rb @@ -1,25 +1,37 @@ RSpec.describe 'group by' do it 'groups words by length' do words = ["sue", "alice", "steve", "sally", "adam", "fort", "tops", "dog", "cat"] + grouped = words.group_by do |word| word.length end + expected = {3=>["sue", "dog", "cat"], 4=>["adam", "fort", "tops"], 5=>["alice", "steve", "sally"]} expect(grouped).to eq(expected) end - xit 'group by odd and even' do + it 'group by odd and even' do numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] - odd_and_even = numbers.group_by do |number| - # Your code goes here - end + + odd_and_even = numbers.group_by do |num| + if num.odd? + 1 + else + 0 + end + end + expected = {1=>[1, 1, 3, 5, 13, 21, 55], 0=>[2, 8, 34]} expect(odd_and_even).to eq(expected) end - xit 'group by first letter' do + it 'group by first letter' do words = ["ant", "axis", "albatross", "bolt", "badge", "butter", "car", "cdr", "column"] - # Your code goes here + + words_by_first_letter = words.group_by do |word| + word.chr + end + expected = {"a"=>["ant", "axis", "albatross"], "b"=>["bolt", "badge", "butter"], "c"=>["car", "cdr", "column"]} expect(words_by_first_letter).to eq(expected) end diff --git a/enumerables/exercises_1/spec/map_pattern_spec.rb b/enumerables/exercises_1/spec/map_pattern_spec.rb index e15e4059..e4f1e7bd 100644 --- a/enumerables/exercises_1/spec/map_pattern_spec.rb +++ b/enumerables/exercises_1/spec/map_pattern_spec.rb @@ -3,55 +3,88 @@ it 'capitalizes' do names = ["alice", "bob", "charlie"] capitalized_names = [] + names.each do |name| capitalized_names << name.capitalize end + expect(capitalized_names).to eq(["Alice", "Bob", "Charlie"]) end - xit 'doubles' do + it 'doubles' do numbers = [1, 2, 3, 4, 5] doubles = [] + numbers.each do |number| - # Your code goes here + doubles << number * 2 end + expect(doubles).to eq([2, 4, 6, 8, 10]) end - xit 'squares' do + it 'squares' do numbers = [1, 2, 3, 4, 5] squares = [] - # Your code goes here + + numbers.each do |num| + squares << num * num + end + expect(squares).to eq([1, 4, 9, 16, 25]) end - xit 'lengths' do + it 'lengths' do names = ["alice", "bob", "charlie", "david", "eve"] - # Your code goes here + lengths = [] + + names.each do |name| + lengths << name.length + end + expect(lengths).to eq([5, 3, 7, 5, 3]) end - xit 'normalize zip codes' do + it 'normalize zip codes' do numbers = [234, 10, 9119, 38881] - # Your code goes here + zip_code = [] + + numbers.each do |num| + zip_code << num.to_s.rjust(5, "0") + end + expect(zip_code).to eq(["00234", "00010", "09119", "38881"]) end - xit 'backwards' do + it 'backwards' do names = ["alice", "bob", "charlie", "david", "eve"] - # Your code goes here + backwards = [] + + names.each do |name| + backwards << name.reverse + end + expect(backwards).to eq(["ecila", "bob", "eilrahc", "divad", "eve"]) end - xit 'words with no vowels' do + it 'words with no vowels' do words = ["green", "sheep", "travel", "least", "boat"] - # Your code goes here + without_vowels = [] + + words.each do |word| + without_vowels.push(word.delete("aeiou")) + end + expect(without_vowels).to eq(["grn", "shp", "trvl", "lst", "bt"]) end - xit 'trims last letter' do + it 'trims last letter' do animals = ["dog", "cat", "mouse", "frog", "platypus"] - # Your code goes here + trimmed = [] + + animals.each do |animal| + trimmed << animal.chop + end + expect(trimmed).to eq(["do", "ca", "mous", "fro", "platypu"]) end end diff --git a/enumerables/exercises_1/spec/map_spec.rb b/enumerables/exercises_1/spec/map_spec.rb index de324004..35ebed5d 100644 --- a/enumerables/exercises_1/spec/map_spec.rb +++ b/enumerables/exercises_1/spec/map_spec.rb @@ -2,53 +2,68 @@ it 'capitalizes' do names = ["alice", "bob", "charlie"] + capitalized_names = names.map do |name| name.capitalize end + # <.map> doesn't need an accumulator cuz you can define a variable that will become the modified array + # <.each> needs one because it returns the original array! expect(capitalized_names).to eq(["Alice", "Bob", "Charlie"]) end - xit 'doubles' do + it 'doubles' do numbers = [1, 2, 3, 4, 5] doubles = numbers.map do |number| - # Your code goes here + number * 2 end expect(doubles).to eq([2, 4, 6, 8, 10]) end - xit 'squares' do + it 'squares' do numbers = [1, 2, 3, 4, 5] - # Your code goes here + squares = numbers.map do |num| + num * num + end expect(squares).to eq([1, 4, 9, 16, 25]) end - xit 'lengths' do + it 'lengths' do names = ["alice", "bob", "charlie", "david", "eve"] - # Your code goes here + lengths = names.map do |name| + name.length + end expect(lengths).to eq([5, 3, 7, 5, 3]) end - xit 'normalize zip codes' do + it 'normalize zip codes' do numbers = [234, 10, 9119, 38881] - # Your code goes here + zip_codes = numbers.map do |num| + num.to_s.rjust(5, "0") + end expect(zip_codes).to eq(["00234", "00010", "09119", "38881"]) end - xit 'backwards' do + it 'backwards' do names = ["alice", "bob", "charlie", "david", "eve"] - # Your code goes here + backwards = names.map do |name| + name.reverse + end expect(backwards).to eq(["ecila", "bob", "eilrahc", "divad", "eve"]) end - xit 'words with no vowels' do + it 'words with no vowels' do words = ["green", "sheep", "travel", "least", "boat"] - # Your code goes here + without_vowels = words.map do |word| + word.delete("aeiou") + end expect(without_vowels).to eq(["grn", "shp", "trvl", "lst", "bt"]) end - xit 'trims last letter' do + it 'trims last letter' do animals = ["dog", "cat", "mouse", "frog", "platypus"] - # Your code goes here + trimmed = animals.map do |animal| + animal.chop + end expect(trimmed).to eq(["do", "ca", "mous", "fro", "platypu"]) end end diff --git a/enumerables/exercises_1/spec/reduce_pattern_spec.rb b/enumerables/exercises_1/spec/reduce_pattern_spec.rb index bb24bd1f..e1eaea0a 100644 --- a/enumerables/exercises_1/spec/reduce_pattern_spec.rb +++ b/enumerables/exercises_1/spec/reduce_pattern_spec.rb @@ -2,53 +2,69 @@ it 'sums a list of numbers' do numbers = [32, 1, 21, 5, 81, 333] sum = 0 + numbers.each do |number| sum = sum + number end + expect(sum).to eq(473) end - xit 'subtracts a list of numbers' do + it 'subtracts a list of numbers' do numbers = [28, 12, 38, 1, 91] difference = 0 numbers.each do |number| - # Your code goes here + difference = difference - number end expect(difference).to eq(-170) end - xit 'multiplies a list of numbers' do + it 'multiplies a list of numbers' do numbers = [2, 3, 5, 7] product = 1 - # Your code goes here + numbers.each do |number| + product = product * number + end expect(product).to eq(210) end - xit 'capitalizes key words in phrase' do + it 'capitalizes key words in phrase' do keywords = ["fish", "blue"] phrase = 'one fish two fish red fish blue fish' - # Your code goes here + + keywords.each do |keyword| + phrase = phrase.gsub(keyword, keyword.upcase) + end + expect(phrase).to eq('one FISH two FISH red FISH BLUE FISH') end - xit 'divide 560 by a bunch of numbers' do + it 'divide 560 by a bunch of numbers' do numbers = [2, 2, 2, 5, 7] quotient = 560 - # Your code goes here + + numbers.each do |num| + quotient = quotient / num + end + expect(quotient).to eq(2) end - xit 'subtracts smallest numbers from 100' do + it 'subtracts smallest numbers from 100' do elements = [[8, 5, 3], [1, 9, 11], [4, 7, 2], [19, 34, 6]] difference = 100 - # Your code goes here + elements.each do |element| + difference = difference - element.min + end expect(difference).to eq(88) end - xit 'adds all second values together' do + it 'adds all second values together' do elements = [["a", 1], ["b", 9], ["c", 21]] sum = 0 - # Your code goes here + elements.each do |element| + sum = sum + element[1] + end expect(sum).to eq(31) end diff --git a/enumerables/exercises_1/spec/reduce_spec.rb b/enumerables/exercises_1/spec/reduce_spec.rb index 9ed0ada9..44f847a4 100644 --- a/enumerables/exercises_1/spec/reduce_spec.rb +++ b/enumerables/exercises_1/spec/reduce_spec.rb @@ -2,52 +2,68 @@ it 'sums a list of numbers' do numbers = [32, 1, 21, 5, 81, 333] + result = numbers.reduce(0) do |sum, number| sum + number end + expect(result).to eq(473) end - xit 'subtracts a list of numbers' do + it 'subtracts a list of numbers' do numbers = [28, 12, 38, 1, 91] result = numbers.reduce(0) do |difference, number| - # Your code goes here + difference - number end expect(result).to eq(-170) end - xit 'multiplies a list of numbers' do + it 'multiplies a list of numbers' do numbers = [2, 3, 5, 7] # initial value is 1 - # Your code goes here + + result = numbers.reduce(1) do |one, number| + one * number + end + expect(result).to eq(210) end - xit 'capitalize key words in phrase' do + + it 'capitalize key words in phrase' do keywords = ["fish", "blue"] # initial value is 'one fish two fish red fish blue fish' - # Your code goes here + result = keywords.reduce("one fish two fish red fish blue fish") do |phrase, keyword| + phrase.gsub(keyword, keyword.upcase) + end expect(result).to eq('one FISH two FISH red FISH BLUE FISH') end - xit 'divides 560 by a bunch of numbers' do + + it 'divides 560 by a bunch of numbers' do numbers = [2, 2, 2, 5, 7] # initial value is 560 - # Your code goes here + result = numbers.reduce(560) do |quotient, number| + quotient / number + end expect(result).to eq(2) end - xit 'subtract smallest values from 100' do + it 'subtract smallest values from 100' do elements = [[8, 5, 3], [1, 9, 11], [4, 7, 2], [19, 34, 6]] # initial value is 100 - # Your code goes here + result = elements.reduce(100) do |start_num, smallest_num| + start_num - smallest_num.min + end expect(result).to eq(88) end - xit 'adds all second values together' do + it 'adds all second values together' do elements = [["a", 1], ["b", 9], ["c", 21]] # initial value is 0 - # Your code goes here + result = elements.reduce(0) do |zero, second_element| + zero + second_element[1] + end expect(result).to eq(31) end end diff --git a/enumerables/exercises_1/spec/reject_pattern_spec.rb b/enumerables/exercises_1/spec/reject_pattern_spec.rb index dd5c2717..290c276f 100644 --- a/enumerables/exercises_1/spec/reject_pattern_spec.rb +++ b/enumerables/exercises_1/spec/reject_pattern_spec.rb @@ -3,92 +3,130 @@ it 'removes zeroes' do numbers = [2, 93, 7, 0, 0, 1, 0, 31, 0, 368] filtered = [] + numbers.each do |number| filtered << number unless number.zero? end + expect(filtered).to eq([2, 93, 7, 1, 31, 368]) end - xit 'removes vowels' do + it 'removes vowels' do letters = ["a", "l", "l", " ", "y", "o", "u", "r", " ", "b", "a", "s", "e", " ", "a", "r", "e", " ", "b", "e", "l", "o", "n", "g", " ", "t", "o", " ", "u", "s"] remaining = [] + letters.each do |letter| - # Your code goes here + remaining << letter unless ["a", "e", "o", "u", "i", "y"].include?(letter) end + expect(remaining).to eq(["l", "l", " ", "r", " ", "b", "s", " ", "r", " ", "b", "l", "n", "g", " ", "t", " ", "s"]) end - xit 'removes numbers divisible by 3' do + it 'removes numbers divisible by 3' do numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] remaining = [] - # Your code goes here + numbers.each do |num| + remaining << num unless num % 3 == 0 + end expect(remaining).to eq([1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20]) end - xit 'removes words longer than 3 letters' do - skip + it 'removes words longer than 3 letters' do words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] - # Your code goes here - expected(selected).to eq(["bad", "cat", "dog", "red"]) + selected = [] + words.each do |word| + selected << word unless word.length > 3 + end + expect(selected).to eq(["bad", "cat", "dog", "red"]) end - xit 'removes words ending in e' do + it 'removes words ending in e' do words = ["are", "you", "strike", "thinking", "belt", "piece", "warble", "sing", "pipe"] - # Your code goes here - expected(selected).to eq(["you", "thinking", "belt", "sing"]) + selected = [] + words.each do |word| + selected << word unless word.end_with?("e") + end + expect(selected).to eq(["you", "thinking", "belt", "sing"]) end - xit 'removes words ending in ing' do + it 'removes words ending in ing' do words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"] - # Your code goes here + selected = [] + words.each do |word| + selected << word unless word.end_with?("ing") + end expect(selected).to eq(["finger", "drought", "bingo", "purposeful"]) end - xit 'removes words containing e' do + it 'removes words containing e' do words = ["four", "red", "five", "blue", "pizza", "purple"] - # Your code goes here + selected = [] + words.each do |word| + selected << word unless word.include?("e") + end expect(selected).to eq(["four", "pizza"]) end - xit 'removes dinosaurs' do + it 'removes dinosaurs' do animals = ["tyrannosaurus", "narwhal", "eel", "achillesaurus", "qingxiusaurus"] - # Your code goes here - expect(notasaurus).to eq(["narwhal", "eel"]) + not_a_saurus = [] + animals.each do |animal| + not_a_saurus << animal unless animal.include?("saurus") + end + expect(not_a_saurus).to eq(["narwhal", "eel"]) end - xit 'removes numbers' do + it 'removes numbers' do elements = ["cat", "dog", 23, 81.1, 56, "aimless", 43] - # Your code goes here - expected(not_numbers).to eq(["cat", "dog", "aimless"]) + not_numbers = [] + elements.each do |element| + not_numbers << element unless element.is_a?(Float) || element.is_a?(Integer) + end + expect(not_numbers).to eq(["cat", "dog", "aimless"]) end - xit 'removes floats' do + it 'removes floats' do elements = ["cat", "dog", 32.333, 23, 56, "aimless", 43.2] - # Your code goes here + not_numbers = [] + elements.each do |element| + not_numbers << element unless element.is_a?(Float) + end expect(not_numbers).to eq(["cat", "dog", 23, 56, "aimless"]) end - xit 'removes animals starting with vowels' do + it 'removes animals starting with vowels' do animals = ["aardvark", "bonobo", "cat", "dog", "elephant"] - # Your code goes here + remaining = [] + animals.each do |animal| + remaining << animal unless "aeoui".include?(animal.chr) + end expect(remaining).to eq(["bonobo", "cat", "dog"]) end - xit 'removes upcased words' do + it 'removes upcased words' do words = ["CAT", "dog", "AIMLESS", "Trevor", "butter"] - # Your code goes here + remaining = [] + words.each do |word| + remaining << word unless word.upcase == word + end expect(remaining).to eq(["dog", "Trevor", "butter"]) end - xit 'removes arrays' do + it 'removes arrays' do elements = ["CAT", ["dog"], 23, [56, 3, 8], "AIMLESS", 43, "butter"] - # Your code goes here - expected(remaining).to eq(["CAT", 23, "AIMLESS", 43, "butter"]) + remaining = [] + elements.each do |element| + remaining << element unless element.is_a?(Array) + end + expect(remaining).to eq(["CAT", 23, "AIMLESS", 43, "butter"]) end - xit 'removes hashes' do + it 'removes hashes' do elements = ["cat", {:dog=>"fido"}, 23, {:stuff=>"things"}, "aimless", 43] - # Your code goes here + remaining = [] + elements.each do |element| + remaining << element unless element.is_a?(Hash) + end expect(remaining).to eq(["cat", 23, "aimless", 43]) end end diff --git a/enumerables/exercises_1/spec/reject_spec.rb b/enumerables/exercises_1/spec/reject_spec.rb index 59101f4a..015cb974 100644 --- a/enumerables/exercises_1/spec/reject_spec.rb +++ b/enumerables/exercises_1/spec/reject_spec.rb @@ -8,83 +8,107 @@ expect(filtered).to eq([2, 93, 7, 1, 31, 368]) end - xit 'removes vowels' do + it 'removes vowels' do letters = ["a", "l", "l", " ", "y", "o", "u", "r", " ", "b", "a", "s", "e", " ", "a", "r", "e", " ", "b", "e", "l", "o", "n", "g", " ", "t", "o", " ", "u", "s"] remaining = letters.reject do |letter| - # Your code goes here + "aeouiy".include?(letter) end expect(remaining).to eq(["l", "l", " ", "r", " ", "b", "s", " ", "r", " ", "b", "l", "n", "g", " ", "t", " ", "s"]) end - xit 'remove numbers divisible by 3' do + it 'remove numbers divisible by 3' do numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - # Your code goes here + remaining = numbers.reject do |num| + num % 3 == 0 + end expect(remaining).to eq([1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20]) end - xit 'remove words longer tghan three letters' do + it 'remove words longer than three letters' do words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] - # Your code goes here + selected = words.reject do |word| + word.length > 3 + end expect(selected).to eq(["bad", "cat", "dog", "red"]) end - xit 'remove words ending in e' do + it 'remove words ending in e' do words = ["are", "you", "strike", "thinking", "belt", "piece", "warble", "sing", "pipe"] - # Your code goes here + selected = words.reject do |word| + word.end_with?("e") + end expect(selected).to eq(["you", "thinking", "belt", "sing"]) end - xit 'remove words ending in ing' do + it 'remove words ending in ing' do words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"] - # Your code goes here + selected = words.reject do |word| + word.end_with?("ing") + end expect(selected).to eq(["finger", "drought", "bingo", "purposeful"]) end - xit 'remove words containing e' do + it 'remove words containing e' do words = ["four", "red", "five", "blue", "pizza", "purple"] - # Your code goes here + selected = words.reject do |word| + word.include?("e") + end expect(selected).to eq(["four", "pizza"]) end - xit 'remove dinosaurs' do + it 'remove dinosaurs' do animals = ["tyrannosaurus", "narwhal", "eel", "achillesaurus", "qingxiusaurus"] - # Your code goes here - expect(notasaurus).to eq(["narwhal", "eel"]) + not_a_saurus = animals.reject do |animal| + animal.include?("saurus") + end + expect(not_a_saurus).to eq(["narwhal", "eel"]) end - xit 'remove numbers' do + it 'remove numbers' do elements = ["cat", "dog", 23, 81.1, 56, "aimless", 43] - # Your code goes here + not_numbers = elements.reject do |element| + element.is_a?(Float) || element.is_a?(Integer) + end expect(not_numbers).to eq(["cat", "dog", "aimless"]) end - xit 'remove floats' do + it 'remove floats' do elements = ["cat", "dog", 32.333, 23, 56, "aimless", 43.2] - # Your code goes here + not_numbers = elements.reject do |element| + element.is_a?(Float) + end expect(not_numbers).to eq(["cat", "dog", 23, 56, "aimless"]) end - xit 'remove animals starting with a vowel' do + it 'remove animals starting with a vowel' do animals = ["aardvark", "bonobo", "cat", "dog", "elephant"] - # Your code goes here + remaining = animals.reject do |animal| + "aeoui".include?(animal.chr) + end expect(remaining).to eq(["bonobo", "cat", "dog"]) end - xit 'remove upcased words' do + it 'remove upcased words' do words = ["CAT", "dog", "AIMLESS", "Trevor", "butter"] - # Your code goes here + remaining = words.reject do |word| + word.upcase == word + end expect(remaining).to eq(["dog", "Trevor", "butter"]) end - xit 'remove arrays' do + it 'remove arrays' do elements = ["CAT", ["dog"], 23, [56, 3, 8], "AIMLESS", 43, "butter"] - # Your code goes here + remaining = elements.reject do |element| + element.is_a?(Array) + end expect(remaining).to eq(["CAT", 23, "AIMLESS", 43, "butter"]) end - xit 'remove hashes' do + it 'remove hashes' do elements = ["cat", {:dog=>"fido"}, 23, {:stuff=>"things"}, "aimless", 43] - # Your code goes here + remaining = elements.reject do |element| + element.is_a?(Hash) + end expect(remaining).to eq(["cat", 23, "aimless", 43]) end end diff --git a/enumerables/exercises_1/spec/select_pattern_spec.rb b/enumerables/exercises_1/spec/select_pattern_spec.rb index ace429eb..e2ae1966 100644 --- a/enumerables/exercises_1/spec/select_pattern_spec.rb +++ b/enumerables/exercises_1/spec/select_pattern_spec.rb @@ -3,73 +3,121 @@ it 'picks even numbers' do numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evens = [] + numbers.each do |number| evens << number if number.even? end + expect(evens).to eq([2, 4, 6, 8, 10]) end - xit 'picks odd numbers' do + it 'picks odd numbers' do numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] odds = [] + numbers.each do |number| - # Your code goes here + odds << number if number.odd? end + expect(odds).to eq([1, 3, 5, 7, 9]) end - xit 'words with three letters' do + it 'words with three letters' do words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] selected = [] - # Your code goes here + + words.each do |word| + selected << word if word.length == 3 + end + expect(selected).to eq(["bad", "cat", "dog", "red"]) end - xit 'words with more than three letters' do + it 'words with more than three letters' do words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] - # Your code goes here + selected = [] + + words.each do |word| + selected << word if word.length > 3 + end + expect(selected).to eq(["pill", "finger", "blue", "table"]) end - xit 'words ending in e' do + it 'words ending in e' do words = ["are", "you", "strike", "thinking", "belt", "piece", "warble", "sing", "pipe"] - # Your code goes here + selected = [] + + words.each do |word| + selected << word if word.end_with?("e") + end + expect(selected).to eq(["are", "strike", "piece", "warble", "pipe"]) end - xit 'words ending in ing' do + it 'words ending in ing' do words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"] - # Your code goes here + selected = [] + + words.each do |word| + selected << word if word.end_with?("ing") + end + expect(selected).to eq(["bring", "singing"]) end - xit 'words containing e' do + it 'words containing e' do words = ["four", "red", "five", "blue", "pizza", "purple"] - # Your code goes here + selected = [] + + words.each do |word| + selected << word if word.include?("e") + end + expect(selected).to eq(["red", "five", "blue", "purple"]) end - xit 'dinosaurs' do + it 'dinosaurs' do animals = ["tyrannosaurus", "narwhal", "eel", "achillesaurus", "qingxiusaurus"] - # Your code goes here + dinosaurs = [] + + animals.each do |animal| + dinosaurs << animal if animal.end_with?("saurus") + end + expect(dinosaurs).to eq(["tyrannosaurus", "achillesaurus", "qingxiusaurus"]) end - xit 'floats' do + it 'floats' do numbers = [3, 1.4, 3.5, 2, 4.9, 9.1, 8.0] - # Your code goes here + floats = [] + + numbers.each do |num| + floats << num if num.is_a?(Float) + end + expect(floats).to eq([1.4, 3.5, 4.9, 9.1, 8.0]) end - xit 'arrays' do + it 'arrays' do elements = ["CAT", ["dog"], 23, [56, 3, 8], "AIMLESS", 43, "butter"] - # Your code goes here + arrays = [] + + elements.each do |element| + arrays << element if element.is_a?(Array) + end + expect(arrays).to eq([["dog"], [56, 3, 8]]) end - xit 'hashes' do + it 'hashes' do elements = ["cat", {:dog=>"fido"}, 23, {:stuff=>"things"}, "aimless", 43] - # Your code goes here + hashes = [] + + elements.each do |element| + hashes << element if element.is_a?(Hash) + end + expect(hashes).to eq([{:dog=>"fido"}, {:stuff=>"things"}]) end end diff --git a/enumerables/exercises_1/spec/select_spec.rb b/enumerables/exercises_1/spec/select_spec.rb index 34e686eb..797d5e8d 100644 --- a/enumerables/exercises_1/spec/select_spec.rb +++ b/enumerables/exercises_1/spec/select_spec.rb @@ -2,71 +2,95 @@ it 'even numbers' do numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + evens = numbers.select do |number| number.even? end + expect(evens).to eq([2, 4, 6, 8, 10]) end - xit 'odd numbers' do + it 'odd numbers' do numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + odds = numbers.select do |number| - # Your code goes here + number.odd? end + # <.select> will only move the number IF it meets the criteria in the codeblock + # this enumb. adds a level of functionality behind the scenes! + # think of it like: ".select this item IF...." expect(odds).to eq([1, 3, 5, 7, 9]) end - xit 'words with three letters' do + it 'words with three letters' do words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] - # Your code goes here + selected = words.select do |word| + word.length == 3 + end expect(selected).to eq(["bad", "cat", "dog", "red"]) end - xit 'words with more than three letters' do + it 'words with more than three letters' do words = ["pill", "bad", "finger", "cat", "blue", "dog", "table", "red"] - # Your code goes here + selected = words.select do |word| + word.length > 3 + end expect(selected).to eq(["pill", "finger", "blue", "table"]) end - xit 'wordss ending in e' do + it 'wordss ending in e' do words = ["are", "you", "strike", "thinking", "belt", "piece", "warble", "sing", "pipe"] - # Your code goes here + selected = words.select do |word| + word.end_with?("e") + end expect(selected).to eq(["are", "strike", "piece", "warble", "pipe"]) end - xit 'words ending in ing' do + it 'words ending in ing' do words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"] - # Your code goes here + selected = words.select do |word| + word.end_with?("ing") + end expect(selected).to eq(["bring", "singing"]) end - xit 'words containing e' do + it 'words containing e' do words = ["four", "red", "five", "blue", "pizza", "purple"] - # Your code goes here + selected = words.select do |word| + word.include?("e") + end expect(selected).to eq(["red", "five", "blue", "purple"]) end - xit 'dinosaurs' do + it 'dinosaurs' do animals = ["tyrannosaurus", "narwhal", "eel", "achillesaurus", "qingxiusaurus"] - # Your code goes here + dinosaurs = animals.select do |animal| + animal.end_with?("saurus") + end expect(dinosaurs).to eq(["tyrannosaurus", "achillesaurus", "qingxiusaurus"]) end - xit 'floats' do + it 'floats' do numbers = [3, 1.4, 3.5, 2, 4.9, 9.1, 8.0] - # Your code goes here + floats = numbers.select do |num| + num.is_a?(Float) + end expect(floats).to eq([1.4, 3.5, 4.9, 9.1, 8.0]) end - xit 'arrays' do + it 'arrays' do elements = ["CAT", ["dog"], 23, [56, 3, 8], "AIMLESS", 43, "butter"] - # Your code goes here + arrays = elements.select do |element| + element.is_a?(Array) + end expect(arrays).to eq([["dog"], [56, 3, 8]]) end - xit 'hashes' do + it 'hashes' do elements = ["cat", {:dog=>"fido"}, 23, {:stuff=>"things"}, "aimless", 43] - # Your code goes here + hashes = elements.select do |element| + element.is_a?(Hash) + end expect(hashes).to eq([{:dog=>"fido"}, {:stuff=>"things"}]) end end diff --git a/enumerables/exercises_1/spec/sort_by_pattern_spec.rb b/enumerables/exercises_1/spec/sort_by_pattern_spec.rb index 0970204a..c625bb5a 100644 --- a/enumerables/exercises_1/spec/sort_by_pattern_spec.rb +++ b/enumerables/exercises_1/spec/sort_by_pattern_spec.rb @@ -3,58 +3,107 @@ it 'sorts alphabetically' do words = ["broccoli", "Carrots", "FISH", "Bacon", "candy"] transformed = [] + words.each do |word| transformed << [word.downcase, word] end + transformed = transformed.sort sorted = [] + transformed.each do |sort_key, word| sorted << word end + expect(sorted).to eq(["Bacon", "broccoli", "candy", "Carrots", "FISH"]) end - xit 'alphabetically by last letter' do + it 'alphabetically by last letter' do things = ["pill", "box", "glass", "water", "sponge"] transformed = [] + things.each do |thing| - # Your code goes here + transformed << [thing.reverse, thing] end + transformed = transformed.sort sorted = [] + transformed.each do |sort_key, thing| sorted << thing end expect(sorted).to eq(["sponge", "pill", "water", "glass", "box"]) end - xit 'sort by distance' do + it 'sort by distance' do distances = ["1cm", "9cm", "30cm", "4cm", "2cm"] transformed = [] - # Your code goes here + + distances.each do |distance| + transformed << [distance.to_i, distance] + end + transformed = transformed.sort sorted = [] + transformed.each do |sort_key, distance| sorted << distance end + expect(sorted).to eq(["1cm", "2cm", "4cm", "9cm", "30cm"]) end - xit 'by length' do + it 'by length' do words = ["heteromorph", "ancyloceratina", "bioengineering", "mathematical", "bug"] - # Your code goes here + transformed = [] + + words.each do |word| + transformed << [word.length, word] + end + + transformed = transformed.sort + sorted = [] + + transformed.each do |sort_key, word| + sorted << word + end + expect(sorted).to eq(["bug", "heteromorph", "mathematical", "ancyloceratina", "bioengineering"]) end - xit 'by proximity to ten' do + it 'by proximity to ten' do prices = [3.02, 9.91, 17.9, 10.01, 11.0] - # Your code goes here + transformed = [] + + prices.each do |price| + transformed << [(price - 10).abs, price] + end + + transformed = transformed.sort + sorted = [] + + transformed.each do |sort_key, price| + sorted << price + end + expect(sorted).to eq([10.01, 9.91, 11.0, 3.02, 17.9]) end - xit 'by number of cents' do + it 'by number of cents' do prices = [3.02, 9.91, 7.9, 10.01, 11.0] - # Your code goes here + transformed = [] + + prices.each do |price| + transformed << [(price.to_i - price).abs, price] + end + + transformed = transformed.sort + sorted = [] + + transformed.each do |sort_key, price| + sorted << price + end + expect(sorted).to eq([11.0, 10.01, 3.02, 7.9, 9.91]) end end diff --git a/enumerables/exercises_1/spec/sort_by_spec.rb b/enumerables/exercises_1/spec/sort_by_spec.rb index fa317429..472b827b 100644 --- a/enumerables/exercises_1/spec/sort_by_spec.rb +++ b/enumerables/exercises_1/spec/sort_by_spec.rb @@ -8,35 +8,45 @@ expect(sorted).to eq(["Bacon", "broccoli", "candy", "Carrots", "FISH"]) end - xit 'alphabetically by last letter' do + it 'alphabetically by last letter' do things = ["pill", "box", "glass", "water", "sponge"] sorted = things.sort_by do |thing| - # Your code goes here + thing[-1] end expect(sorted).to eq(["sponge", "pill", "water", "glass", "box"]) end - xit 'distance' do + it 'distance' do distances = ["1cm", "9cm", "30cm", "4cm", "2cm"] - # Your code goes here + sorted = distances.sort_by do |distance| + distance.to_i + end expect(sorted).to eq(["1cm", "2cm", "4cm", "9cm", "30cm"]) end - xit 'length' do + it 'length' do words = ["heteromorph", "ancyloceratina", "bioengineering", "mathematical", "bug"] - # Your code goes here + sorted = words.sort_by do |word| + word.length + end expect(sorted).to eq(["bug", "heteromorph", "mathematical", "ancyloceratina", "bioengineering"]) end - xit 'proximity to ten' do + it 'proximity to ten' do prices = [3.02, 9.91, 17.9, 10.01, 11.0] - # Your code goes here + + sorted = prices.sort_by do |price| + (price - 10).abs + end + expect(sorted).to eq([10.01, 9.91, 11.0, 3.02, 17.9]) end - xit 'number of cents' do + it 'number of cents' do prices = [3.02, 9.91, 7.9, 10.01, 11.0] - # Your code goes here + sorted = prices.sort_by do |price| + (price.to_i - price).abs + end expect(sorted).to eq([11.0, 10.01, 3.02, 7.9, 9.91]) end end diff --git a/images/enumerables-setup-map.jpg b/images/enumerables-setup-map.jpg new file mode 100644 index 00000000..e69de29b diff --git a/iteration/exercises/spec/map_pattern_spec.rb b/iteration/exercises/spec/map_pattern_spec.rb index 22a17b77..dce699b7 100644 --- a/iteration/exercises/spec/map_pattern_spec.rb +++ b/iteration/exercises/spec/map_pattern_spec.rb @@ -9,34 +9,44 @@ expect(capitalized_names).to eq(["Alice", "Bob", "Charlie"]) end - xit 'test 2' do + it 'test 2' do family = { mother: "alice", father: "bob", brother: "charlie" } + capitalized_family = {} + family.each do |relationship, name| capitalized_family[relationship] = name.capitalize end + expected = { mother: "Alice", father: "Bob", brother: "Charlie" } + expect(capitalized_family).to eq(expected) end - xit 'test 3' do + + + it 'test 3' do numbers = [1, 2, 3, 4, 5] doubles = [] + numbers.each do |number| - # Your Code Here + doubles << number * 2 end + expect(doubles).to eq([2, 4, 6, 8, 10]) end - xit 'test 4' do + + + it 'test 4' do numbers = { one: 1, two: 2, @@ -45,9 +55,11 @@ five: 5 } doubles = {} + numbers.each do |name, number| - # Your Code Here + doubles[name] = number * 2 end + expected = { one: 2, two: 4, @@ -58,15 +70,21 @@ expect(doubles).to eq(expected) end - xit 'test 5' do + + it 'test 5' do numbers = [1, 2, 3, 4, 5] squares = [] - # Your Code Here + + numbers.each do |number| + squares << number ** 2 + end expect(squares).to eq([1, 4, 9, 16, 25]) end - xit 'test 6' do + + + it 'test 6' do numbers = { one: 1, two: 2, @@ -75,7 +93,10 @@ five: 5 } squares = {} - # Your Code Here + + numbers.each do |word, number| + squares[word] = number ** 2 + end expected = { one: 1, @@ -87,14 +108,23 @@ expect(squares).to eq(expected) end - xit 'test 7' do + + + it 'test 7' do names = ["alice", "bob", "charlie", "david", "eve"] - #Your Code Here + lengths = [] + + names.each do |name| + lengths << name.length + end expect(lengths).to eq([5, 3, 7, 5, 3]) end - xit 'test 8' do + + + + it 'test 8' do family = { mother: "alice", father: "bob", @@ -102,7 +132,12 @@ uncle: "david", sister: "eve" } - #Your Code Here + + lengths = {} + + family.each do |relationship, name| + lengths[relationship] = name.length + end expected = { mother: 5, @@ -114,14 +149,25 @@ expect(lengths).to eq(expected) end - xit 'test 9' do + + + + it 'test 9' do names = ["alice", "bob", "charlie", "david", "eve"] - #Your Code Here + + backwards = [] + + names.each do |name| + backwards << name.reverse + end expect(backwards).to eq(["ecila", "bob", "eilrahc", "divad", "eve"]) end - xit 'test 10' do + + + + it 'test 10' do family = { mother: "alice", father: "bob", @@ -129,7 +175,12 @@ uncle: "david", sister: "eve" } - #Your Code Here + + backwards = {} + + family.each do |relationship, name| + backwards[relationship] = name.reverse + end expected = { mother: "ecila", @@ -140,5 +191,6 @@ } expect(backwards).to eq(expected) end + end diff --git a/iteration/exercises/spec/max_and_min_by_pattern_spec.rb b/iteration/exercises/spec/max_and_min_by_pattern_spec.rb index 6fbd4eb5..c0b7d356 100644 --- a/iteration/exercises/spec/max_and_min_by_pattern_spec.rb +++ b/iteration/exercises/spec/max_and_min_by_pattern_spec.rb @@ -1,15 +1,22 @@ RSpec.describe 'max and min by pattern' do + it 'test 1' do numbers = [1, 100, 1000, 1000000] greatest = numbers[0] + # the original array does NOT need to be sorted first + # cuz the method below will always find the greatest number after it's full iteration + numbers.each do |number| if number > greatest greatest = number end end - expect(greatest).to eq(100000) + + expect(greatest).to eq(1000000) end + + it 'test 2' do magnitudes = { ones: 1, @@ -17,26 +24,38 @@ thousands: 1000, millions: 1000000 } + greatest = magnitudes[magnitudes.keys[0]] + # The first element (index pos. 0) of the value(s) of the keys is being called on here + magnitudes.each do |name, value| if value > greatest greatest = value end end + expect(greatest).to eq(1000000) end - xit 'test 3' do + + + it 'test 3' do meals = ["banana", "nuts", "salad", "steak", "cake"] shortest_word = meals[0] + meals.each do |meal| - # Your Code Here + if meal.length < shortest_word.length + shortest_word = meal + end end - + expect(shortest_word).to eq("nuts") end - xit 'test 4' do + + + + it 'test 4' do meals = { breakfast: "banana", snack: "nuts", @@ -45,22 +64,85 @@ dessert: "cake" } shortest_word = meals[meals.keys.first] + + # meals[meals.keys.first] =(is equal to)= meals[:breakfast] => "banana" + # meals returns hash, keys returns array, first returns string/value + # shortest_word = meals[meals.keys[0]] # This is the same as the line above + meals.each do |meal, dish| - # Your Code Here + if dish.length < shortest_word.length + shortest_word = dish + end end expect(shortest_word).to eq("nuts") end - xit 'test 5' do + + + it 'test 4.5' do + meals = { + breakfast: "banana", + snack: "nuts", + lunch: "salad", + dinner: "steak", + dessert: "cake" + } + + # longest_word = meals[meals.keys.last] + # meals.each do |meal, dish| + # if dish.length > longest_word.length + # longest_word = dish + # end + # end + # expect(longest_word).to eq("banana") + + + # words_length_array = meals.map do |meal, dish| + # [dish.length, dish] + # end.sort.group_by do |dish| + # dish.first + # end + #=> {4=>[[4, "cake"], [4, "nuts"]], 5=>[[5, "salad"], [5, "steak"]], 6=>[[6, "banana"]]} + + # words_length.sort + # words_length.sort.group_by(&:first) + # &: is a shortcut for + + + # .map returns a modified ARRAY / .each returns the original collection (hash/array) + words_length_array = meals.map do |meal, dish| + [dish.length, dish] + end.sort + require 'pry'; binding.pry + + expect(words_length_array.first.last).to eq("cake") + #=> words_length_array: [[4, "cake"], [4, "nuts"], [5, "salad"], [5, "steak"], [6, "banana"]] + #=> words_length_array.first: [4, "cake"] + #=> words_length_array.first.last: "cake" + + expect(words_length_array.last.last).to eq("banana") + end + + + + + it 'test 5' do stats = [3001, 431, 1695, 0.27601, 0.340] most_digits = stats[0] - # Your Code Here + + stats.each do |element| + if most_digits.to_s.delete(".").length < element.to_s.delete(".").length + most_digits = element + end + end expect(most_digits).to eq(0.27601) end - xit 'test 6' do + + + it 'test 6' do stats = { games_played: 3001, home_runs: 431, @@ -68,19 +150,39 @@ batting_average: 0.27601, on_base_percentage: 0.340 } + most_digits = stats[stats.keys.first] - # Your Code Here + + stats.each do |title, number| + # if there was only one value in the || it would default to the ARRAY of the key-value PAIR + if most_digits.to_s.delete(".").length < number.to_s.delete(".").length + most_digits = number + end + end + expect(most_digits).to eq(0.27601) end - xit 'test 7' do + + + + it 'test 7' do ages = [39, 45, 29, 24, 50] - # Your Code Here + oldest = ages[0] + + ages.each do |age| + if age > oldest + oldest = age + end + end expect(oldest).to eq(50) end + + + xit 'test 8' do ages = { abdi: 39, @@ -89,12 +191,47 @@ margaret: 24, miguel: 50 } - # Your Code Here + + oldest = {} + # # We must have this variable set to something so it can be redefined inside the code block + oldest_age = ages[ages.keys.first] + # oldest_age = ages.values.first #=> only this still passes the test + +# ages[ages.keys.first] =true= ages.values.first + + ages.each do |name, age| + if age > oldest_age + oldest_age = age + oldest = {name: name.to_s, age: age} + # Here we redefine that previously made empty hash + end + end expected = {name: "miguel", age: 50} expect(oldest).to eq(expected) + + ## ATTEMPT TO USE .MAP ## + + oldest_age = ages[ages.keys.first] + oldest = ages.map do |name, age| + if age > oldest_age + oldest_age = age + {name: name.to_s, age: age} + end + end.compact + expected = {name: "miguel", age: 50} + # we use .compact cuz .map returns nil for elements that's do NOT pass the if conditional! + expect(oldest.last).to eq(expected) + #if you don't put .last you will get an array returned cuz .map returns an array, + # AND the array will include multiple hashes (last line of our code block) that were created + # so you only want the LAST one/oldest one! end + + + + + xit 'test 9' do programmers = [["katrina", "sandi", "jim", "aaron", "desi"], ["abby", "jon", "susan"]] # Your Code Here @@ -102,6 +239,9 @@ expect(fewest_programmers).to eq(["abby", "jon", "susan"]) end + + + xit 'test 10' do programmers = {ruby: ["katrina", "sandi", "jim", "aaron", "desi"], java: ["abby", "jon", "susan"]} # Your Code Here diff --git a/mythical-creatures/README.md b/mythical-creatures/README.md index df22a247..af3a30d2 100644 --- a/mythical-creatures/README.md +++ b/mythical-creatures/README.md @@ -36,6 +36,7 @@ Continue to follow the errors that your test provides until the test passes. The * `werewolf_spec.rb` * `centaur_spec.rb` * `ogre_spec.rb` +* `lovisa_spec.rb` * `direwolf_spec.rb` * `the_journey_spec.rb` (see below) diff --git a/mythical-creatures/lib/centaur.rb b/mythical-creatures/lib/centaur.rb new file mode 100644 index 00000000..516c4abb --- /dev/null +++ b/mythical-creatures/lib/centaur.rb @@ -0,0 +1,83 @@ +class Centaur + attr_reader :name, + :breed + + def initialize(name, breed) + @name = name + @breed = breed + @cranky = false + @standing = true + @runs = 0 + @shots = 0 + @sick = 0 + end + + def shoot + if @shots < 3 && @standing + @shots += 1 + 'Twang!!!' + else + 'NO!' + end + end + + def run + if @standing + @runs +=1 + 'Clop clop clop clop!' + else + 'NO!' + end + end + + def cranky? + check_if_cranky + @cranky + end + + def check_if_cranky + if @runs + @shots >= 3 + @cranky = true + else + @cranky = false + end + end + + def standing? + @standing + end + + def sleep + if @standing + 'NO!' + else + @shots = 0 + @runs = 0 + end + end + + def lay_down + @standing = false + end + + def laying? + !@standing + end + + def stand_up + @standing = true + end + + def drink_potion + if @standing && @cranky == false + @sick = true + elsif @standing && @cranky == true + @shots = 0 + @runs = 0 + end + end + + def sick? + @sick + end +end \ No newline at end of file diff --git a/mythical-creatures/lib/direwolf.rb b/mythical-creatures/lib/direwolf.rb new file mode 100644 index 00000000..2a1644f2 --- /dev/null +++ b/mythical-creatures/lib/direwolf.rb @@ -0,0 +1,56 @@ +class Direwolf + attr_reader :name, + :home, + :size, + :starks_to_protect, + :location + + def initialize(name, home = 'Beyond the Wall', size = 'Massive') + @name = name + @home = home + @size = size + @starks_to_protect = [] + @location = home + @hunts_white_walkers = true + end + + def protects(a_stark) + if a_stark.location == @location && @starks_to_protect.length < 2 + @starks_to_protect << a_stark + a_stark.safe = true + @hunts_white_walkers = false + end + end + + def hunts_white_walkers? + @hunts_white_walkers + end + + def leaves(a_stark) + a_stark.safe = false + @starks_to_protect.delete(a_stark) + end + +end + +class Stark + attr_reader :name, + :home, + :location, + :house_words + + attr_accessor :safe + + def initialize(name, location = 'Winterfell') + @name = name + @home = "Winderfell" + @location = location + @safe = false + @house_words = 'Winter is Coming' + end + + def safe? + @safe + end + +end \ No newline at end of file diff --git a/mythical-creatures/lib/dragon.rb b/mythical-creatures/lib/dragon.rb new file mode 100644 index 00000000..5f6dbdce --- /dev/null +++ b/mythical-creatures/lib/dragon.rb @@ -0,0 +1,27 @@ +class Dragon + attr_reader :name, + :color, + :rider, + :hungry, + :meals + + def initialize(name, color, rider) + @name = name + @color = color + @rider = rider + @hungry = true + @meals = 0 + end + + def hungry? + @hungry + end + + def eat + @meals += 1 + if @meals >=3 + @hungry = false + end + end + +end \ No newline at end of file diff --git a/mythical-creatures/lib/hobbit.rb b/mythical-creatures/lib/hobbit.rb new file mode 100644 index 00000000..0a053458 --- /dev/null +++ b/mythical-creatures/lib/hobbit.rb @@ -0,0 +1,35 @@ +class Hobbit + attr_reader :name, + :disposition, + :age, + :is_short + + def initialize(name, disposition = "homebody") + @name = name + @disposition = disposition + @age = 0 + @is_short = true + end + + def celebrate_birthday + @age += 1 + end + + def adult? + @age > 32 + # doesn't need an if statement because adult? will return a boolean + end + + def old? + @age > 100 + end + + def has_ring? + @name == "Frodo" + end + + def is_short? + @is_short + end + +end \ No newline at end of file diff --git a/mythical-creatures/lib/lovisa.rb b/mythical-creatures/lib/lovisa.rb new file mode 100644 index 00000000..f3328003 --- /dev/null +++ b/mythical-creatures/lib/lovisa.rb @@ -0,0 +1,21 @@ +class Lovisa + attr_reader :title, + :characteristics + + def initialize(title, characteristics = ["brilliant"]) + @title = title + @characteristics = characteristics + end + + def brilliant? + characteristics.include?("brilliant") + end + + def kind? + characteristics.include?("kind") + end + + def say(words) + "**;* #{words} **;*" + end +end \ No newline at end of file diff --git a/mythical-creatures/lib/medusa.rb b/mythical-creatures/lib/medusa.rb new file mode 100644 index 00000000..72b6a45c --- /dev/null +++ b/mythical-creatures/lib/medusa.rb @@ -0,0 +1,45 @@ +class Medusa + attr_reader :name, + :statues + + def initialize(name) + @name = name + @statues = [] + @victims_full = false + end + + def stare(victim) + @statues << victim + victim.stoned = true + revive + end + + def revive + if @statues.count > 3 + @statues[0].stoned = false + @statues.shift + end + end + + def victims_full? + if @statues.count >= 3 + @victims_full = true + else + false + end + end +end + +class Person + attr_reader :name + attr_accessor :stoned + + def initialize(name) + @name = name + @stoned = false + end + + def stoned? + @stoned + end +end \ No newline at end of file diff --git a/mythical-creatures/lib/ogre.rb b/mythical-creatures/lib/ogre.rb new file mode 100644 index 00000000..c8151e2d --- /dev/null +++ b/mythical-creatures/lib/ogre.rb @@ -0,0 +1,58 @@ +class Ogre + attr_reader :name, + :home, + :swings, + :encounter_counter + + def initialize(name, home = "Swamp") + @name = name + @home = home + @swings = 0 + @encounter_counter = 0 + end + + def encounter(human) + human.encounter_counter += 1 + @encounter_counter += 1 + if human.encounter_counter >= 3 + human.notices_ogre = true + swing_at(human) + human.encounter_counter = 0 + end + end + + def swing_at(human) + @swings += 1 + if @swings.even? + human.knocked_out = true + end + end + + def apologize(human) + human.knocked_out = false + end + +end + +class Human + attr_reader :name + attr_accessor :encounter_counter, + :notices_ogre, + :knocked_out + + def initialize + @name = "Jane" + @encounter_counter = 0 + @notices_ogre = false + @knocked_out = false + end + + def notices_ogre? + @notices_ogre + end + + def knocked_out? + @knocked_out + end + +end \ No newline at end of file diff --git a/mythical-creatures/lib/phoenix.rb b/mythical-creatures/lib/phoenix.rb new file mode 100644 index 00000000..68d0c485 --- /dev/null +++ b/mythical-creatures/lib/phoenix.rb @@ -0,0 +1,109 @@ +class Phoenix + attr_reader :name, + :color, + :mood, + :emotional_awareness, + :pharaoh + + def initialize(name, color = "golden", mood = "stoic") + @name = name + @color = color + @mood = mood + @emotional_awareness = {} + @releases_tear = false + @pharaoh = nil + end + + def bursts_into_flames + @color = "golden" + @mood = "stoic" + @pharaoh = nil + @emotional_awareness = {} + @releases_tear = false + end + + def feels_emotion(emotion) + if @emotional_awareness.has_key?(emotion) + @emotional_awareness[emotion] += 1 + + #pharaoh dies! + if @emotional_awareness[emotion] == 2 + @color = "scarlet" + @mood = "fiery" + elsif @emotional_awareness[emotion] == 3 + @color = "crimson" + @mood = "ablaze" + @releases_tear = true + @pharaoh.healthy = true if @pharaoh != nil + # if @pharaoh != nil + # pharaoh.healthy = true + # end + elsif @emotional_awareness[emotion] == 4 + @releases_tear = false + @color = "deep violet" + @mood = "incandescent" + elsif @emotional_awareness[emotion] >= 5 + bursts_into_flames + end + + else + @emotional_awareness[emotion] = 1 + @color = "amber" + @mood = "heated" + end + end + + def releases_tear? + @releases_tear + end + + def follows_pharaoh(pharaoh) + @pharaoh = pharaoh + end +end + + +class Pharaoh + attr_reader :name, + :reputation, + :dynastic_period, + :phoenix + + attr_accessor :age, :healthy + + def initialize(name, reputation, dynastic_period, phoenix) + @name = name + @reputation = reputation + @dynastic_period = dynastic_period + @phoenix = phoenix + @healthy = true + @dead = false + @age = 0 + end + + def healthy? + @healthy + end + + def ages + @age += 1 + if @age >= 18 + @healthy = false + end + end + + def takes_action(emotion) + @phoenix.feels_emotion(emotion) + end + + def dead? + @dead + end + + def dies + #DOES this make sense here? NO = it should be in the Phoneix class + 5.times { @phoenix.feels_emotion(:sorrow) } + @dead = true + @healthy = false + end +end diff --git a/mythical-creatures/lib/pirate.rb b/mythical-creatures/lib/pirate.rb new file mode 100644 index 00000000..5eaa568e --- /dev/null +++ b/mythical-creatures/lib/pirate.rb @@ -0,0 +1,29 @@ +class Pirate + attr_reader :name, + :job, + :cursed, + :booty + + def initialize(name, job = "Scallywag") + @name = name + @job = job + @cursed = false + @heinous_acts = 0 + @booty = 0 + end + + def cursed? + @cursed + end + + def commit_heinous_act + @heinous_acts += 1 + if @heinous_acts >= 3 + @cursed = true + end + end + + def rob_ship + @booty += 100 + end +end \ No newline at end of file diff --git a/mythical-creatures/lib/unicorn.rb b/mythical-creatures/lib/unicorn.rb index e69de29b..e9add4d1 100644 --- a/mythical-creatures/lib/unicorn.rb +++ b/mythical-creatures/lib/unicorn.rb @@ -0,0 +1,24 @@ +class Unicorn + attr_reader :name, + :color, + :pockets + + + def initialize(name, color = "silver") + @name = name + @color = color + @pockets = [] + end + + # def pockets + # pockets = [] + # end + + def silver? + @color == "silver" + end + + def say(text) + "**;* #{text} **;*" + end +end diff --git a/mythical-creatures/lib/vampire.rb b/mythical-creatures/lib/vampire.rb new file mode 100644 index 00000000..f5ef6fae --- /dev/null +++ b/mythical-creatures/lib/vampire.rb @@ -0,0 +1,19 @@ +class Vampire + attr_reader :name, + :pet, + :thirsty + + def initialize(name, pet = "bat") + # pet in paramaters above are a default-optional argument + # but pet is change-able cuz it's ALSO below too: + @name = name + @pet = pet + @thirsty = true + #thirsty here is good cuz as an argument (above) you wouldn't know WHAT is true + end + + def drink + @thirsty = false + end + +end \ No newline at end of file diff --git a/mythical-creatures/lib/werewolf.rb b/mythical-creatures/lib/werewolf.rb new file mode 100644 index 00000000..4fed2a23 --- /dev/null +++ b/mythical-creatures/lib/werewolf.rb @@ -0,0 +1,56 @@ +class Werewolf + attr_reader :name, + :location, + :victims + + + def initialize(name, location = "London") + @name = name + @location = location + @human = true + @hungry = false + @victims = [] + end + + def human? + @human + end + + def wolf? + if human? + false + else + true + end + end + + def change! + if human? + @human = false + @hungry = true + else + @human = true + @hungry = false + end + end + + def hungry? + @hungry + end + + def consumes(victim) + if wolf? + @victims << victim + @hungry = false + victim.status = :dead_as_a_doorknob + end + end +end + +class Victim + attr_accessor :status + + def initialize + @status = :alive + end +end \ No newline at end of file diff --git a/mythical-creatures/lib/wizard.rb b/mythical-creatures/lib/wizard.rb new file mode 100644 index 00000000..14de543e --- /dev/null +++ b/mythical-creatures/lib/wizard.rb @@ -0,0 +1,45 @@ +class Wizard + attr_reader :name, + :bearded + # don't need rested here because we don't call it outside the class file + + def initialize(name, bearded: true) + @name = name + @bearded = bearded + @rested = true + @spells = 0 + end + + def bearded? + @bearded + end + + def incantation(input) + "sudo #{input}" + end + + def rested? + @rested + end + + def cast + if @spells < 2 + @spells += 1 + "MAGIC MISSILE!" + else + @rested = false + end + # this method doesn't allow the wizard to cast any spells + # AFTER the wizard is tired! + + # OPTIONS 2 # + + # @spells += 1 + # if @spells < 3 + # else + # @rested = false + # end + # "MAGIC MISSILE!" + # this last line is the return so it MUST go at the end + end +end \ No newline at end of file diff --git a/mythical-creatures/spec/centaur_spec.rb b/mythical-creatures/spec/centaur_spec.rb index 9dfcd0d1..a3e68237 100644 --- a/mythical-creatures/spec/centaur_spec.rb +++ b/mythical-creatures/spec/centaur_spec.rb @@ -113,14 +113,44 @@ end it 'becomes rested after drinking a potion' do - # your code here + centaur = Centaur.new('George', 'Palomino') + + centaur.shoot + centaur.run + centaur.shoot + expect(centaur.cranky?).to be true + + centaur.drink_potion + expect(centaur.cranky?).to be false + + centaur.run + centaur.shoot + expect(centaur.cranky?).to be false + + centaur.shoot + expect(centaur.cranky?).to be true end it 'can only drink a potion whilst standing' do - # your code here + centaur = Centaur.new('George', 'Palomino') + + centaur.shoot + centaur.run + centaur.shoot + expect(centaur.cranky?).to be true + + centaur.lay_down + centaur.drink_potion + + expect(centaur.cranky?).to be true end - it 'gets stick if a potion is drunk while rested' do - # your code here + it 'gets sick if a potion is drunk while rested' do + centaur = Centaur.new('George', 'Palomino') + expect(centaur.cranky?).to be false + + centaur.drink_potion + + expect(centaur.sick?).to be true end end diff --git a/mythical-creatures/spec/direwolf_spec.rb b/mythical-creatures/spec/direwolf_spec.rb index 345cc5a2..e5785e9c 100644 --- a/mythical-creatures/spec/direwolf_spec.rb +++ b/mythical-creatures/spec/direwolf_spec.rb @@ -66,20 +66,23 @@ it 'can only protect two Starks at a time' do summer_wolf = Direwolf.new('Summer', "Winterfell") lady_wolf = Direwolf.new('Lady', "Winterfell") + sansa_stark = Stark.new('Sansa') - john_stark = Stark.new('Jon') + jon_stark = Stark.new('Jon') rob_stark = Stark.new('Rob') bran_stark = Stark.new('Bran') arya_stark = Stark.new('Arya') summer_wolf.protects(sansa_stark) - summer_wolf.protects(john_stark) + summer_wolf.protects(jon_stark) + lady_wolf.protects(rob_stark) lady_wolf.protects(bran_stark) lady_wolf.protects(arya_stark) expect(summer_wolf.starks_to_protect).to include(sansa_stark) expect(summer_wolf.starks_to_protect).to include(jon_stark) + expect(lady_wolf.starks_to_protect).to include(rob_stark) expect(lady_wolf.starks_to_protect).to include(bran_stark) expect(lady_wolf.starks_to_protect).to_not include(arya_stark) @@ -121,27 +124,31 @@ it 'can leave and stop protecting Starks' do summer_wolf = Direwolf.new('Summer', "Winterfell") lady_wolf = Direwolf.new('Lady', "Winterfell") + sansa_stark = Stark.new('Sansa') arya_stark = Stark.new('Arya') summer_wolf.protects(arya_stark) lady_wolf.protects(sansa_stark) + summer_wolf.leaves(arya_stark) expect(summer_wolf.starks_to_protect).to be_empty - expect(lady_wolf.starks_to_protect.first.name).to be('Sansa') + expect(lady_wolf.starks_to_protect.first.name).to eq('Sansa') expect(arya_stark.safe?).to be false end it 'returns the Stark object when it leaves' do summer_wolf = Direwolf.new('Summer', "Winterfell") lady_wolf = Direwolf.new('Lady', "Winterfell") + sansa_stark = Stark.new('Sansa') arya_stark = Stark.new('Arya') rickon_stark = Stark.new('Rickon') summer_wolf.protects(arya_stark) - lady_wolf.protects(sansa_stark) + lady_wolf.protects(rickon_stark) + summer_wolf.leaves(arya_stark) expected = lady_wolf.leaves(rickon_stark) diff --git a/mythical-creatures/spec/hobbit_spec.rb b/mythical-creatures/spec/hobbit_spec.rb index f49faed3..beb08ad0 100644 --- a/mythical-creatures/spec/hobbit_spec.rb +++ b/mythical-creatures/spec/hobbit_spec.rb @@ -58,21 +58,38 @@ expect(hobbit.adult?).to be true end - xit 'is old at the age of 101' do + it 'is old at the age of 101' do # create a hobbit # have hobbit age 101 years # check that hobbit.old? returns true + hobbit = Hobbit.new('Otho') + + expect(hobbit.old?).to eq(false) + + 101.times do + hobbit.celebrate_birthday + end + + expect(hobbit.old?).to eq(true) end - xit 'it has the ring if its name is Frodo' do + it 'it has the ring if its name is Frodo' do # create a hobbit named Frodo # create a second hobbit named Sam # check that .has_ring? for Frodo returns true # check that .has_ring? for Sam returns false + sam = Hobbit.new('Samwise') + frodo = Hobbit.new('Frodo') + + expect(frodo.has_ring?).to be true + expect(sam.has_ring?).to be false end - xit 'they are short' do + it 'they are short' do # create a hobbit # check that is_short? returns true + sam = Hobbit.new('Samwise') + + expect(sam.is_short?).to be true end end diff --git a/mythical-creatures/spec/lovisa_spec.rb b/mythical-creatures/spec/lovisa_spec.rb index 50cbd6f4..722e7bf4 100644 --- a/mythical-creatures/spec/lovisa_spec.rb +++ b/mythical-creatures/spec/lovisa_spec.rb @@ -4,27 +4,28 @@ RSpec.describe Lovisa do it 'she has a title' do lovisa = Lovisa.new('Lovisa the Swedish Goddess') + expect(lovisa.title).to eq('Lovisa the Swedish Goddess') end it 'she is brilliant by default' do lovisa = Lovisa.new('Lovisa the Mentor') + expect(lovisa.characteristics).to eq(['brilliant']) - expect(lovisa.brilliant?).to eq(true) expect(lovisa.brilliant?).to be true end it "she is more than brilliant" do loivsa = Lovisa.new('Lovisa the friend', ['brilliant', 'kind']) + expect(loivsa.characteristics).to eq(['brilliant', 'kind']) - expect(loivsa.brilliant?).to eq(true) expect(loivsa.brilliant?).to be true - expect(loivsa.kind?).to eq(true) expect(loivsa.kind?).to be true end it 'she says sparkly stuff' do loivsa = Lovisa.new('Lovisa the Loved') + expect(loivsa.say('Wonderful!')).to eq('**;* Wonderful! **;*') expect(loivsa.say('You are doing great!')).to eq('**;* You are doing great! **;*') end diff --git a/mythical-creatures/spec/medusa_spec.rb b/mythical-creatures/spec/medusa_spec.rb index 628c02ef..40659e53 100644 --- a/mythical-creatures/spec/medusa_spec.rb +++ b/mythical-creatures/spec/medusa_spec.rb @@ -32,10 +32,42 @@ end it 'can only have three victims' do - # your code here + medusa = Medusa.new('Cassiopeia') + leon = Person.new('Leon') + mel = Person.new('Mel') + huy = Person.new('Huy') + cyclops = Person.new('Cyclops') + + medusa.stare(leon) + medusa.stare(mel) + expect(medusa.victims_full?).to be false + + medusa.stare(huy) + expect(medusa.victims_full?).to be true end it 'if a fourth victim is stoned the first is unstoned' do - # your code here + medusa = Medusa.new('Cassiopeia') + leon = Person.new('Leon') + mel = Person.new('Mel') + huy = Person.new('Huy') + cyclops = Person.new('Cyclops') + + medusa.stare(leon) + medusa.stare(mel) + expect(medusa.victims_full?).to be false + + medusa.stare(huy) + expect(medusa.victims_full?).to be true + + medusa.stare(cyclops) + + expect(leon.stoned?).to be false + expect(medusa.victims_full?).to be true + expect(medusa.statues.count).to eq(3) + + expect(mel.stoned?).to be true + expect(huy.stoned?).to be true + expect(cyclops.stoned?).to be true end end diff --git a/mythical-creatures/spec/ogre_spec.rb b/mythical-creatures/spec/ogre_spec.rb index 1e8f1cf3..dd04448f 100644 --- a/mythical-creatures/spec/ogre_spec.rb +++ b/mythical-creatures/spec/ogre_spec.rb @@ -80,7 +80,9 @@ 6.times { ogre.encounter(human) } expect(ogre.encounter_counter).to eq(6) + expect(ogre.swings).to eq(2) + expect(human.knocked_out?).to be true end diff --git a/mythical-creatures/spec/phoenix_spec.rb b/mythical-creatures/spec/phoenix_spec.rb new file mode 100644 index 00000000..941f9001 --- /dev/null +++ b/mythical-creatures/spec/phoenix_spec.rb @@ -0,0 +1,189 @@ +require 'rspec' +require './lib/phoenix' + +RSpec.describe Phoenix do + + describe "The Phoenix was Self-created at the Beginning of Time" do + it "exists" do + phoenix = Phoenix.new("Bennu") + + expect(phoenix.name).to eq("Bennu") + end + + it "is born golden, stoic, and without a pharaoh" do + phoenix = Phoenix.new("Bennu") + + expect(phoenix.color).to eq("golden") + expect(phoenix.mood).to eq("stoic") + expect(phoenix.pharaoh).to eq(nil) + end + + it "changes color & mood when feeling an emotion 1, 2, 3, and 4 times" do + phoenix = Phoenix.new("Bennu") + + phoenix.feels_emotion(:cognizance) + expect(phoenix.color).to eq("amber") + expect(phoenix.mood).to eq("heated") + + phoenix.feels_emotion(:cognizance) + expect(phoenix.color).to eq("scarlet") + expect(phoenix.mood).to eq("fiery") + + phoenix.feels_emotion(:cognizance) + expect(phoenix.color).to eq("crimson") + expect(phoenix.mood).to eq("ablaze") + + phoenix.feels_emotion(:cognizance) + expect(phoenix.color).to eq("deep violet") + expect(phoenix.mood).to eq("incandescent") + end + + it "has emotional awareness about how many times it has the same emotion" do + phoenix = Phoenix.new("Bennu") + + phoenix.feels_emotion(:exuberance) + + phoenix.feels_emotion(:curiosity) + phoenix.feels_emotion(:curiosity) + phoenix.feels_emotion(:curiosity) + + phoenix.feels_emotion(:gratitude) + phoenix.feels_emotion(:gratitude) + + expect(phoenix.emotional_awareness).to eq({:exuberance => 1, :curiosity => 3, :gratitude => 2}) + expect(phoenix.emotional_awareness.include?(:sorrow)).to eq(false) + end + + it "releases a tear after feeling the same emotion on the 3rd time ONLY" do + phoenix = Phoenix.new("Bennu") + + 2.times { phoenix.feels_emotion(:confusion) } + expect(phoenix.releases_tear?).to be false + + phoenix.feels_emotion(:confusion) + expect(phoenix.releases_tear?).to be true + + phoenix.feels_emotion(:confusion) + expect(phoenix.releases_tear?).to be false + end + + it "bursts into flames and is reborn after feeling the same emotion 5 times" do + phoenix = Phoenix.new("Bennu") + + 5.times { phoenix.feels_emotion(:revelation) } + + expect(phoenix.color).to eq("golden") + expect(phoenix.mood).to eq("stoic") + expect(phoenix.pharaoh).to eq(nil) + expect(phoenix.emotional_awareness).to eq({}) + end + end + + describe "The Phoenix throughout Ancient Egypt" do + it "a pharaoh has a name, reputation, dynastic period, and the phoenix" do + phoenix = Phoenix.new("Bennu") + narmer = Pharaoh.new("Narmer", "The Unifier", "3100 BCE", phoenix) + + expect(narmer.name).to eq("Narmer") + expect(narmer.reputation).to eq("The Unifier") + expect(narmer.dynastic_period).to eq("3100 BCE") + expect(narmer.phoenix).to eq(phoenix) + end + + it "a pharaoh can check if they are healthy" do + phoenix = Phoenix.new("Bennu") + narmer = Pharaoh.new("Narmer", "The Unifier", "3100 BCE", phoenix) + + expect(narmer.healthy?).to eq(true) + end + + it "a pharaoh can age" do + phoenix = Phoenix.new("Bennu") + narmer = Pharaoh.new("Narmer", "The Unifier", "3100 BCE", phoenix) + + 24.times { narmer.ages } + + expect(narmer.age).to eq(24) + end + + it "a pharaoh can die" do + phoenix = Phoenix.new("Bennu") + narmer = Pharaoh.new("Narmer", "The Unifier", "3100 BCE", phoenix) + + narmer.age = 60 + expect(narmer.dead?).to eq(false) + + narmer.dies + expect(narmer.dead?).to eq(true) + end + + it "the phoenix chooses to follow the pharaoh" do + phoenix = Phoenix.new("Bennu") + khufu = Pharaoh.new("Khufu", "The Builder", "3150 BCE", phoenix) + + phoenix.follows_pharaoh(khufu) + + expect(phoenix.pharaoh).to eq(khufu) + end + + it "the phoenix feels an emotion when the pharaoh takes an action" do + phoenix = Phoenix.new("Bennu") + khufu = Pharaoh.new("Khufu", "The Builder", "3150 BCE", phoenix) + phoenix.follows_pharaoh(khufu) + + khufu.takes_action(:perseverance) + + expect(phoenix.emotional_awareness).to eq({:perseverance => 1}) + end + + it "the pharaoh is unhealthy at the age of 18 or older" do + phoenix = Phoenix.new("Bennu") + khufu = Pharaoh.new("Khufu", "The Builder", "3150 BCE", phoenix) + phoenix.follows_pharaoh(khufu) + + khufu.age = 17 + expect(khufu.healthy?).to eq(true) + + khufu.ages + expect(khufu.healthy?).to eq(false) + + khufu.ages + expect(khufu.healthy?).to eq(false) + end + + it "the unhealthy pharaoh becomes healthy after the phoenix releases a tear" do + phoenix = Phoenix.new("Bennu") + tutankhamun = Pharaoh.new("Tutankhamun", "The Child", "1500 BCE", phoenix) + phoenix.follows_pharaoh(tutankhamun) + + 18.times { tutankhamun.ages } + expect(tutankhamun.healthy?).to eq(false) + + 3.times { tutankhamun.takes_action(:compassion) } + expect(phoenix.releases_tear?).to eq(true) + + expect(tutankhamun.healthy?).to eq(true) + end + + it "when the pharaoh dies the phoenix feels sorrow 5 times, bursts into flames, and is reborn" do + phoenix = Phoenix.new("Bennu") + tutankhamun = Pharaoh.new("Tutankhamun", "The Child", "1500 BCE", phoenix) + phoenix.follows_pharaoh(tutankhamun) + + 4.times { tutankhamun.takes_action(:trepidation) } + + expect(phoenix.color).to eq("deep violet") + expect(phoenix.mood).to eq("incandescent") + expect(phoenix.pharaoh).to eq(tutankhamun) + expect(phoenix.emotional_awareness).to eq({:trepidation => 4}) + + tutankhamun.dies + expect(tutankhamun.dead?).to eq(true) + + expect(phoenix.color).to eq("golden") + expect(phoenix.mood).to eq("stoic") + expect(phoenix.pharaoh).to eq(nil) + expect(phoenix.emotional_awareness).to eq({}) + end + end +end \ No newline at end of file diff --git a/mythical-creatures/spec/pirate_spec.rb b/mythical-creatures/spec/pirate_spec.rb index ec2c086e..28c87f9a 100644 --- a/mythical-creatures/spec/pirate_spec.rb +++ b/mythical-creatures/spec/pirate_spec.rb @@ -40,12 +40,25 @@ it 'has a booty' do # create a pirate # check that the pirate starts with 0 booty + pirate = Pirate.new('Jack') + + expect(pirate.booty).to eq(0) end it 'gets 100 booty for robbing a ship' do # create a pirate # rob some ships # check that the pirate got 100 booty for each ship it robbed + pirate = Pirate.new('Jack') + + pirate.rob_ship + expect(pirate.booty).to eq(100) + + pirate.rob_ship + expect(pirate.booty).to eq(200) + + pirate.rob_ship + expect(pirate.booty).to eq(300) end end diff --git a/mythical-creatures/spec/unicorn_spec.rb b/mythical-creatures/spec/unicorn_spec.rb index 477028c2..0319bf3f 100644 --- a/mythical-creatures/spec/unicorn_spec.rb +++ b/mythical-creatures/spec/unicorn_spec.rb @@ -3,26 +3,32 @@ RSpec.describe Unicorn do it 'has a name' do - unicorn = Unicorn.new('Robert') - expect(unicorn.name).to eq('Robert') + robert_unicorn = Unicorn.new('Robert') + + expect(robert_unicorn.name).to eq('Robert') + expect(robert_unicorn.silver?).to eq(true) + expect(robert_unicorn.pockets).to eq([]) end - it 'is silver by default' do + xit 'is silver by default' do unicorn = Unicorn.new('Margaret') + expect(unicorn.color).to eq('silver') expect(unicorn.silver?).to eq(true) - expect(unicorn.silver?).to be true + # expect(unicorn.silver?).to be true end - it 'doesnt have to be silver' do + xit 'doesnt have to be silver' do unicorn = Unicorn.new('Barbara', 'purple') + expect(unicorn.color).to eq('purple') expect(unicorn.silver?).to eq(false) - expect(unicorn.silver?).to be false + # expect(unicorn.silver?).to be false end - it 'says sparkly stuff' do + xit 'says sparkly stuff' do unicorn = Unicorn.new('Johnny') + expect(unicorn.say('Wonderful!')).to eq('**;* Wonderful! **;*') expect(unicorn.say('I dont like you very much.')).to eq('**;* I dont like you very much. **;*') end diff --git a/mythical-creatures/spec/werewolf_spec.rb b/mythical-creatures/spec/werewolf_spec.rb index aab327ca..aa2588cd 100644 --- a/mythical-creatures/spec/werewolf_spec.rb +++ b/mythical-creatures/spec/werewolf_spec.rb @@ -14,12 +14,15 @@ it 'is by default human' do werewolf = Werewolf.new('David', 'London') - expect(werewolf.human?).to be false + + expect(werewolf.human?).to be true end it 'when starting as a human, changing makes it turn into a werewolf' do werewolf = Werewolf.new('David', 'London') + werewolf.change! + expect(werewolf.wolf?).to be true expect(werewolf.human?).to be false end @@ -50,35 +53,70 @@ end it 'is not hungry by default' do - # your code here + werewolf = Werewolf.new('David', 'London') + + expect(werewolf.hungry?).to be false end it 'becomes hungry after changing to a werewolf' do - # your code here + werewolf = Werewolf.new('David', 'London') + + expect(werewolf.hungry?).to be false + werewolf.change! + expect(werewolf.hungry?).to be true end - class Victim - attr_accessor :status + # class Victim + # attr_accessor :status - def initialize - @status = :alive - end - end + # def initialize + # @status = :alive + # end + # end it 'consumes a victim' do - # your code here + werewolf = Werewolf.new('David', 'London') + victim = Victim.new + + werewolf.change! + + expect(werewolf.victims).to eq([]) + + werewolf.consumes(victim) + + expect(werewolf.victims).to eq([victim]) end - it 'cannot consume a victim if it is in human form' do - # your code here + it 'cannot consume a victim if (the werewolf) is in human form' do + werewolf = Werewolf.new('David', 'London') + victim = Victim.new + + werewolf.consumes(victim) + + expect(werewolf.victims).to eq([]) end it 'a werewolf that has consumed a human being is no longer hungry' do - # your code here + werewolf = Werewolf.new('David', 'London') + victim = Victim.new + + werewolf.change! + + expect(werewolf.hungry?).to be true + + werewolf.consumes(victim) + + expect(werewolf.hungry?).to be false end it 'a werewolf who has consumed a victim makes the victim dead' do - # your code here + werewolf = Werewolf.new('David', 'London') + victim = Victim.new + + werewolf.change! + werewolf.consumes(victim) + + expect(victim.status).to eq(:dead_as_a_doorknob) end end diff --git a/mythical-creatures/spec/wizard_spec.rb b/mythical-creatures/spec/wizard_spec.rb index fbd73b71..fe0d03dd 100644 --- a/mythical-creatures/spec/wizard_spec.rb +++ b/mythical-creatures/spec/wizard_spec.rb @@ -3,8 +3,8 @@ RSpec.describe Wizard do it 'has a name' do - wizard = Wizard.new('Eric') - expect(wizard.name).to eq('Eric') + wizard = Wizard.new('Erik') + expect(wizard.name).to eq('Erik') end it 'has a different name' do @@ -35,11 +35,17 @@ it 'starts rested' do # create wizard # .rested? returns true + wizard = Wizard.new('Stella', bearded: false) + + expect(wizard.rested?).to be true end it 'can cast spells' do # create wizard # .cast returns "MAGIC MISSILE!" + wizard = Wizard.new('Sal', bearded: true) + + expect(wizard.cast).to eq("MAGIC MISSILE!") end it 'gets tired after casting three spells' do @@ -48,5 +54,13 @@ # check if wizard is rested # casts spell # check wizard is not rested + wizard = Wizard.new('Sal', bearded: true) + + wizard.cast + wizard.cast + expect(wizard.rested?).to be true + + wizard.cast + expect(wizard.rested?).to be false end end