Created
November 24, 2017 15:31
-
-
Save DonSchado/3c92fcaf4c58943f68f4a33536c7548b to your computer and use it in GitHub Desktop.
Railslove Ruby programming exercise A2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Railslove Ruby programming exercise A2 | |
# | |
# To run the following exercise you need to have rspec installed. | |
# Then inspect the failing specs by running `rspec subset.rb` | |
# | |
# The task is to get all possible subsets of a group of Railslovers (without duplicates). | |
# So return a collection containing a group of each Railslover alone, | |
# then a group of pairs, then groups of 3, etc... | |
# The empty group is not considered a valid Railslove group. :) | |
# | |
# Write code in the body of the subsets method to make the tests pass. | |
# | |
# Refactor until you're happy with your solution, by asking the follwing questions: | |
# * Is the code understandable/maintainable? | |
# * Do I really need all these if/else/elsif statements? | |
# | |
require 'ostruct' | |
class Collection < OpenStruct | |
def subsets | |
# | |
# your code goes here | |
# | |
railslovers | |
end | |
end | |
# don't touch me | |
require 'rspec' | |
RSpec.describe 'Collection' do | |
let(:railslovers) { %w(Bumi Jan Tim Lars) } | |
subject(:collection) { Collection.new(railslovers: railslovers) } | |
describe '#subsets' do | |
it 'returns not the initial collection' do | |
expect(subject.subsets).not_to eq(railslovers) | |
end | |
it 'is already a big collection for four railslovers' do | |
expect(subject.subsets.size).to eq(15) | |
end | |
it 'contains groups of 1' do | |
expect(subject.subsets).to include(['Bumi']) | |
expect(subject.subsets).to include(['Jan']) | |
expect(subject.subsets).to include(['Tim']) | |
expect(subject.subsets).to include(['Lars']) | |
end | |
it 'contains groups of 2' do | |
expect(subject.subsets).to include(['Bumi', 'Tim']) | |
expect(subject.subsets).to include(['Bumi', 'Jan']) | |
expect(subject.subsets).to include(['Jan', 'Tim']) | |
expect(subject.subsets).to include(['Jan', 'Lars']) | |
end | |
it 'contains groups of 3' do | |
expect(subject.subsets).to include(['Jan', 'Tim', 'Lars']) | |
expect(subject.subsets).to include(['Bumi', 'Tim', 'Lars']) | |
end | |
it 'contains groups of 4' do | |
expect(subject.subsets).to include(railslovers) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment