diff --git a/recursion/3_total_integers/.rspec b/recursion/3_total_integers/.rspec new file mode 100644 index 0000000000..279bfd9e99 --- /dev/null +++ b/recursion/3_total_integers/.rspec @@ -0,0 +1 @@ +--require spec_helper --format documentation --color diff --git a/recursion/3_total_integers/exercises/total_integers_exercises.rb b/recursion/3_total_integers/exercises/total_integers_exercises.rb new file mode 100644 index 0000000000..b247cd4e1e --- /dev/null +++ b/recursion/3_total_integers/exercises/total_integers_exercises.rb @@ -0,0 +1,10 @@ +def total_integers(array) + # Count the total number of integers inside of the given array + # The array may be nested, and the integers inside these "inner" layers must also be counted + # + # Example: `total_integers([0, 1, [5]]) == 3` + # + # NOTE: you may notice that `Array#flatten` would make quick work of this, + # but you should implement this method without using it. The tests will check + # to make sure `#flatten` isn't used. +end diff --git a/recursion/3_total_integers/spec/spec_helper.rb b/recursion/3_total_integers/spec/spec_helper.rb new file mode 100644 index 0000000000..71b90cde10 --- /dev/null +++ b/recursion/3_total_integers/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/recursion/3_total_integers/spec/total_integers_exercises_spec.rb b/recursion/3_total_integers/spec/total_integers_exercises_spec.rb new file mode 100644 index 0000000000..147029740c --- /dev/null +++ b/recursion/3_total_integers/spec/total_integers_exercises_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' +require_relative '../exercises/total_integers_exercises' + +RSpec.describe '#total_integers' do + it 'returns 3 with a 3 integer array' do + three_int_array = [1, 2, 3] + expect(total_integers(three_int_array)).to eq 3 + end + + xit 'ignores non integer values' do + array_with_string = [1, 2, '3'] + expect(total_integers(array_with_string)).to eq 2 + end + + xit 'ignores floating point numbers' do + float_array = [1.0, 2.5, 0.7] + expect(total_integers(float_array)).to eq 0 + end + + xit 'returns 0 with an empty nested array' do + empty_nested_array = [[], [], []] + expect(total_integers(empty_nested_array)).to eq 0 + end + + xit 'returns 2 with a deeply nested two integer array' do + deeply_nested_array = [[[[[[[[[[[[[[4]]]]]], 246]]]]]]]] + expect(total_integers(deeply_nested_array)).to eq 2 + end + + xit 'returns 3 with a complex, deeply nested three integer array' do + complex_array = [{}, [555], '444', [nil, 74.0, [4]], [[6]]] + expect(total_integers(complex_array)).to eq 3 + end + + xit "does not call `Array#flatten`" do + three_int_array = [1, [2, 3]] + allow(three_int_array).to receive(:flatten).and_return [1, 2, 3] + + total_integers(three_int_array) + expect(three_int_array).not_to have_received(:flatten) + end +end diff --git a/solutions/recursion/3_total_integers/.rspec b/solutions/recursion/3_total_integers/.rspec new file mode 100644 index 0000000000..279bfd9e99 --- /dev/null +++ b/solutions/recursion/3_total_integers/.rspec @@ -0,0 +1 @@ +--require spec_helper --format documentation --color diff --git a/solutions/recursion/3_total_integers/exercises/total_integers_exercises.rb b/solutions/recursion/3_total_integers/exercises/total_integers_exercises.rb new file mode 100644 index 0000000000..acce0ff7b6 --- /dev/null +++ b/solutions/recursion/3_total_integers/exercises/total_integers_exercises.rb @@ -0,0 +1,12 @@ +def total_integers(array) + array.sum do |element| + case element + when Integer + 1 + when Array + total_integers(element) + else + 0 + end + end +end diff --git a/solutions/recursion/3_total_integers/spec/spec_helper.rb b/solutions/recursion/3_total_integers/spec/spec_helper.rb new file mode 100644 index 0000000000..71b90cde10 --- /dev/null +++ b/solutions/recursion/3_total_integers/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/3_total_integers/spec/total_integers_exercises_spec.rb b/solutions/recursion/3_total_integers/spec/total_integers_exercises_spec.rb new file mode 100644 index 0000000000..99434cfd8a --- /dev/null +++ b/solutions/recursion/3_total_integers/spec/total_integers_exercises_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' +require_relative '../exercises/total_integers_exercises' + +RSpec.describe '#total_integers' do + it 'returns 3 with a 3 integer array' do + three_int_array = [1, 2, 3] + expect(total_integers(three_int_array)).to eq 3 + end + + it 'ignores non integer values' do + array_with_string = [1, 2, '3'] + expect(total_integers(array_with_string)).to eq 2 + end + + it 'ignores floating point numbers' do + float_array = [1.0, 2.5, 0.7] + expect(total_integers(float_array)).to eq 0 + end + + it 'returns 0 with an empty nested array' do + empty_nested_array = [[], [], []] + expect(total_integers(empty_nested_array)).to eq 0 + end + + it 'returns 2 with a deeply nested two integer array' do + deeply_nested_array = [[[[[[[[[[[[[[4]]]]]], 246]]]]]]]] + expect(total_integers(deeply_nested_array)).to eq 2 + end + + it 'returns 3 with a complex, deeply nested three integer array' do + complex_array = [{}, [555], '444', [nil, 74.0, [4]], [[6]]] + expect(total_integers(complex_array)).to eq 3 + end + + it "does not call `Array#flatten`" do + three_int_array = [1, [2, 3]] + allow(three_int_array).to receive(:flatten).and_return [1, 2, 3] + + total_integers(three_int_array) + expect(three_int_array).not_to have_received(:flatten) + end +end