diff --git a/recursion/2_contains/.rspec b/recursion/2_contains/.rspec new file mode 100644 index 0000000000..279bfd9e99 --- /dev/null +++ b/recursion/2_contains/.rspec @@ -0,0 +1 @@ +--require spec_helper --format documentation --color diff --git a/recursion/2_contains/exercises/contains_exercises.rb b/recursion/2_contains/exercises/contains_exercises.rb new file mode 100644 index 0000000000..e6442bbfde --- /dev/null +++ b/recursion/2_contains/exercises/contains_exercises.rb @@ -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 diff --git a/recursion/2_contains/spec/contains_spec.rb b/recursion/2_contains/spec/contains_spec.rb new file mode 100644 index 0000000000..ec24e4a814 --- /dev/null +++ b/recursion/2_contains/spec/contains_spec.rb @@ -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 diff --git a/recursion/2_contains/spec/spec_helper.rb b/recursion/2_contains/spec/spec_helper.rb new file mode 100644 index 0000000000..71b90cde10 --- /dev/null +++ b/recursion/2_contains/spec/spec_helper.rb @@ -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 diff --git a/solutions/recursion/2_contains/.rspec b/solutions/recursion/2_contains/.rspec new file mode 100644 index 0000000000..279bfd9e99 --- /dev/null +++ b/solutions/recursion/2_contains/.rspec @@ -0,0 +1 @@ +--require spec_helper --format documentation --color diff --git a/solutions/recursion/2_contains/exercises/contains_exercises.rb b/solutions/recursion/2_contains/exercises/contains_exercises.rb new file mode 100644 index 0000000000..b7f6e32a43 --- /dev/null +++ b/solutions/recursion/2_contains/exercises/contains_exercises.rb @@ -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 + diff --git a/solutions/recursion/2_contains/spec/contains_spec.rb b/solutions/recursion/2_contains/spec/contains_spec.rb new file mode 100644 index 0000000000..c154cc9cd1 --- /dev/null +++ b/solutions/recursion/2_contains/spec/contains_spec.rb @@ -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 diff --git a/solutions/recursion/2_contains/spec/spec_helper.rb b/solutions/recursion/2_contains/spec/spec_helper.rb new file mode 100644 index 0000000000..71b90cde10 --- /dev/null +++ b/solutions/recursion/2_contains/spec/spec_helper.rb @@ -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