Skip to content
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/5_pascal/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper --format documentation --color
18 changes: 18 additions & 0 deletions recursion/5_pascal/exercises/pascal_exercises.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def pascal(row_number)
# Pascal's triangle is modeled as follows:
# - The first row is `1`.
# - Each row can be considered to have a hidden `0` to either sides of it. So the first row could also be said to be `0, 1, 0`
# - To obtain the next row, we take each number and add it with its rightmost neighbor.
#
# First row: `[1]`
# Second row: `[0+1, 1+0]` or simply `[1, 1]`
# Third row: `[0+1, 1+1, 1+0]` or simply `[1, 2, 1]`
# Fourth row: `[0+1, 1+2, 2+1, 1+0]` or simply `[1, 3, 3, 1]`
#
#
# The pattern continues forever.
#
# Your task is to create a *recursive* function, `pascal` - that will take an input `n` and output the `n`th pascal's row as an array of numbers.
#
# For example, `pascal(3)` should return `[1, 2, 1]`.
end
32 changes: 32 additions & 0 deletions recursion/5_pascal/spec/pascal_exercises_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'
require_relative '../exercises/pascal_exercises'

RSpec.describe '#pascal' do
it 'gets the first row of the pascal triangle' do
expect(pascal(1)).to eq([1])
end

xit 'gets the second row of the pascal triangle' do
expect(pascal(2)).to eq([1, 1])
end

xit 'gets the third row of the pascal triangle' do
expect(pascal(3)).to eq([1, 2, 1])
end

xit 'gets the fourth row of the pascal triangle' do
expect(pascal(4)).to eq([1, 3, 3, 1])
end

xit 'gets the fifth row of the pascal triangle' do
expect(pascal(5)).to eq([1, 4, 6, 4, 1])
end

xit 'gets the sixth row of the pascal triangle' do
expect(pascal(6)).to eq([1, 5, 10, 10, 5, 1])
end

xit 'gets the seventh row of the pascal triangle' do
expect(pascal(7)).to eq([1, 6, 15, 20, 15, 6, 1])
end
end
18 changes: 18 additions & 0 deletions recursion/5_pascal/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/5_pascal/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper --format documentation --color
9 changes: 9 additions & 0 deletions solutions/recursion/5_pascal/exercises/pascal_exercises.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def pascal(row_number)
current_line = [1]
return current_line if row_number == 1

previous_line = pascal(row_number - 1) + [0]
previous_line.each_cons(2) { |a, b| current_line << a + b }

current_line
end
32 changes: 32 additions & 0 deletions solutions/recursion/5_pascal/spec/pascal_exercises_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'
require_relative '../exercises/pascal_exercises'

RSpec.describe '#pascal' do
it 'gets the first row of the pascal triangle' do
expect(pascal(1)).to eq([1])
end

it 'gets the second row of the pascal triangle' do
expect(pascal(2)).to eq([1, 1])
end

it 'gets the third row of the pascal triangle' do
expect(pascal(3)).to eq([1, 2, 1])
end

it 'gets the fourth row of the pascal triangle' do
expect(pascal(4)).to eq([1, 3, 3, 1])
end

it 'gets the fifth row of the pascal triangle' do
expect(pascal(5)).to eq([1, 4, 6, 4, 1])
end

it 'gets the sixth row of the pascal triangle' do
expect(pascal(6)).to eq([1, 5, 10, 10, 5, 1])
end

it 'gets the seventh row of the pascal triangle' do
expect(pascal(7)).to eq([1, 6, 15, 20, 15, 6, 1])
end
end
18 changes: 18 additions & 0 deletions solutions/recursion/5_pascal/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