From 1453ccad4f1ea32b25e7dc1623edb14023862f53 Mon Sep 17 00:00:00 2001 From: Diana Han Date: Mon, 12 Aug 2019 16:29:17 -0700 Subject: [PATCH 1/9] implement wave 1 --- lib/adagrams.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..1fc1030 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,10 @@ +LETTER_POOL = {A: 9, N: 6, B: 2, O: 8, C: 2, P: 2, D: 4, Q: 1, E: 12, R: 6, F: 2, S: 4, G: 3, T: 6, H: 2, U: 4, I: 9, V: 2, J: 1, W: 2, K: 1, X: 1, L: 4, Y: 2, M: 2, Z: 1} + + +def draw_letters + letters = LETTER_POOL.map do |letter, count| + Array.new(count, letter.to_s) + end + + return letters.flatten.sample(10) +end \ No newline at end of file From 90679eaf30ae8a27fb8c5cdf50aaac0bd8492f21 Mon Sep 17 00:00:00 2001 From: Diana Han Date: Tue, 13 Aug 2019 16:59:55 -0700 Subject: [PATCH 2/9] wave 2 --- lib/adagrams.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 1fc1030..52b139c 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -7,4 +7,17 @@ def draw_letters end return letters.flatten.sample(10) -end \ No newline at end of file +end + +# method may change stings in letters_in_hand +def uses_available_letters?(input, letters_in_hand) + input.each_char do |c| + index = letters_in_hand.find_index(c) + if index.nil? + return false + else + letters_in_hand.delete_at(index) + end + end +return true +end From 6af72383bb4626f0eaf4698b6b20ea640e192f48 Mon Sep 17 00:00:00 2001 From: Diana Han Date: Tue, 13 Aug 2019 17:02:11 -0700 Subject: [PATCH 3/9] wave 3 --- lib/adagrams.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 52b139c..fdb896a 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -21,3 +21,51 @@ def uses_available_letters?(input, letters_in_hand) end return true end + +SCORE_CHART = [ + { + letters: %w(A E I O U L N R S T), + value: 1 + }, + { + letters: %w(D G), + value: 2 + }, + { + letters: %w(B C M P), + value: 3 + }, + { + letters: %w(F H V W Y), + value: 4 + }, + { + letters: %w(K), + value: 5 + }, + { + letters: %w(J K), + value: 8 + }, + { + letters: %w(Q Z), + value: 10 + } +] + +# refactor! +def score_word(word) + total_points = 0 + word.each_char do |c| + SCORE_CHART.each do |score| + if score[:letters].include?(c) + total_points += score[:value] + end + end + end + if word.length > 6 + total_points += 8 + end + return total_points +end + From d959a80f05b93b516de938757ae4f47fac7da6d1 Mon Sep 17 00:00:00 2001 From: Diana Han Date: Wed, 14 Aug 2019 11:34:54 -0700 Subject: [PATCH 4/9] wave 4 in process --- lib/adagrams.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index fdb896a..2545df1 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -9,7 +9,7 @@ def draw_letters return letters.flatten.sample(10) end -# method may change stings in letters_in_hand +# this method may change strings in letters_in_hand def uses_available_letters?(input, letters_in_hand) input.each_char do |c| index = letters_in_hand.find_index(c) @@ -69,3 +69,26 @@ def score_word(word) return total_points end +p score_word(input) +words = ["cat", "elephant"] + +# wave 4 +def highest_score_from(words) + winning_word = { word: nil, score: 0} + words.each do |word| + score = score_word(word) #the score of current word + if score > winning_word[:score] + winning_word[:score] = score + winning_word[:word] = word + elsif score == winning_word[:score] + if word.length > winning_word[:word].length + next + end + end + end + return winning_word +end + + + +p highest_score_from(words) From eac4d737881728978129ff218b83f70ea385fa93 Mon Sep 17 00:00:00 2001 From: Mira Date: Wed, 14 Aug 2019 11:40:10 -0700 Subject: [PATCH 5/9] clear method calls from adagrams.rb so tests will run --- lib/adagrams.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 2545df1..940799a 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -69,9 +69,6 @@ def score_word(word) return total_points end -p score_word(input) -words = ["cat", "elephant"] - # wave 4 def highest_score_from(words) winning_word = { word: nil, score: 0} @@ -89,6 +86,3 @@ def highest_score_from(words) return winning_word end - - -p highest_score_from(words) From 7d0649b3800bc498296e9edb6fb3968b804a8c66 Mon Sep 17 00:00:00 2001 From: Mira Date: Wed, 14 Aug 2019 14:44:25 -0700 Subject: [PATCH 6/9] wave 4 and some formatting --- lib/adagrams.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 940799a..bedf66f 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,4 +1,4 @@ -LETTER_POOL = {A: 9, N: 6, B: 2, O: 8, C: 2, P: 2, D: 4, Q: 1, E: 12, R: 6, F: 2, S: 4, G: 3, T: 6, H: 2, U: 4, I: 9, V: 2, J: 1, W: 2, K: 1, X: 1, L: 4, Y: 2, M: 2, Z: 1} +LETTER_POOL = {A: 9, N: 6, B: 2, O: 8, C: 2, P: 2, D: 4, Q: 1, E: 12, R: 6, F: 2, S: 4, G: 3, T: 6, H: 2, U: 4, I: 9, V: 2, J: 1, W: 2, K: 1, X: 1, L: 4, Y: 2, M: 2, Z: 1} def draw_letters @@ -11,6 +11,7 @@ def draw_letters # this method may change strings in letters_in_hand def uses_available_letters?(input, letters_in_hand) + letters_in_hand = letters_in_hand.clone input.each_char do |c| index = letters_in_hand.find_index(c) if index.nil? @@ -44,7 +45,7 @@ def uses_available_letters?(input, letters_in_hand) value: 5 }, { - letters: %w(J K), + letters: %w(J X), value: 8 }, { @@ -56,7 +57,7 @@ def uses_available_letters?(input, letters_in_hand) # refactor! def score_word(word) total_points = 0 - word.each_char do |c| + word.upcase.each_char do |c| SCORE_CHART.each do |score| if score[:letters].include?(c) total_points += score[:value] @@ -70,19 +71,30 @@ def score_word(word) end # wave 4 + +def break_tie(word_1, word_2) + length_1 = word_1[:word].length + length_2 = word_2[:word].length + + return word_1 if length_1 == length_2 + return word_1 if length_1 == 10 + return word_2 if length_2 == 10 + return length_1 < length_2 ? word_1 : word_2 +end + def highest_score_from(words) winning_word = { word: nil, score: 0} words.each do |word| score = score_word(word) #the score of current word + if score > winning_word[:score] winning_word[:score] = score winning_word[:word] = word elsif score == winning_word[:score] - if word.length > winning_word[:word].length - next - end + winning_word = break_tie(winning_word, { word: word, score: score }) end end + return winning_word end From 21f849ce430a3b67a7750542b336ceab9144d0a7 Mon Sep 17 00:00:00 2001 From: Mira Date: Wed, 14 Aug 2019 15:35:17 -0700 Subject: [PATCH 7/9] refactoring for clarity --- lib/adagrams.rb | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index bedf66f..1f6d37b 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,5 +1,6 @@ -LETTER_POOL = {A: 9, N: 6, B: 2, O: 8, C: 2, P: 2, D: 4, Q: 1, E: 12, R: 6, F: 2, S: 4, G: 3, T: 6, H: 2, U: 4, I: 9, V: 2, J: 1, W: 2, K: 1, X: 1, L: 4, Y: 2, M: 2, Z: 1} - +LETTER_POOL = { A: 9, N: 6, B: 2, O: 8, C: 2, P: 2, D: 4, Q: 1, E: 12, + R: 6, F: 2, S: 4, G: 3, T: 6, H: 2, U: 4, I: 9, V: 2, + J: 1, W: 2, K: 1, X: 1, L: 4, Y: 2, M: 2, Z: 1 } def draw_letters letters = LETTER_POOL.map do |letter, count| @@ -9,21 +10,23 @@ def draw_letters return letters.flatten.sample(10) end -# this method may change strings in letters_in_hand def uses_available_letters?(input, letters_in_hand) - letters_in_hand = letters_in_hand.clone - input.each_char do |c| - index = letters_in_hand.find_index(c) + letters = letters_in_hand.clone + + input.upcase.each_char do |c| + index = letters.find_index(c) + if index.nil? return false else - letters_in_hand.delete_at(index) + letters.delete_at(index) end end -return true + + return true end -SCORE_CHART = [ +scores = [ { letters: %w(A E I O U L N R S T), value: 1 @@ -54,24 +57,26 @@ def uses_available_letters?(input, letters_in_hand) } ] -# refactor! +# make scores into a global hash +SCORE_CHART = {} +scores.each do |score| + score[:letters].each do |letter| + SCORE_CHART[letter] = score[:value] + end +end + def score_word(word) total_points = 0 + word.upcase.each_char do |c| - SCORE_CHART.each do |score| - if score[:letters].include?(c) - total_points += score[:value] - end - end - end - if word.length > 6 - total_points += 8 + total_points += SCORE_CHART[c] end + + total_points += 8 if word.length >= 7 + return total_points end -# wave 4 - def break_tie(word_1, word_2) length_1 = word_1[:word].length length_2 = word_2[:word].length @@ -84,6 +89,7 @@ def break_tie(word_1, word_2) def highest_score_from(words) winning_word = { word: nil, score: 0} + words.each do |word| score = score_word(word) #the score of current word From e805e7960122e9038b335e8ccd0b33e362ce9c2f Mon Sep 17 00:00:00 2001 From: Diana Han Date: Wed, 14 Aug 2019 16:02:24 -0700 Subject: [PATCH 8/9] wave 5 tests --- test/adagrams_test.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/adagrams_test.rb b/test/adagrams_test.rb index 90ec44d..e0da148 100644 --- a/test/adagrams_test.rb +++ b/test/adagrams_test.rb @@ -178,4 +178,23 @@ expect(best_word[:score]).must_equal 18 end end + + describe "is_in_english_dict?" do + + it 'should say cat is a word' do + + input = "cat" + + output = is_in_english_dict?(input) + expect(output).must_equal true + end + + it 'should say adagrams is not a word' do + + input = "adagrams" + + output = is_in_english_dict?(input) + expect(output).must_equal false + end + end end From 4e55a5fd815c442d23bcc08cdd2cca6f863e24bc Mon Sep 17 00:00:00 2001 From: Diana Han Date: Wed, 14 Aug 2019 16:03:35 -0700 Subject: [PATCH 9/9] wave 5 implemented --- lib/adagrams.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 1f6d37b..55629ff 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -104,3 +104,6 @@ def highest_score_from(words) return winning_word end +def is_in_english_dict?(input) + File.read("assets/dictionary-english.csv").split.include?(input) +end