From 3b9878afdd80c769c8639af6b47c47d2efae8d5b Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Thu, 3 Jul 2025 19:29:49 -0600 Subject: [PATCH 1/3] Add total integers exercise --- recursion/3_total_integers/.rspec | 1 + .../exercises/total_integers_exercises.rb | 6 ++++ .../3_total_integers/spec/spec_helper.rb | 18 ++++++++++ .../spec/total_integers_exercises_spec.rb | 34 +++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 recursion/3_total_integers/.rspec create mode 100644 recursion/3_total_integers/exercises/total_integers_exercises.rb create mode 100644 recursion/3_total_integers/spec/spec_helper.rb create mode 100644 recursion/3_total_integers/spec/total_integers_exercises_spec.rb 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..4d12d70982 --- /dev/null +++ b/recursion/3_total_integers/exercises/total_integers_exercises.rb @@ -0,0 +1,6 @@ +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` +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..dfe570990c --- /dev/null +++ b/recursion/3_total_integers/spec/total_integers_exercises_spec.rb @@ -0,0 +1,34 @@ +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 +end From 6b9aa75e06e86c231c3b4131cd79688f0c06bbd2 Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Sat, 5 Jul 2025 19:32:58 -0600 Subject: [PATCH 2/3] Add solution --- solutions/recursion/3_total_integers/.rspec | 1 + .../exercises/total_integers_exercises.rb | 12 +++++++ .../3_total_integers/spec/spec_helper.rb | 18 ++++++++++ .../spec/total_integers_exercises_spec.rb | 34 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 solutions/recursion/3_total_integers/.rspec create mode 100644 solutions/recursion/3_total_integers/exercises/total_integers_exercises.rb create mode 100644 solutions/recursion/3_total_integers/spec/spec_helper.rb create mode 100644 solutions/recursion/3_total_integers/spec/total_integers_exercises_spec.rb 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..7489926dc5 --- /dev/null +++ b/solutions/recursion/3_total_integers/spec/total_integers_exercises_spec.rb @@ -0,0 +1,34 @@ +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 +end From f915929954ca2190077a5dd9a056b6ada4ee91f9 Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Sun, 6 Jul 2025 13:26:42 -0600 Subject: [PATCH 3/3] Add spec to make sure `#flatten` isn't used --- .../exercises/total_integers_exercises.rb | 4 ++++ .../spec/total_integers_exercises_spec.rb | 8 ++++++++ .../spec/total_integers_exercises_spec.rb | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/recursion/3_total_integers/exercises/total_integers_exercises.rb b/recursion/3_total_integers/exercises/total_integers_exercises.rb index 4d12d70982..b247cd4e1e 100644 --- a/recursion/3_total_integers/exercises/total_integers_exercises.rb +++ b/recursion/3_total_integers/exercises/total_integers_exercises.rb @@ -3,4 +3,8 @@ def total_integers(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/total_integers_exercises_spec.rb b/recursion/3_total_integers/spec/total_integers_exercises_spec.rb index dfe570990c..147029740c 100644 --- a/recursion/3_total_integers/spec/total_integers_exercises_spec.rb +++ b/recursion/3_total_integers/spec/total_integers_exercises_spec.rb @@ -31,4 +31,12 @@ 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/spec/total_integers_exercises_spec.rb b/solutions/recursion/3_total_integers/spec/total_integers_exercises_spec.rb index 7489926dc5..99434cfd8a 100644 --- a/solutions/recursion/3_total_integers/spec/total_integers_exercises_spec.rb +++ b/solutions/recursion/3_total_integers/spec/total_integers_exercises_spec.rb @@ -31,4 +31,12 @@ 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