From 15ed345151d881363e3cf9ca7ded4b20bbf28920 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Mon, 14 Sep 2020 17:22:04 -0700 Subject: [PATCH 1/6] define draw letters method --- lib/adagrams.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..0457bf7 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,42 @@ +# makes an array of all possible letters + + +def draw_letters + # p letter_pool + letter_pool = [] + letter_pool_hash = { + A: 9, + B: 2, + C: 2, + D: 4, + E: 12, + F: 2, + G: 3, + H: 2, + I: 9, + J: 1, + K: 1, + L: 4, + M: 2, + N: 6, + O: 8, + P: 2, + Q: 1, + R: 6, + S: 4, + T: 6, + U: 4, + V: 2, + W: 2, + X: 1, + Y: 2, + Z: 1 + } + + letter_pool_hash.each do |letter, frequency| + frequency.times { letter_pool << letter.to_s } + end + + random_letter = letter_pool.shuffle.take(10) + return random_letter +end From 7574ddc812e04fe22cf2a30eee059b9c79438c52 Mon Sep 17 00:00:00 2001 From: Ringo Date: Mon, 14 Sep 2020 21:38:46 -0700 Subject: [PATCH 2/6] define uses_available_letters? method --- lib/adagrams.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 0457bf7..43123ff 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -40,3 +40,15 @@ def draw_letters random_letter = letter_pool.shuffle.take(10) return random_letter end + +def uses_available_letters?(input, letters_in_hand) + letters_in_hand_copy = letters_in_hand.clone + input.each_char do |letter| + if letters_in_hand_copy.include?(letter) + letters_in_hand_copy.delete(letter) + else + return false + end + end + return true +end \ No newline at end of file From 552421fc3d15969210d1524d049802d6ecf1f4f9 Mon Sep 17 00:00:00 2001 From: Ringo Date: Tue, 15 Sep 2020 14:37:22 -0700 Subject: [PATCH 3/6] define score_word method --- lib/adagrams.rb | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 43123ff..74f1c39 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -51,4 +51,31 @@ def uses_available_letters?(input, letters_in_hand) end end return true -end \ No newline at end of file +end + +def score_word(word) + score = 0 + word.upcase.each_char do |charater| + case charater + when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" + score += 1 + when "D", "G" + score += 2 + when "B", "C", "M", "P" + score += 3 + when "F", "H", "V", "W", "Y" + score += 4 + when "K" + score += 5 + when "J", "X" + score += 8 + when "Q", "Z" + score += 10 + end + end + + if word.length >= 7 + score += 8 + end + return score +end From c7987ed667412e146c849e2f070f665c8eff9cc2 Mon Sep 17 00:00:00 2001 From: Ringo Date: Tue, 15 Sep 2020 15:58:29 -0700 Subject: [PATCH 4/6] define highest_score_from method --- lib/adagrams.rb | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 74f1c39..071ce38 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -55,8 +55,8 @@ def uses_available_letters?(input, letters_in_hand) def score_word(word) score = 0 - word.upcase.each_char do |charater| - case charater + word.upcase.each_char do |character| + case character when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" score += 1 when "D", "G" @@ -77,5 +77,33 @@ def score_word(word) if word.length >= 7 score += 8 end + return score end + +def highest_score_from(words) + winner = { + word: "", + score: 0 + } + + high_score = words.max_by { |word| score_word(word) } + high_words = words.select { |word| score_word(word) == score_word(high_score) } + # high_words.sort_by! { |word| word.length } + + if high_words.length == 1 + winner[:word] = high_words[0] + winner[:score] = score_word(high_words[0]) + elsif high_words.any? { |word| word.length == 10 } + ten_letter_words = high_words.select { |word| word if word.length == 10 } + winner[:word] = ten_letter_words[0] + winner[:score] = score_word(ten_letter_words[0]) + else + shortest_word_length = (high_words.min_by { |word| word.length }).length + short_words_array = high_words.select { |word| word.length == shortest_word_length } + winner[:word] = short_words_array[0] + winner[:score] = score_word(short_words_array[0]) + end + + return winner +end \ No newline at end of file From 1a75eef48fe3d65cdd95f6938e26b6c92a23466f Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Wed, 16 Sep 2020 17:21:03 -0700 Subject: [PATCH 5/6] define is_in_english_dict? method --- lib/adagrams.rb | 49 +++++++++---------------------------------------- 1 file changed, 9 insertions(+), 40 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 071ce38..b74bf40 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,51 +1,16 @@ -# makes an array of all possible letters - +require 'csv' def draw_letters - # p letter_pool - letter_pool = [] - letter_pool_hash = { - A: 9, - B: 2, - C: 2, - D: 4, - E: 12, - F: 2, - G: 3, - H: 2, - I: 9, - J: 1, - K: 1, - L: 4, - M: 2, - N: 6, - O: 8, - P: 2, - Q: 1, - R: 6, - S: 4, - T: 6, - U: 4, - V: 2, - W: 2, - X: 1, - Y: 2, - Z: 1 - } - - letter_pool_hash.each do |letter, frequency| - frequency.times { letter_pool << letter.to_s } - end - + letter_pool = %W( #{ "A" * 9 + "B" * 2 + "C" * 2 + "D" * 4 + "E" * 12 + "F" * 2 + "G" * 3 + "H" * 2 + "I" * 9 + "J" * 1 + "K" * 1 + "L" * 4 + "M" * 2 + "N" * 6 + "O" * 8 + "P" * 2 + "Q" * 1 + "R" * 6 + "S" * 4 + "T" * 6 + "U" * 4 + "V" * 2 + "W" * 2 + "X" * 1 + "Y" * 2 + "Z" * 1} ).join.split("") random_letter = letter_pool.shuffle.take(10) return random_letter end def uses_available_letters?(input, letters_in_hand) letters_in_hand_copy = letters_in_hand.clone - input.each_char do |letter| + input.upcase.each_char do |letter| if letters_in_hand_copy.include?(letter) - letters_in_hand_copy.delete(letter) + letters_in_hand_copy.delete_at(letters_in_hand_copy.index(letter)) else return false end @@ -89,7 +54,6 @@ def highest_score_from(words) high_score = words.max_by { |word| score_word(word) } high_words = words.select { |word| score_word(word) == score_word(high_score) } - # high_words.sort_by! { |word| word.length } if high_words.length == 1 winner[:word] = high_words[0] @@ -106,4 +70,9 @@ def highest_score_from(words) end return winner +end + +def is_in_english_dict?(input) + dictionary = CSV.read('../adagrams/assets/dictionary-english.csv').flatten + return dictionary.include?(input.downcase) end \ No newline at end of file From 0429e43103fa62452493bad87652853ea470198d Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Thu, 17 Sep 2020 16:53:51 -0700 Subject: [PATCH 6/6] add wave5 test --- test/adagrams_test.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/adagrams_test.rb b/test/adagrams_test.rb index 90ec44d..1814309 100644 --- a/test/adagrams_test.rb +++ b/test/adagrams_test.rb @@ -178,4 +178,28 @@ expect(best_word[:score]).must_equal 18 end end + + describe "is_in_english_dict?" do + it "finds real word in dictionary" do + good_word = "bamboo" + expect(is_in_english_dict?(good_word)).must_equal true + end + + it "doesn't find fake word in dictionary" do + bad_word = "zybje" + expect(is_in_english_dict?(bad_word)).wont_equal true + + empty_word = "" + expect(is_in_english_dict?(empty_word)).wont_equal true + + too_long_word = "incongruous" + expect(is_in_english_dict?(too_long_word)).wont_equal true + end + end + end + + + + +