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/4_permutations/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper --format documentation --color
16 changes: 16 additions & 0 deletions recursion/4_permutations/exercises/permutation_exercises.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def permutations(array)
# Write a method that takes in an array of integers and returns an array of
# all possible permutations of the original array. The permutations of a set
# are the different ways the elements can be arranged.
#
# For simplicity, the integers are guaranteed to not repeat.
#
# Examples:
# `permutations([1, 2, 3])` has six different permutations (or ways the elements can be arranged)
# it should return `[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]`
#
# `permutations([])` returns `[[]]`, as there's only one arrangement of an empty set
#
# NOTE: the tests do not check for ordering, so a return of `[[1, 2], [2, 1]]`
# will be treated the same as `[[2, 1], [1, 2]]`
end
34 changes: 34 additions & 0 deletions recursion/4_permutations/spec/permutation_exercises_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'
require_relative '../exercises/permutation_exercises'

RSpec.describe '#permutations' do
it "returns 1 possible permutation for a set containing 0 numbers" do
expect(permutations([])).to eq [[]]
end

xit "returns 2 possible permutations for a set containing 2 numbers" do
expect(permutations([1, 2])).to match_array([[2, 1], [1, 2]])
end

xit "returns 6 possible permutations for a set containing 3 numbers" do
expected_set = [
[1, 2, 3], [1, 3, 2], [2, 1, 3],
[2, 3, 1], [3, 1, 2], [3, 2, 1]
]

expect(permutations([1, 2, 3])).to match_array(expected_set)
end

xit "returns 24 possible permutations for a set containing 4 numbers" do
expected_set = [
[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2],
[1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3],
[2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1],
[3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1],
[3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2],
[4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]
]

expect(permutations([1, 2, 3, 4])).to match_array(expected_set)
end
end
18 changes: 18 additions & 0 deletions recursion/4_permutations/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/4_permutations/.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,11 @@
def permutations(array, index = 0, results = [])
results << array.dup if index == array.length

(index...array.length).each do |i|
array[index], array[i] = array[i], array[index]
permutations(array, index + 1, results)
array[index], array[i] = array[i], array[index]
end

results
end
34 changes: 34 additions & 0 deletions solutions/recursion/4_permutations/spec/permutations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'
require_relative '../exercises/permutation_exercises'

RSpec.describe '#permutations' do
it "returns 1 possible permutation for a set containing 0 numbers" do
expect(permutations([])).to eq [[]]
end

it "returns 2 possible permutations for a set containing 2 numbers" do
expect(permutations([1, 2])).to match_array([[2, 1], [1, 2]])
end

it "returns 6 possible permutations for a set containing 3 numbers" do
expected_set = [
[1, 2, 3], [1, 3, 2], [2, 1, 3],
[2, 3, 1], [3, 1, 2], [3, 2, 1]
]

expect(permutations([1, 2, 3])).to match_array(expected_set)
end

it "returns 24 possible permutations for a set containing 4 numbers" do
expected_set = [
[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2],
[1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3],
[2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1],
[3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1],
[3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2],
[4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]
]

expect(permutations([1, 2, 3, 4])).to match_array(expected_set)
end
end
18 changes: 18 additions & 0 deletions solutions/recursion/4_permutations/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