Skip to content

Recursion Exercise: Add #contains? exercise #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 recursion/2_contains/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper --format documentation --color
8 changes: 8 additions & 0 deletions recursion/2_contains/exercises/contains_exercises.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def contains?(hash, search_value)
# Write a method that recursively searches for a value in a nested hash.
# It should return `true` if the object contains that value.
#
# Examples:
# contains?({ foo: { bar: "baz" } }, "baz") # true
# contains?({ foo: { bar: "baz" } }, "egg") # false
end
48 changes: 48 additions & 0 deletions recursion/2_contains/spec/contains_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require_relative "../exercises/contains_exercises"

describe "#contains?" do
let(:nested_hash) do
{
data: {
duplicate: "e",
stuff: {
thing: {
banana: "banana",
more_stuff: {
something: "foo"
}
}
},
info: {
duplicate: "e",
magic_number: 44,
empty: nil
}
}
}
end

it "returns true if the provided number is a value in the hash" do
present_number = 44
expect(contains?(nested_hash, present_number)).to be true
end

xit "returns true if the provided string is a value in the hash" do
present_string = "foo"
expect(contains?(nested_hash, present_string)).to be true
end

xit "returns false when the provided string is not a value in the hash" do
absent_string = "bar"
expect(contains?(nested_hash, absent_string)).to be false
end

xit "returns true when looking for a value that is present multiple times" do
duplicated_value = "e"
expect(contains?(nested_hash, duplicated_value)).to be true
end

xit "returns true when the provided value is present and is `nil`" do
expect(contains?(nested_hash, nil)).to be true
end
end
18 changes: 18 additions & 0 deletions recursion/2_contains/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/recursion/2_contains/.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,8 @@
def contains?(hash, search_value)
values = hash.values
return true if values.include?(search_value)

nested_hashes = values.select { |value| value.is_a?(Hash) }
nested_hashes.any? { |nested_hash| contains?(nested_hash, search_value) }
end

48 changes: 48 additions & 0 deletions solutions/recursion/2_contains/spec/contains_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require_relative "../exercises/contains_exercises"

describe "#contains?" do
let(:nested_hash) do
{
data: {
duplicate: "e",
stuff: {
thing: {
banana: "banana",
more_stuff: {
something: "foo"
}
}
},
info: {
duplicate: "e",
magic_number: 44,
empty: nil
}
}
}
end

it "returns true if the provided number is a value in the hash" do
present_number = 44
expect(contains?(nested_hash, present_number)).to be true
end

it "returns true if the provided string is a value in the hash" do
present_string = "foo"
expect(contains?(nested_hash, present_string)).to be true
end

it "returns false when the provided string is not a value in the hash" do
absent_string = "bar"
expect(contains?(nested_hash, absent_string)).to be false
end

it "returns true when looking for a value that is present multiple times" do
duplicated_value = "e"
expect(contains?(nested_hash, duplicated_value)).to be true
end

it "returns true when the provided value is present and is `nil`" do
expect(contains?(nested_hash, nil)).to be true
end
end
18 changes: 18 additions & 0 deletions solutions/recursion/2_contains/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