Created
March 24, 2012 10:17
-
-
Save spaghetticode/2180864 to your computer and use it in GitHub Desktop.
Solution to quiz n. 0
This file contains 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
# game 0 | |
class Combinator | |
def initialize(source) | |
@columns = (File.readlines(source) rescue source).map{|line| line.chomp.split(//)} | |
end | |
def combine | |
letters = @columns.shift | |
@columns.empty? ? letters : build_words(letters) | |
end | |
private | |
def build_words(letters) | |
next_col = combine | |
letters.inject([]) do |words, letter| | |
next_col.inject(words) do |words, word| | |
words << letter + word | |
end | |
end | |
end | |
end | |
if ARGV[0] | |
puts Combinator.new(ARGV[0]).combine | |
end | |
require 'rspec' | |
describe Combinator do | |
describe '#combine' do | |
it 'should combine letters in words' do | |
Combinator.new(%w[psc oe s t ae]).combine.should =~ %w[sosta soste posta poste costa coste sesta seste pesta peste cesta ceste] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment