Last active
December 14, 2015 02:18
-
-
Save elle/5012139 to your computer and use it in GitHub Desktop.
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
#! /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