Sha256: d78e463f03067c8da02a63c5a832fb321c4d0a3bac17d0300737ed9de278fb94

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

class LetterPressIsNotAsGoodAsBoggle
  class WordList
    class Node
      attr_reader :parent

      def initialize(char, parent=nil)
        @char, @parent, @children = char, parent, Hash.new
      end

      def add(chars)
        if chars.empty?
          is_word!
          return
        end
        char = chars.shift
        add_child char, chars
      end

      def child?(char)
        @children.has_key? char
      end

      def child(char)
        @children.fetch char
      end

      def word?
        @is_word
      end

      def to_s
        @parent.to_s << @char
      end

      def words
        words = []
        words << to_s if word?
        @children.each { |char, child| words.concat child.words }
        words
      end

      def inspect
        "#<Dictionary::Node #{to_s}>"
      end

      private

      def add_child(char, chars)
        @children[char] ||= Node.new char, self
        @children[char].add chars
      end

      def is_word!
        @is_word = true
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
letter_press_is_not_as_good_as_boggle-1.0.2 lib/letter_press_is_not_as_good_as_boggle/word_list/node.rb
letter_press_is_not_as_good_as_boggle-1.0.1 lib/letter_press_is_not_as_good_as_boggle/word_list/node.rb
letter_press_is_not_as_good_as_boggle-1.0.0 lib/letter_press_is_not_as_good_as_boggle/word_list/node.rb