Skip to content

Recursion Exercises: Add total integers exercise #120

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 3 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/3_total_integers/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper --format documentation --color
10 changes: 10 additions & 0 deletions recursion/3_total_integers/exercises/total_integers_exercises.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions recursion/3_total_integers/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
42 changes: 42 additions & 0 deletions recursion/3_total_integers/spec/total_integers_exercises_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions solutions/recursion/3_total_integers/.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,12 @@
def total_integers(array)
array.sum do |element|
case element
when Integer
1
when Array
total_integers(element)
else
0
end
end
end
18 changes: 18 additions & 0 deletions solutions/recursion/3_total_integers/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
Original file line number Diff line number Diff line change
@@ -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