From d2937c07bad7c0e9d88ed3abc32650334a28cfae Mon Sep 17 00:00:00 2001 From: leehiggins Date: Mon, 10 Feb 2020 18:44:58 -0800 Subject: [PATCH 01/21] Completed Wave 1 via remote pair programming --- adagrams.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb new file mode 100644 index 0000000..b0d73a8 --- /dev/null +++ b/adagrams.rb @@ -0,0 +1,29 @@ +require 'awesome_print' + +def draw_letters + letters = {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} + + pool_of_letters = [] + + letters.each do |letter, amount| + amount.times do + pool_of_letters << letter.to_s + end + end + + drawn_letters = [] + + 10.times do + letter = pool_of_letters.sample + pool_of_letters.slice!(pool_of_letters.index(letter)) + drawn_letters << letter + end + + drawn_letters.each do |letter| + pool_of_letters << letter + end + + return drawn_letters +end + +print draw_letters \ No newline at end of file From f7978f93c8698e68fc7702fda2b8095a856f9020 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 11 Feb 2020 15:27:10 -0800 Subject: [PATCH 02/21] Moved code from wrong adagrams.rb into lib/adagrams.rb --- adagrams.rb | 29 ----------------------------- lib/adagrams.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 29 deletions(-) delete mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb deleted file mode 100644 index b0d73a8..0000000 --- a/adagrams.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'awesome_print' - -def draw_letters - letters = {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} - - pool_of_letters = [] - - letters.each do |letter, amount| - amount.times do - pool_of_letters << letter.to_s - end - end - - drawn_letters = [] - - 10.times do - letter = pool_of_letters.sample - pool_of_letters.slice!(pool_of_letters.index(letter)) - drawn_letters << letter - end - - drawn_letters.each do |letter| - pool_of_letters << letter - end - - return drawn_letters -end - -print draw_letters \ No newline at end of file diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..4ede828 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,27 @@ +# require 'awesome_print' + +def draw_letters + letters = {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} + + pool_of_letters = [] + + letters.each do |letter, amount| + amount.times do + pool_of_letters << letter.to_s + end + end + + hand = [] + + 10.times do + letter = pool_of_letters.sample + pool_of_letters.slice!(pool_of_letters.index(letter)) + hand << letter + end + + hand.each do |letter| + pool_of_letters << letter + end + + return hand +end \ No newline at end of file From 4862d97058c104140b8bc668ddc0977a82da9f34 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 11 Feb 2020 16:28:21 -0800 Subject: [PATCH 03/21] wave two complete --- lib/adagrams.rb | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 4ede828..324d27e 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -23,5 +23,38 @@ def draw_letters pool_of_letters << letter end - return hand -end \ No newline at end of file + return hand.sort +end + +hand = draw_letters +puts "Your hand is #{hand}." + +def uses_available_letters?(input, letters_in_hand) + lih_clone = letters_in_hand.dup + word = input.split('') + + letters_match = [] + + word.each do |letter| + if lih_clone.index(letter) == nil + break + else + lih_clone.slice!(lih_clone.index(letter)) + letters_match << letter + end + end + + if word == letters_match + word.each do |letter| + lih_clone << letter + end + return true + else + return false + end + + +end + +puts "What is your word?" +uses_available_letters?(gets.chomp.upcase, hand) \ No newline at end of file From 99f052c4c889fcbc509d2b9946c080c9a6488444 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 11 Feb 2020 17:01:07 -0800 Subject: [PATCH 04/21] Add Wave 3: score_word method --- lib/adagrams.rb | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 324d27e..3314cd8 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -53,8 +53,44 @@ def uses_available_letters?(input, letters_in_hand) return false end - end puts "What is your word?" -uses_available_letters?(gets.chomp.upcase, hand) \ No newline at end of file +word = gets.chomp.upcase +uses_available_letters?(word, hand) + +# We want a method that returns the score of a given word as defined by the Adagrams game. +# Name this method score_word in adagrams.rb. This method should have the following properties: + +# Has one parameter: word, which is a string of characters +def score_word(word) + score_chart = { + 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], + 2 => ["D", "G"], + 3 => ["B", "C", "M", "P"], + 4 => ["F", "H", "V", "W", "Y"], + 5 => ["K"], + 6 => ["J", "X"], + 7 => ["Q", "Z"] + } + + word = word.split('') + + score = 0 + word.each do |letter| + score_chart.each do |value, array| + if array.include?(letter) + score = score + value.to_i + end + end + end + # p score + +end + +score_word(word) + +# Returns an integer representing the number of points +# Each letter within word has a point value. The number of points of each letter is summed up to represent the total score of word +# Each letter's point value is described in the table below +# If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points \ No newline at end of file From 6775643cab896955afb86d1f380cf87be9affd43 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 11 Feb 2020 17:16:27 -0800 Subject: [PATCH 05/21] Wave 3 complete --- lib/adagrams.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 3314cd8..b2af9c6 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -70,22 +70,28 @@ def score_word(word) 3 => ["B", "C", "M", "P"], 4 => ["F", "H", "V", "W", "Y"], 5 => ["K"], - 6 => ["J", "X"], - 7 => ["Q", "Z"] + 8 => ["J", "X"], + 10 => ["Q", "Z"] } word = word.split('') + + # p word score = 0 word.each do |letter| score_chart.each do |value, array| - if array.include?(letter) - score = score + value.to_i + if array.include?(letter.upcase) + score += value.to_i end end end - # p score + if word.length > 6 && word.length < 11 + score += 8 + end + + # p score end score_word(word) From 1ac9173956b0e713d433b76660ff8a77635dec97 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 12 Feb 2020 09:09:08 -0800 Subject: [PATCH 06/21] Fixed score_word so it was actually returning score --- lib/adagrams.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index b2af9c6..d82e89e 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -91,7 +91,7 @@ def score_word(word) score += 8 end - # p score + return score end score_word(word) From 57e4501e419c86133b32d78b5f9242d6b394daaa Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 12 Feb 2020 10:30:56 -0800 Subject: [PATCH 07/21] Add highest_score_from method, creates an array of hashes containing each word and its score as key-value pairs --- lib/adagrams.rb | 75 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index d82e89e..f50cffa 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -96,7 +96,74 @@ def score_word(word) score_word(word) -# Returns an integer representing the number of points -# Each letter within word has a point value. The number of points of each letter is summed up to represent the total score of word -# Each letter's point value is described in the table below -# If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points \ No newline at end of file +# After several hands have been drawn, words have been submitted, checked, scored, and played, we want a way to find the highest scoring word. This method looks at the array of words and calculates which of these words has the highest score, applies any tie-breaking logic, and returns the winning word in a special data structure. + +# Add a method called highest_score_from in adagrams.rb. This method should have the following properties: + +# Has one parameter: words, which is an array of strings +def highest_score_from(words) + words_with_scores = [] + + words.each do |word| + words_with_scores.push({:word => word, :score => score_word(word)}) + end + + # get highest score # + # run through hash & see + # if score appears more than once + # write into new array + + # check if any length == 10 + # else find shortest word.length + # if multiple are shortest + # take first + + # shortest_word = words_with_scores.min_by do |index| + # index[:word].length + # end + + # highest_score = words_with_scores.max_by(5) do |index| + # # if index[:word].length == 10 + # index[:score] + # elsif words_with_scores.min_by do |index| + # p index[:word].length + # index[:score] + # end + # else + # index[:score] + # end + # end + + + + # words_with_scores.each do |word| + # if word[:word].length == 10 + # highest_score = word + # elsif word[:word].length + # word.each do |k, v| + + # end + + # end + + + # word with highest score wins + #if there is a tie + # word with 10 letters wins + # else then word.length that is shortest wins + # else the word that occurs first (if length and score are equal) + # end + + + puts highest_score + + # return {:word => "word", + # :score => 0} +end + +highest_score_from(["banana", "dog", "potato"]) + +# In the case of tie in scores, use these tie-breaking rules: +# prefer the word with the fewest letters... +# ...unless one word has 10 letters. If the top score is tied between multiple words and one is 10 letters long, choose the one with 10 letters over the one with fewer tiles +# If the there are multiple words that are the same score and the same length, pick the first one in the supplied list \ No newline at end of file From 921f459e7ed384dbae18587e5f546dffc9355ca8 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Wed, 12 Feb 2020 15:24:29 -0800 Subject: [PATCH 08/21] create tie breaking solution for shortest word --- lib/adagrams.rb | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index f50cffa..11c0114 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -108,19 +108,40 @@ def highest_score_from(words) words_with_scores.push({:word => word, :score => score_word(word)}) end - # get highest score # - # run through hash & see - # if score appears more than once - # write into new array + highest_score = words_with_scores.max_by do |index| + index[:score] + end + + tied_scores = [] + words_with_scores.each do |word| + if word[:score] == highest_score[:score] + tied_scores << word + end + end + + shortest_word = tied_scores.min_by do |index| + index[:word].length + end + + puts shortest_word + + winner = '' + + tied_scores.each do |tied| + if tied[:word].length == 10 + return tied + elsif tied[:word].length == shortest_word[:word].length + return tied + else + next + end + end # check if any length == 10 # else find shortest word.length # if multiple are shortest # take first - # shortest_word = words_with_scores.min_by do |index| - # index[:word].length - # end # highest_score = words_with_scores.max_by(5) do |index| # # if index[:word].length == 10 @@ -154,14 +175,11 @@ def highest_score_from(words) # else the word that occurs first (if length and score are equal) # end - - puts highest_score - # return {:word => "word", # :score => 0} end -highest_score_from(["banana", "dog", "potato"]) +highest_score_from(["MMMM", "WWW"]) # In the case of tie in scores, use these tie-breaking rules: # prefer the word with the fewest letters... From 8dd25329cf06388bb3794a3ae8c7f088f8c1cad7 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Wed, 12 Feb 2020 15:38:46 -0800 Subject: [PATCH 09/21] fixed bug in wave 4 for 10 letter tie breaker --- lib/adagrams.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 11c0114..67102d5 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -123,9 +123,12 @@ def highest_score_from(words) index[:word].length end - puts shortest_word + tied_scores.sort_by! do |tied| + -tied[:word].length + end + + puts tied_scores - winner = '' tied_scores.each do |tied| if tied[:word].length == 10 @@ -179,7 +182,7 @@ def highest_score_from(words) # :score => 0} end -highest_score_from(["MMMM", "WWW"]) +highest_score_from(["BBBBBB", "AAAAAAAAAA"]) # In the case of tie in scores, use these tie-breaking rules: # prefer the word with the fewest letters... From e1c52c238513704d989e153b86348d15913354c7 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Wed, 12 Feb 2020 15:46:36 -0800 Subject: [PATCH 10/21] Cleaned up the file, removed comments --- lib/adagrams.rb | 60 +++++-------------------------------------------- 1 file changed, 6 insertions(+), 54 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 67102d5..19381ac 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,4 +1,4 @@ -# require 'awesome_print' +## WAVE 1 ## def draw_letters letters = {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} @@ -29,6 +29,8 @@ def draw_letters hand = draw_letters puts "Your hand is #{hand}." +## WAVE 2 ## + def uses_available_letters?(input, letters_in_hand) lih_clone = letters_in_hand.dup word = input.split('') @@ -59,10 +61,8 @@ def uses_available_letters?(input, letters_in_hand) word = gets.chomp.upcase uses_available_letters?(word, hand) -# We want a method that returns the score of a given word as defined by the Adagrams game. -# Name this method score_word in adagrams.rb. This method should have the following properties: +## WAVE 3 ## -# Has one parameter: word, which is a string of characters def score_word(word) score_chart = { 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], @@ -76,8 +76,6 @@ def score_word(word) word = word.split('') - # p word - score = 0 word.each do |letter| score_chart.each do |value, array| @@ -96,11 +94,8 @@ def score_word(word) score_word(word) -# After several hands have been drawn, words have been submitted, checked, scored, and played, we want a way to find the highest scoring word. This method looks at the array of words and calculates which of these words has the highest score, applies any tie-breaking logic, and returns the winning word in a special data structure. - -# Add a method called highest_score_from in adagrams.rb. This method should have the following properties: +## WAVE 4 ## -# Has one parameter: words, which is an array of strings def highest_score_from(words) words_with_scores = [] @@ -139,52 +134,9 @@ def highest_score_from(words) next end end - - # check if any length == 10 - # else find shortest word.length - # if multiple are shortest - # take first - - - # highest_score = words_with_scores.max_by(5) do |index| - # # if index[:word].length == 10 - # index[:score] - # elsif words_with_scores.min_by do |index| - # p index[:word].length - # index[:score] - # end - # else - # index[:score] - # end - # end - - - - # words_with_scores.each do |word| - # if word[:word].length == 10 - # highest_score = word - # elsif word[:word].length - # word.each do |k, v| - - # end - - # end - - - # word with highest score wins - #if there is a tie - # word with 10 letters wins - # else then word.length that is shortest wins - # else the word that occurs first (if length and score are equal) - # end - # return {:word => "word", - # :score => 0} end highest_score_from(["BBBBBB", "AAAAAAAAAA"]) -# In the case of tie in scores, use these tie-breaking rules: -# prefer the word with the fewest letters... -# ...unless one word has 10 letters. If the top score is tied between multiple words and one is 10 letters long, choose the one with 10 letters over the one with fewer tiles -# If the there are multiple words that are the same score and the same length, pick the first one in the supplied list \ No newline at end of file +## WAVE 5 ## \ No newline at end of file From 3cecb9d67ef1931c5101725daeeea8e801b989e5 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Wed, 12 Feb 2020 16:10:15 -0800 Subject: [PATCH 11/21] Refactor Waves 1-3 --- lib/adagrams.rb | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 19381ac..966589f 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -35,15 +35,10 @@ def uses_available_letters?(input, letters_in_hand) lih_clone = letters_in_hand.dup word = input.split('') - letters_match = [] - - word.each do |letter| - if lih_clone.index(letter) == nil - break - else - lih_clone.slice!(lih_clone.index(letter)) - letters_match << letter - end + letters_match = word.map do |letter| + break if lih_clone.index(letter) == nil + lih_clone.slice!(lih_clone.index(letter)) + letter end if word == letters_match @@ -79,16 +74,12 @@ def score_word(word) score = 0 word.each do |letter| score_chart.each do |value, array| - if array.include?(letter.upcase) - score += value.to_i - end + score += value.to_i if array.include?(letter.upcase) end end - if word.length > 6 && word.length < 11 - score += 8 - end - + score += 8 if word.length > 6 && word.length < 11 + return score end @@ -124,7 +115,6 @@ def highest_score_from(words) puts tied_scores - tied_scores.each do |tied| if tied[:word].length == 10 return tied From f6a3311a8da74d1ff70eb9b0d6cc44978d9d13df Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Wed, 12 Feb 2020 17:06:42 -0800 Subject: [PATCH 12/21] start refactoring wave 4 to use different data structure --- lib/adagrams.rb | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 966589f..601bd96 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -88,45 +88,42 @@ def score_word(word) ## WAVE 4 ## def highest_score_from(words) - words_with_scores = [] + words_with_scores = {} words.each do |word| - words_with_scores.push({:word => word, :score => score_word(word)}) + words_with_scores[word] = score_word(word) end - highest_score = words_with_scores.max_by do |index| - index[:score] + highest_score = words_with_scores.max_by do |word, score| + score end - tied_scores = [] - words_with_scores.each do |word| - if word[:score] == highest_score[:score] - tied_scores << word - end - end - - shortest_word = tied_scores.min_by do |index| - index[:word].length + tied_scores = {} + words_with_scores.each do |word, score| + tied_scores[word] = score if score == highest_score[1] end - tied_scores.sort_by! do |tied| - -tied[:word].length + shortest_length = 0 + tied_scores.min_by do |word, score| + shortest_length = word.length end - puts tied_scores + # tied_scores.sort_by! do |tied| + # -tied[:word].length + # end - tied_scores.each do |tied| - if tied[:word].length == 10 - return tied - elsif tied[:word].length == shortest_word[:word].length - return tied - else - next - end - end + # tied_scores.each do |tied| + # if tied[:word].length == 10 + # return tied + # elsif tied[:word].length == shortest_word[:word].length + # return tied + # else + # next + # end + # end end -highest_score_from(["BBBBBB", "AAAAAAAAAA"]) +highest_score_from(["potato", "banana", "dog"]) ## WAVE 5 ## \ No newline at end of file From c82b583f4c14d5e36f03c6f8c55abae43a2f3191 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Thu, 13 Feb 2020 14:53:35 -0800 Subject: [PATCH 13/21] complete refactor of wave 4 with condensed data structure --- lib/adagrams.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 601bd96..421a933 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -108,19 +108,20 @@ def highest_score_from(words) shortest_length = word.length end - # tied_scores.sort_by! do |tied| - # -tied[:word].length + # tied_scores.sort_by! do |word, score| + # -word.length # end - # tied_scores.each do |tied| - # if tied[:word].length == 10 - # return tied - # elsif tied[:word].length == shortest_word[:word].length - # return tied - # else - # next - # end - # end + tied_scores.each do |word, score| + hash = {word => score} + if word.length == 10 + return hash + elsif word.length == shortest_length + return hash + else + next + end + end end From 61741329ccf878dad5dace589602f55116347356 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Thu, 13 Feb 2020 15:02:46 -0800 Subject: [PATCH 14/21] change back to nested hash structure and refactor --- lib/adagrams.rb | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 421a933..8ecef0a 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -88,43 +88,37 @@ def score_word(word) ## WAVE 4 ## def highest_score_from(words) - words_with_scores = {} - - words.each do |word| - words_with_scores[word] = score_word(word) + words_with_scores = words.map do |word| + {:word => word, :score => score_word(word)} end - highest_score = words_with_scores.max_by do |word, score| - score + highest_score = words_with_scores.max_by do |index| + index[:score] end - tied_scores = {} - words_with_scores.each do |word, score| - tied_scores[word] = score if score == highest_score[1] + tied_scores = [] + words_with_scores.each do |word| + tied_scores << word if word[:score] == highest_score[:score] end - shortest_length = 0 - tied_scores.min_by do |word, score| - shortest_length = word.length + shortest_length = tied_scores.min_by do |index| + index[:word].length end - # tied_scores.sort_by! do |word, score| - # -word.length - # end - - tied_scores.each do |word, score| - hash = {word => score} - if word.length == 10 - return hash - elsif word.length == shortest_length - return hash - else - next - end + tied_scores.sort_by! do |tied| + -tied[:word].length end + tied_scores.each do |tied| + case tied[:word].length + when 10 + return tied + when shortest_length[:word].length + return tied + else + next + end + end end -highest_score_from(["potato", "banana", "dog"]) - ## WAVE 5 ## \ No newline at end of file From d51614742752b77d3542f76b6ba2df1ed24fcc7f Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Thu, 13 Feb 2020 15:39:45 -0800 Subject: [PATCH 15/21] wave 5 complete - check input against disctionary --- lib/adagrams.rb | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 8ecef0a..b3c9e05 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,3 +1,5 @@ +require 'csv' + ## WAVE 1 ## def draw_letters @@ -26,14 +28,11 @@ def draw_letters return hand.sort end -hand = draw_letters -puts "Your hand is #{hand}." - ## WAVE 2 ## def uses_available_letters?(input, letters_in_hand) lih_clone = letters_in_hand.dup - word = input.split('') + word = input.upcase.split('') letters_match = word.map do |letter| break if lih_clone.index(letter) == nil @@ -52,10 +51,6 @@ def uses_available_letters?(input, letters_in_hand) end -puts "What is your word?" -word = gets.chomp.upcase -uses_available_letters?(word, hand) - ## WAVE 3 ## def score_word(word) @@ -83,8 +78,6 @@ def score_word(word) return score end -score_word(word) - ## WAVE 4 ## def highest_score_from(words) @@ -121,4 +114,19 @@ def highest_score_from(words) end end -## WAVE 5 ## \ No newline at end of file +## WAVE 5 ## + +def is_in_english_dict?(input) + input_array = [input] + + dictionary = CSV.read('assets/dictionary-english.csv').map do |word| + word + end + + if dictionary.include?(input_array) + return true + else + return false + end + +end \ No newline at end of file From e8508f64c9c9919848131e06d727134484614710 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Thu, 13 Feb 2020 15:42:42 -0800 Subject: [PATCH 16/21] refactor wave 5 --- lib/adagrams.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index b3c9e05..2fe86df 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -123,10 +123,5 @@ def is_in_english_dict?(input) word end - if dictionary.include?(input_array) - return true - else - return false - end - + return dictionary.include?(input_array) ? true : false end \ No newline at end of file From aeac0acad135876ba43a23494a5c4db2c6bb1821 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Thu, 13 Feb 2020 15:50:00 -0800 Subject: [PATCH 17/21] add test cases for is_in_english_dict method --- test/adagrams_test.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/adagrams_test.rb b/test/adagrams_test.rb index 90ec44d..31e3344 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 method" do + it 'if word appears in the dictionary' do + # arrange + word = "banana" + + # act + is_in_dictionary = is_in_english_dict?(word) + + # assert + expect(is_in_dictionary).must_equal true + end + + it 'if word does not appear in the dictionary' do + # arrange + word = "gaenih" + + # act + is_in_dictionary = is_in_english_dict?(word) + + # assert + expect(is_in_dictionary).must_equal false + end + end end From 27a47e678b98d849141229fd7ce92caec6c55764 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Thu, 13 Feb 2020 16:55:45 -0800 Subject: [PATCH 18/21] add group_by method to wave 5 --- lib/adagrams.rb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 2fe86df..b7c781b 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,7 +1,6 @@ require 'csv' -## WAVE 1 ## - +## WAVE 1 - build a hand of 10 random letters from a pool ## def draw_letters letters = {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} @@ -28,8 +27,7 @@ def draw_letters return hand.sort end -## WAVE 2 ## - +## WAVE 2 - check if an input word only uses words contained within the hand ## def uses_available_letters?(input, letters_in_hand) lih_clone = letters_in_hand.dup word = input.upcase.split('') @@ -48,11 +46,9 @@ def uses_available_letters?(input, letters_in_hand) else return false end - end -## WAVE 3 ## - +## WAVE 3 - returns the score of a given word ## def score_word(word) score_chart = { 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], @@ -78,8 +74,7 @@ def score_word(word) return score end -## WAVE 4 ## - +## WAVE 4 - find the highest scoring word ## def highest_score_from(words) words_with_scores = words.map do |word| {:word => word, :score => score_word(word)} @@ -114,14 +109,15 @@ def highest_score_from(words) end end -## WAVE 5 ## - -def is_in_english_dict?(input) - input_array = [input] - +## WAVE 5 - verify that the input word is valid against the english dictionary ## +def is_in_english_dict?(input) dictionary = CSV.read('assets/dictionary-english.csv').map do |word| - word + word[0] end - return dictionary.include?(input_array) ? true : false + dictionary_grouped = dictionary.group_by do |word| + word[0] + end + + return dictionary_grouped[input[0]].include?(input) end \ No newline at end of file From bd7d849cf67410f9ffa5116cd834e42a5bb0dc4f Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Thu, 13 Feb 2020 16:55:56 -0800 Subject: [PATCH 19/21] remove unnecessary comments --- test/adagrams_test.rb | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/test/adagrams_test.rb b/test/adagrams_test.rb index 31e3344..cacd361 100644 --- a/test/adagrams_test.rb +++ b/test/adagrams_test.rb @@ -181,24 +181,14 @@ describe "is_in_english_dict method" do it 'if word appears in the dictionary' do - # arrange word = "banana" - - # act is_in_dictionary = is_in_english_dict?(word) - - # assert expect(is_in_dictionary).must_equal true end it 'if word does not appear in the dictionary' do - # arrange word = "gaenih" - - # act is_in_dictionary = is_in_english_dict?(word) - - # assert expect(is_in_dictionary).must_equal false end end From 0d008e353f0546dbf6a0aef71f5cd622633f395b Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Fri, 14 Feb 2020 14:32:05 -0800 Subject: [PATCH 20/21] Changed input to downcase in Wave 5, exit loop if tied_scores only has one element in Wave 4 --- lib/adagrams.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index b7c781b..b638951 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -89,6 +89,8 @@ def highest_score_from(words) tied_scores << word if word[:score] == highest_score[:score] end + return tied_scores[0] if tied_scores.length == 1 + shortest_length = tied_scores.min_by do |index| index[:word].length end @@ -111,10 +113,12 @@ def highest_score_from(words) ## WAVE 5 - verify that the input word is valid against the english dictionary ## def is_in_english_dict?(input) - dictionary = CSV.read('assets/dictionary-english.csv').map do |word| + input = input.downcase + + dictionary = CSV.read('assets/dictionary-english.csv', headers: true).map do |word| word[0] end - + dictionary_grouped = dictionary.group_by do |word| word[0] end From 6acaf6cc73b2e9ffb0ed01d3dcf1c99bdb2d3314 Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Fri, 14 Feb 2020 14:33:06 -0800 Subject: [PATCH 21/21] Added a test to check that word is downcase --- test/adagrams_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/adagrams_test.rb b/test/adagrams_test.rb index cacd361..561110b 100644 --- a/test/adagrams_test.rb +++ b/test/adagrams_test.rb @@ -191,5 +191,16 @@ is_in_dictionary = is_in_english_dict?(word) expect(is_in_dictionary).must_equal false end + + it 'check if word is downcase' do + word = "BANANA" + weird_word = "bAnANas" + + is_in_dictionary = is_in_english_dict?(word) + expect(is_in_dictionary).must_equal true + + is_in_dictionary = is_in_english_dict?(weird_word) + expect(is_in_dictionary).must_equal true + end end end