Skip to content

Instantly share code, notes, and snippets.

@elle
Last active December 14, 2015 02:18
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 elle/5012139 to your computer and use it in GitHub Desktop.
Save elle/5012139 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'minitest/autorun'
require 'minitest/spec'
class Letters
WORDS = %w.cat bat bath baths.
attr_accessor :letters
def initialize(letters)
@letters = letters
end
def find_longest
matches.group_by(&:size).max.last[0]
end
def matches
WORDS.select { |word| word if contains_letters?(word) }
end
def contains_letters?(word)
(sorted_word(word) - sorted_letters).empty?
end
def sorted_word(word)
word.chars.to_a.sort
end
def sorted_letters
letters.chars.to_a.sort
end
end
describe Letters do
before { @result = Letters.new('abhtg').find_longest }
it 'finds bath' do
assert_equal 'bath', @result
end
%w.cat bat baths..each do |word|
it "does not find #{word}" do
refute_equal word, @result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment