Skip to content

Instantly share code, notes, and snippets.

@spaghetticode
Created March 24, 2012 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spaghetticode/2180864 to your computer and use it in GitHub Desktop.
Save spaghetticode/2180864 to your computer and use it in GitHub Desktop.
Solution to quiz n. 0
# 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