Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions solutions/ruby_basics/10_basic_enumerables/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper --format documentation --color
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def display_current_inventory(inventory_list)
# use #each to iterate through each item of the inventory_list (a hash)
# use puts to output each list item "<key>, quantity: <value>" to console
inventory_list.each do |key, value|
puts "#{key}, quantity: #{value}"
end
end

def display_guess_order(guesses)
# use #each_with_index to iterate through each item of the guesses (an array)
# use puts to output each list item "Guess #<number> is <item>" to console
# hint: the number should start with 1
guesses.each_with_index do |guess, index|
puts "Guess ##{index + 1} is #{guess}"
end
end

def find_absolute_values(numbers)
# use #map to iterate through each item of the numbers (an array)
# return an array of absolute values of each number
numbers.map { |number| number.abs }
end

def find_low_inventory(inventory_list)
# use #select to iterate through each item of the inventory_list (a hash)
# return a hash of items with values less than 4
inventory_list.select { |_key, value| value < 4 }
end

def find_word_lengths(word_list)
# use #reduce to iterate through each item of the word_list (an array)
# return a hash with each word as the key and its length as the value
# hint: look at the documentation and review the reduce examples in basic enumerable lesson
word_list.reduce(Hash.new) do |length_hash, word|
length_hash[word] = word.length # This adds the word as the key & its length as the value
length_hash # This is the required return value for each iteration
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'spec_helper'
require_relative '../exercises/basic_enumerable_exercises'

RSpec.describe 'Basic Enumerable Exercises' do

describe 'display current inventory exercise' do

it 'outputs each inventory item' do
expect($stdout).to receive(:puts).with("apples, quantity: 1")
expect($stdout).to receive(:puts).with("bananas, quantity: 3")
expect($stdout).to receive(:puts).with("oranges, quantity: 7")
fruit = { apples: 1, bananas: 3, oranges: 7 }
display_current_inventory(fruit)
end

# remove the 'x' from the line below to unskip the test
it 'outputs item without quantity when value is nil' do
expect($stdout).to receive(:puts).with("pineapples, quantity: ")
fruit = { pineapples: nil }
display_current_inventory(fruit)
end
end

describe 'display guess order exercise' do

it 'outputs each guess of strings in order' do
expect($stdout).to receive(:puts).with("Guess #1 is cookies")
expect($stdout).to receive(:puts).with("Guess #2 is cake")
expect($stdout).to receive(:puts).with("Guess #3 is ice cream")
guesses = ['cookies', 'cake', 'ice cream']
display_guess_order(guesses)
end

it 'outputs each guess of integers in order' do
expect($stdout).to receive(:puts).with("Guess #1 is 553")
expect($stdout).to receive(:puts).with("Guess #2 is 554")
expect($stdout).to receive(:puts).with("Guess #3 is 555")
guesses = [553, 554, 555]
display_guess_order(guesses)
end
end

describe 'find absolute values exercise' do

it 'returns an array of positive integers' do
numbers = [0, -7, 14, -21]
result = [0, 7, 14, 21]
expect(find_absolute_values(numbers)).to eq(result)
end

it 'returns an array of positive floating point numbers' do
numbers = [-3.14, 6.28, -9.42]
result = [3.14, 6.28, 9.42]
expect(find_absolute_values(numbers)).to eq(result)
end
end

describe 'find low inventory exercise' do

it 'returns a hash with integer values' do
fruit = { apples: 1, peaches: 4, bananas: 3, oranges: 7 }
result = { apples: 1, bananas: 3 }
expect(find_low_inventory(fruit)).to eq(result)
end

it 'returns a hash with floating point number values' do
cakes = { chocolate_cake: 2.5, vanilla_cake: 4.25, carrot_cake: 3.75 }
result = { chocolate_cake: 2.5, carrot_cake: 3.75 }
expect(find_low_inventory(cakes)).to eq(result)
end
end

describe 'find word length exercise' do

it 'returns a hash with rocket syntax when using strings' do
animals = ['cat', 'horse', 'rabbit', 'deer']
result = { 'cat' => 3, 'horse' => 5, 'rabbit' => 6, 'deer' => 4 }
expect(find_word_lengths(animals)).to eq(result)
end

it 'returns a hash with symbols syntax when using symbols' do
animals = [:cat, :horse, :rabbit, :deer]
result = { cat: 3, horse: 5, rabbit: 6, deer: 4 }
expect(find_word_lengths(animals)).to eq(result)
end
end
end
18 changes: 18 additions & 0 deletions solutions/ruby_basics/10_basic_enumerables/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups
end

module FormatterOverrides
def dump_pending(_)
end
end

RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides
1 change: 1 addition & 0 deletions solutions/ruby_basics/11_predicate_enumerables/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper --format documentation --color
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def coffee_drink?(drink_list)
# use #include? to return true when the drink_list (array) contains the string "coffee" or "espresso"
drink_list.include?("coffee") || drink_list.include?("espresso")
end

def correct_guess?(guess_list, answer)
# use #any? to return true when any element of the guess_list (array) equals the answer (number)
guess_list.any?(answer)
end

def twenty_first_century_years?(year_list)
# use #all? to return true when all of the years in the year_list (array) are between 2001 and 2100
year_list.all? { |year| year.between?(2001, 2100) }
end

def correct_format?(word_list)
# use #none? to return true when none of the words in the word_list (array) are in upcase
word_list.none? { |word| word == word.upcase }
end

def valid_scores?(score_list, perfect_score)
# use #one? to return true when only one value in the score_list (hash) is equal to the perfect_score (number)
score_list.one? { |_key, value| value == perfect_score }
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require 'spec_helper'
require_relative '../exercises/predicate_enumerable_exercises'

RSpec.describe 'Predicate Enumerable Exercises' do

describe 'coffee drink exercise' do

it 'returns true when coffee is included' do
drink_list = ["coffee", "water", "tea"]
expect(coffee_drink?(drink_list)).to be true
end

# remove the 'x' from the line below to unskip the test
it 'returns true when espresso is included' do
drink_list = ["milk", "juice", "espresso"]
expect(coffee_drink?(drink_list)).to be true
end

it 'returns false when coffee or espresso is not included' do
drink_list = ["tea", "water", "milk"]
expect(coffee_drink?(drink_list)).to be false
end

it 'returns false when the list is empty' do
drink_list = []
expect(coffee_drink?(drink_list)).to be false
end
end

describe 'correct guess exercise' do

it 'returns true when the list contains the answer' do
guess_list = [2, 3, 4, 5]
answer = 5
expect(correct_guess?(guess_list, answer)).to be true
end

it 'returns false when the list does not contain the answer' do
guess_list = [6, 7, 8, 9]
answer = 5
expect(correct_guess?(guess_list, answer)).to be false
end

it 'returns false when the list is empty' do
guess_list = []
answer = 5
expect(correct_guess?(guess_list, answer)).to be false
end
end

describe 'twenty-first century years exercise' do

it 'returns true when all of the years are between 2001 and 2100' do
year_list = [2001, 2002, 2099, 2100]
expect(twenty_first_century_years?(year_list)).to be true
end

it 'returns false when some of the years are not between 2001 and 2100' do
year_list = [2000, 2042, 2084, 2101]
expect(twenty_first_century_years?(year_list)).to be false
end

it 'returns true when the list is empty' do
year_list = []
expect(twenty_first_century_years?(year_list)).to be true
end
end

describe 'correct format exercise' do

it 'returns true when none of the words in the list are in upcase' do
word_list = ["Pepsi", "Coke", "Dr. Pepper"]
expect(correct_format?(word_list)).to be true
end

it 'returns false when at least one word in the list is in upcase' do
word_list = ["PEPSI", "Coke", "Dr. Pepper"]
expect(correct_format?(word_list)).to be false
end

it 'returns true when the list is empty' do
word_list = []
expect(correct_format?(word_list)).to be true
end
end

describe 'valid scores exercise' do

it 'returns true when only one score is a 10' do
score_list = { easy_to_read: 10, uses_best_practices: 8, clever: 7 }
perfect_score = 10
expect(valid_scores?(score_list, perfect_score)).to be true
end

it 'returns false when more than one score is a 10' do
score_list = { easy_to_read: 10, uses_best_practices: 10, clever: 9 }
perfect_score = 10
expect(valid_scores?(score_list, perfect_score)).to be false
end

it 'returns false when the list is empty' do
score_list = {}
perfect_score = 10
expect(valid_scores?(score_list, perfect_score)).to be false
end
end
end
18 changes: 18 additions & 0 deletions solutions/ruby_basics/11_predicate_enumerables/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups
end

module FormatterOverrides
def dump_pending(_)
end
end

RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides
1 change: 1 addition & 0 deletions solutions/ruby_basics/12_nested_collections/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper --format documentation --color
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
def blank_seating_chart(number_of_rows, seats_per_row)
# return a 2d array to represent a seating chart that contains
# number_of_rows nested arrays, each with seats_per_row entries of nil to
# represent that each seat is empty.

# Example: blank_seating_chart(2, 3) should return:
# [
# [nil, nil, nil],
# [nil, nil, nil]
# ]

# NOTE: if one of the nested arrays is changed, the others should **not**
# change with it
Array.new(number_of_rows) { Array.new(seats_per_row) }
end

def add_seat_to_row(chart, row_index, seat_to_add)
# take a chart (2d array) and add seat_to_add to the end of the row that is
# at row_index index of the chart, then return the chart
chart[row_index].push(seat_to_add)
chart
end

def add_another_row(chart, row_to_add)
# take a chart and add row_to_add to the end of the chart,
# then return the chart.
chart << row_to_add
end

def delete_seat_from_row(chart, row_index, seat_index)
# take a chart and delete the seat at seat_index of the row at row_index of
# the chart, then return the chart

# Hint: explore the ruby docs to find a method for deleting from an array!
chart[row_index].delete_at(seat_index)
chart
end

def delete_row_from_chart(chart, row_index)
# take a chart and delete the row at row_index of the chart,
# then return the chart
chart.delete_at(row_index)
chart
end

def count_empty_seats(chart)
# take a chart and return the number of empty (nil) seats in it

# NOTE: `chart` should **not** be mutated
chart.flatten.count(nil)
end

def find_favorite(array_of_hash_objects)
# take an array_of_hash_objects and return the hash which has the key/value
# pair :is_my_favorite? => true. If no hash returns the value true to the key
# :is_my_favorite? it should return nil

# array_of_hash_objects will look something like this:
# [
# { name: 'Ruby', is_my_favorite?: true },
# { name: 'JavaScript', is_my_favorite?: false },
# { name: 'HTML', is_my_favorite?: false }
# ]

# TIP: there will only be a maximum of one hash in the array that will
# return true to the :is_my_favorite? key
array_of_hash_objects.find { |hash| hash[:is_my_favorite?] }
end
Loading