Class: JumbleSolver::Jumble
- Inherits:
-
Object
- Object
- JumbleSolver::Jumble
- Defined in:
- lib/jumble_solver/jumble.rb
Instance Method Summary collapse
-
#initialize ⇒ Jumble
constructor
A new instance of Jumble.
- #load_dictionary ⇒ Object
- #solve(letters) ⇒ Object
Constructor Details
#initialize ⇒ Jumble
Returns a new instance of Jumble.
3 4 5 6 |
# File 'lib/jumble_solver/jumble.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/jumble_solver/jumble.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(letters) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/jumble_solver/jumble.rb', line 17 def solve letters valid_words = [] letters = letters.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 |