Class: AnagramSolver::Anagram
- Inherits:
-
Object
- Object
- AnagramSolver::Anagram
- Defined in:
- lib/anagram_solver/anagram.rb
Instance Method Summary collapse
-
#initialize ⇒ Anagram
constructor
A new instance of Anagram.
- #load_dictionary ⇒ Object
- #solve(target_word) ⇒ Object
Constructor Details
#initialize ⇒ Anagram
Returns a new instance of Anagram.
3 4 5 6 |
# File 'lib/anagram_solver/anagram.rb', line 3 def initialize # loading the dictionary @word_list = load_dictionary end |
Instance Method Details
#load_dictionary ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/anagram_solver/anagram.rb', line 8 def load_dictionary wl = [] file = File.open("/usr/share/dict/words", 'r') file.each_line do |line| wl.push(line.chomp) end wl end |
#solve(target_word) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/anagram_solver/anagram.rb', line 17 def solve target_word valid_words = [] letters = target_word.downcase.split(//) no_of_letters = letters.length @word_list.each do |word| next unless word.length == no_of_letters word_copy = word word = word.split(//) letters.each do |letter| if word.include? letter word.delete_at word.index(letter) end end if word.length == 0 valid_words.push(word_copy) end end valid_words end |