Sha256: ca8be0418bf1fca754cf0deb7a34f3016f95669758d224dfe4a581369ecb9371

Contents?: true

Size: 651 Bytes

Versions: 239

Compression:

Stored size: 651 Bytes

Contents

module BookKeeping
  VERSION = 2
end

class Anagram
  attr_reader :subject
  def initialize(word)
    @subject = AnagramSubject.new(word)
  end

  def match(candidates)
    candidates.select do |candidate|
      subject.anagram_of? candidate
    end
  end
end

class AnagramSubject
  attr_reader :subject
  def initialize(subject)
    @subject = subject
  end

  def anagram_of?(word)
    !duplicate?(word) && fingerprint == canonicalize(word)
  end

  def duplicate?(word)
    word.downcase == subject.downcase
  end

  def canonicalize(word)
    word.downcase.chars.sort
  end

  def fingerprint
    @fingerprint ||= canonicalize(subject)
  end
end

Version data entries

239 entries across 239 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.179 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.178 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.177 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.176 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.175 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.174 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.173 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.172 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.171 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.170 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.169 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.167 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.166 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.165 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.164 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.163 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.162 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.161 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb
trackler-2.2.1.160 tracks/ruby/exercises/anagram/.meta/solutions/anagram.rb