From ec7adbe1c16c26f0c7f13d91c18b8a2018168815 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 11 Feb 2020 16:01:35 -0800 Subject: [PATCH 01/14] Completed wave 1, 2, 3, and making progress on wave 4 --- adagrams.rb | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb new file mode 100644 index 0000000..bb17384 --- /dev/null +++ b/adagrams.rb @@ -0,0 +1,117 @@ +# wave 1 +def draw_letters + letters_pool = %w(A A A A A A A A A B B C C D D D D E E E E E E E E E E E E F F G G G H H I I I I I I I I I J K L L L L M M N N N N N N O O O O O O O O P P Q R R R R R R S S S S T T T T T T U U U U V V W W X Y Y Z) + hand = [] + + 10.times do + hand << letters_pool.sample + end + + return hand +end + +# wave 2 +def uses_available_letters?(input, letters_in_hand) + letters = input.upcase.split('') + is_in_hand = true + + letters.each do |letter| + if !(letters_in_hand.include?(letter)) + is_in_hand = false + end + end + + return is_in_hand +end + +# wave 3 +def score_word (word) + letters = word.upcase.split('') + score = 0 + + score_table = { + 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'], + 8 => ['J', 'X'], + 10 => ['Q', 'Z'] + } + + letters.each do |letter| + score_table.each do |score_value, letter_array| + if letter_array.include?(letter) + score += score_value.to_i + end + end + end + + if letters.length >= 7 && letters.length <= 10 + score += 8 + end + + return score +end + +#a method that accepts an array of words that have been created, gets the highest score from this array. Pass each word from the array into the scoring method, returns will be inserted into an array, and for the word that is the highest score it will be reserved into a single hash with the word as the key and the score as the value. + +# wave 4 +def highest_score_from(words) + words_hash = {} + + words.each do |word| + words_hash[word] = score_word(word) + end + + + highest_word = [] + highest_scored = 0 + + +#in case of ties: + if words_hash == words_hash.invert.invert #will invert the key and value pairs of hash twice, thus eliminiting any duplicate values. + words_hash.each do |word, score| + if score == highest_scored + + else + highest_scored = score + end + end + + + # words_hash.each do |word, score| + # if highest_word == word + # unless highest_word.length == 10 || highest_word.length + # highest_word = word + # end + # if word.length + # end + # end + + + + + words_hash.each do |key, value| + if value == highest_scored + highest_word << key + end + end + + return {word: highest_word, score: highest_scored} +end + +drawn_hand = draw_letters + +user_word = 'o' + +validity = uses_available_letters?(user_word, drawn_hand) + +result = highest_score_from(['alicia', 'jessica', 'jared', 'shonda', 'becca']) +puts result + +score = 0 + +if validity == true + score = score_word (user_word) +end \ No newline at end of file From 67125d7b56cbf8217b97c52887e195f17bceacc7 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 11 Feb 2020 16:35:26 -0800 Subject: [PATCH 02/14] Wave 4: Works, but not correctly. --- adagrams.rb | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/adagrams.rb b/adagrams.rb index bb17384..b7c5cf3 100644 --- a/adagrams.rb +++ b/adagrams.rb @@ -65,40 +65,24 @@ def highest_score_from(words) end - highest_word = [] + highest_word = "" highest_scored = 0 - -#in case of ties: - if words_hash == words_hash.invert.invert #will invert the key and value pairs of hash twice, thus eliminiting any duplicate values. - words_hash.each do |word, score| - if score == highest_scored - - else - highest_scored = score + words_hash.each do |word, score| + if score > highest_scored + highest_scored = score + highest_word = word + elsif score == highest_scored + #it's a duplicate! + case word + when word.length == 10 && highest_word.length != 10 + highest_word = word + when highest_word.length > word.length + highest_word = word end - end - - - # words_hash.each do |word, score| - # if highest_word == word - # unless highest_word.length == 10 || highest_word.length - # highest_word = word - # end - # if word.length - # end - # end - - - - - words_hash.each do |key, value| - if value == highest_scored - highest_word << key end + return {word: highest_word, score: highest_scored} end - - return {word: highest_word, score: highest_scored} end drawn_hand = draw_letters From b262a6c0dc9245004328d1bfc66dd9492b53b593 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 11 Feb 2020 16:40:01 -0800 Subject: [PATCH 03/14] Wave 4: Updated and magic, boom, it works. --- adagrams.rb | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/adagrams.rb b/adagrams.rb index b7c5cf3..a7e5ea3 100644 --- a/adagrams.rb +++ b/adagrams.rb @@ -54,8 +54,6 @@ def score_word (word) return score end -#a method that accepts an array of words that have been created, gets the highest score from this array. Pass each word from the array into the scoring method, returns will be inserted into an array, and for the word that is the highest score it will be reserved into a single hash with the word as the key and the score as the value. - # wave 4 def highest_score_from(words) words_hash = {} @@ -64,7 +62,6 @@ def highest_score_from(words) words_hash[word] = score_word(word) end - highest_word = "" highest_scored = 0 @@ -73,16 +70,16 @@ def highest_score_from(words) highest_scored = score highest_word = word elsif score == highest_scored - #it's a duplicate! case word - when word.length == 10 && highest_word.length != 10 - highest_word = word - when highest_word.length > word.length - highest_word = word + when word.length == 10 && highest_word.length != 10 + highest_word = word + when highest_word.length > word.length + highest_word = word end end - return {word: highest_word, score: highest_scored} end + + return {word: highest_word, score: highest_scored} end drawn_hand = draw_letters @@ -91,7 +88,7 @@ def highest_score_from(words) validity = uses_available_letters?(user_word, drawn_hand) -result = highest_score_from(['alicia', 'jessica', 'jared', 'shonda', 'becca']) +result = highest_score_from(['alicia', 'jestica', 'jessica', 'jestica', 'becca']) puts result score = 0 From 05971f0fe83d86302807bc03601bc1f2fcfc4d2f Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Tue, 11 Feb 2020 16:45:02 -0800 Subject: [PATCH 04/14] Wave 4/4; completed; pending 5? --- adagrams.rb | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/adagrams.rb b/adagrams.rb index a7e5ea3..772a627 100644 --- a/adagrams.rb +++ b/adagrams.rb @@ -80,19 +80,4 @@ def highest_score_from(words) end return {word: highest_word, score: highest_scored} -end - -drawn_hand = draw_letters - -user_word = 'o' - -validity = uses_available_letters?(user_word, drawn_hand) - -result = highest_score_from(['alicia', 'jestica', 'jessica', 'jestica', 'becca']) -puts result - -score = 0 - -if validity == true - score = score_word (user_word) end \ No newline at end of file From 137a46b313adfc757a23866a857ce8589bb035e0 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Wed, 12 Feb 2020 09:19:58 -0800 Subject: [PATCH 05/14] Added comments to explain code --- adagrams.rb | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/adagrams.rb b/adagrams.rb index 772a627..b60a370 100644 --- a/adagrams.rb +++ b/adagrams.rb @@ -1,6 +1,9 @@ -# wave 1 +# Wave 1 +# Draws ten letters for the user. def draw_letters + # Variable holding our pool of letters. letters_pool = %w(A A A A A A A A A B B C C D D D D E E E E E E E E E E E E F F G G G H H I I I I I I I I I J K L L L L M M N N N N N N O O O O O O O O P P Q R R R R R R S S S S T T T T T T U U U U V V W W X Y Y Z) + # Variable holding the letters passed to the user. hand = [] 10.times do @@ -10,9 +13,12 @@ def draw_letters return hand end -# wave 2 -def uses_available_letters?(input, letters_in_hand) +# Wave 2 +# Checks if the word the user inputs is available in letters in hand. +def uses_available_letters? (input, letters_in_hand) + # Variable holding array of letters of chosen word. letters = input.upcase.split('') + # Variable tracking if the word is available. is_in_hand = true letters.each do |letter| @@ -24,9 +30,12 @@ def uses_available_letters?(input, letters_in_hand) return is_in_hand end -# wave 3 +# Wave 3 +# Scores the word that the user has inputted. def score_word (word) + # Variable holding individual letters of chosen word. letters = word.upcase.split('') + # Variable tracking the score of the word. score = 0 score_table = { @@ -47,6 +56,8 @@ def score_word (word) end end + # If the chosen word is greater in length than 7 but less than 10, + # 8 more points will be added to their score. if letters.length >= 7 && letters.length <= 10 score += 8 end @@ -54,16 +65,18 @@ def score_word (word) return score end -# wave 4 +# Wave 4 +# Chooses the highest scoring word from all words inputted. def highest_score_from(words) + # Variable tracking each word and its respective score. words_hash = {} words.each do |word| words_hash[word] = score_word(word) end - highest_word = "" - highest_scored = 0 + highest_word = "" # Variable tracking word that scores the highest. + highest_scored = 0 # Variable tracking the highest score. words_hash.each do |word, score| if score > highest_scored From 83b1beefa3bbaa5765a5ea9164ba16340bd84e26 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Wed, 12 Feb 2020 09:24:16 -0800 Subject: [PATCH 06/14] Recommitting, accidentally changed --- wave-4-game.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wave-4-game.rb b/wave-4-game.rb index 958dc10..a003f77 100644 --- a/wave-4-game.rb +++ b/wave-4-game.rb @@ -75,4 +75,4 @@ def run_game display_goodbye_message end -run_game +run_game \ No newline at end of file From 9b04f8474afa843b3e02803bbe5f9bed3eeb756c Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Wed, 12 Feb 2020 11:28:13 -0800 Subject: [PATCH 07/14] practicing git push, disregard this message --- adagrams.rb | 96 ---------------------------------------------- lib/adagrams.rb | 100 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 96 deletions(-) delete mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb deleted file mode 100644 index b60a370..0000000 --- a/adagrams.rb +++ /dev/null @@ -1,96 +0,0 @@ -# Wave 1 -# Draws ten letters for the user. -def draw_letters - # Variable holding our pool of letters. - letters_pool = %w(A A A A A A A A A B B C C D D D D E E E E E E E E E E E E F F G G G H H I I I I I I I I I J K L L L L M M N N N N N N O O O O O O O O P P Q R R R R R R S S S S T T T T T T U U U U V V W W X Y Y Z) - # Variable holding the letters passed to the user. - hand = [] - - 10.times do - hand << letters_pool.sample - end - - return hand -end - -# Wave 2 -# Checks if the word the user inputs is available in letters in hand. -def uses_available_letters? (input, letters_in_hand) - # Variable holding array of letters of chosen word. - letters = input.upcase.split('') - # Variable tracking if the word is available. - is_in_hand = true - - letters.each do |letter| - if !(letters_in_hand.include?(letter)) - is_in_hand = false - end - end - - return is_in_hand -end - -# Wave 3 -# Scores the word that the user has inputted. -def score_word (word) - # Variable holding individual letters of chosen word. - letters = word.upcase.split('') - # Variable tracking the score of the word. - score = 0 - - score_table = { - 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'], - 8 => ['J', 'X'], - 10 => ['Q', 'Z'] - } - - letters.each do |letter| - score_table.each do |score_value, letter_array| - if letter_array.include?(letter) - score += score_value.to_i - end - end - end - - # If the chosen word is greater in length than 7 but less than 10, - # 8 more points will be added to their score. - if letters.length >= 7 && letters.length <= 10 - score += 8 - end - - return score -end - -# Wave 4 -# Chooses the highest scoring word from all words inputted. -def highest_score_from(words) - # Variable tracking each word and its respective score. - words_hash = {} - - words.each do |word| - words_hash[word] = score_word(word) - end - - highest_word = "" # Variable tracking word that scores the highest. - highest_scored = 0 # Variable tracking the highest score. - - words_hash.each do |word, score| - if score > highest_scored - highest_scored = score - highest_word = word - elsif score == highest_scored - case word - when word.length == 10 && highest_word.length != 10 - highest_word = word - when highest_word.length > word.length - highest_word = word - end - end - end - - return {word: highest_word, score: highest_scored} -end \ No newline at end of file diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..589738f 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,100 @@ +# Wave 1 +# Draws ten letters for the user. +def draw_letters + # Variable holding our pool of letters. + letters_pool = %w(A A A A A A A A A B B C C D D D D E E E E E E E E E E E E F F G G G H H I I I I I I I I I J K L L L L M M N N N N N N O O O O O O O O P P Q R R R R R R S S S S T T T T T T U U U U V V W W X Y Y Z) + # Variable holding the letters passed to the user. + hand = [] + + 10.times do + hand << letters_pool.sample + end + + return hand +end + +# Wave 2 +# Checks if the word the user inputs is available in letters in hand. +def uses_available_letters?(input, letters_in_hand) + # Variable holding array of letters of chosen word. + letters = input.upcase.split('') + # Variable tracking if the word is available. + is_in_hand = true + is_checked = [] + letters.each do |letter| + if letters_in_hand.include?(letter) == false + is_in_hand = false + elsif is_checked.include?(letter) + is_in_hand = false + else + is_checked << letter + end + end + + return is_in_hand +end + +# Wave 3 +# Scores the word that the user has inputted. +def score_word(word) + # Variable holding individual letters of chosen word. + letters = word.upcase.split('') + # Variable tracking the score of the word. + score = 0 + + score_table = { + 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'], + 8 => ['J', 'X'], + 10 => ['Q', 'Z'] + } + + letters.each do |letter| + score_table.each do |score_value, letter_array| + if letter_array.include?(letter) + score += score_value.to_i + end + end + end + + # If the chosen word is greater in length than 7 but less than 10, + # 8 more points will be added to their score. + if letters.length >= 7 && letters.length <= 10 + score += 8 + end + + return score +end + +# Wave 4 +# Chooses the highest scoring word from all words inputted. +def highest_score_from(words) + # Variable tracking each word and its respective score. + words_hash = {} + + words.each do |word| + words_hash[word] = score_word(word) + end + + highest_word = "" # Variable tracking word that scores the highest. + highest_scored = 0 # Variable tracking the highest score. + + words_hash.each do |word, score| + if score > highest_scored + highest_scored = score + highest_word = word + elsif score == highest_scored + case word + when word.length == 10 && highest_word.length != 10 + highest_word = word + when word.length < highest_word.length + highest_word = word + end + end + end + + return {word: highest_word, score: highest_scored} +end \ No newline at end of file From 37c22bda21944f8e663add09451873fb1676f04c Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Wed, 12 Feb 2020 14:49:23 -0800 Subject: [PATCH 08/14] Fixed wave 2 bug; it now properly checks if word can be made from hand. --- lib/adagrams.rb | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 589738f..21480ae 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -18,20 +18,9 @@ def draw_letters def uses_available_letters?(input, letters_in_hand) # Variable holding array of letters of chosen word. letters = input.upcase.split('') - # Variable tracking if the word is available. - is_in_hand = true - is_checked = [] - letters.each do |letter| - if letters_in_hand.include?(letter) == false - is_in_hand = false - elsif is_checked.include?(letter) - is_in_hand = false - else - is_checked << letter - end - end - - return is_in_hand + + check_overlap = letters_in_hand & letters + check_overlap == letters ? true : false end # Wave 3 From 8f4945cb4b19b036d9d6f21f2249cb38c1e679c4 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Wed, 12 Feb 2020 15:08:18 -0800 Subject: [PATCH 09/14] Wave 4: Fixed bugs with order of priority in case of tie. --- lib/adagrams.rb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 21480ae..4875fd8 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -18,7 +18,7 @@ def draw_letters def uses_available_letters?(input, letters_in_hand) # Variable holding array of letters of chosen word. letters = input.upcase.split('') - + check_overlap = letters_in_hand & letters check_overlap == letters ? true : false end @@ -72,16 +72,19 @@ def highest_score_from(words) highest_scored = 0 # Variable tracking the highest score. words_hash.each do |word, score| - if score > highest_scored - highest_scored = score - highest_word = word - elsif score == highest_scored - case word - when word.length == 10 && highest_word.length != 10 - highest_word = word - when word.length < highest_word.length - highest_word = word - end + case + when score > highest_scored + highest_scored = score + highest_word = word + # In case of a tie + when score == highest_scored && word.length == 10 && highest_word.length != 10 + highest_word = word + when score == highest_scored && highest_word.length == 10 && word.length != 10 + highest_word + when score == highest_scored && word.length == 10 && highest_word.length == 10 + highest_word + when score == highest_scored && word.length < highest_word.length + highest_word = word end end From 3fae805c53a9213f0064f0bcc53ab2d8d02698a6 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Wed, 12 Feb 2020 15:12:13 -0800 Subject: [PATCH 10/14] Clean up code spacing --- lib/adagrams.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 4875fd8..d854c96 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -9,7 +9,6 @@ def draw_letters 10.times do hand << letters_pool.sample end - return hand end @@ -18,7 +17,6 @@ def draw_letters def uses_available_letters?(input, letters_in_hand) # Variable holding array of letters of chosen word. letters = input.upcase.split('') - check_overlap = letters_in_hand & letters check_overlap == letters ? true : false end @@ -48,13 +46,11 @@ def score_word(word) end end end - # If the chosen word is greater in length than 7 but less than 10, # 8 more points will be added to their score. if letters.length >= 7 && letters.length <= 10 score += 8 end - return score end @@ -63,14 +59,13 @@ def score_word(word) def highest_score_from(words) # Variable tracking each word and its respective score. words_hash = {} + highest_word = "" # Variable tracking word that scores the highest. + highest_scored = 0 # Variable tracking the highest score. words.each do |word| words_hash[word] = score_word(word) end - highest_word = "" # Variable tracking word that scores the highest. - highest_scored = 0 # Variable tracking the highest score. - words_hash.each do |word, score| case when score > highest_scored @@ -87,6 +82,5 @@ def highest_score_from(words) highest_word = word end end - return {word: highest_word, score: highest_scored} end \ No newline at end of file From cae6e94793ff9a2abf70d7ea6e4429cb8d9a3bd7 Mon Sep 17 00:00:00 2001 From: Jessica Liang Date: Wed, 12 Feb 2020 16:24:24 -0800 Subject: [PATCH 11/14] Added a method for tie breaking. --- lib/adagrams.rb | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index d854c96..ab02a74 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -67,20 +67,28 @@ def highest_score_from(words) end words_hash.each do |word, score| - case - when score > highest_scored - highest_scored = score - highest_word = word - # In case of a tie - when score == highest_scored && word.length == 10 && highest_word.length != 10 - highest_word = word - when score == highest_scored && highest_word.length == 10 && word.length != 10 - highest_word - when score == highest_scored && word.length == 10 && highest_word.length == 10 - highest_word - when score == highest_scored && word.length < highest_word.length - highest_word = word + if score > highest_scored + highest_scored = score + highest_word = word + # In case of a tie + elsif score == highest_scored + highest_word = tiebreaker(highest_word, word) end end return {word: highest_word, score: highest_scored} +end + +# Breaks the tie. +def tiebreaker(highest_word, word) + case + when word.length == 10 && highest_word.length != 10 + highest_word = word + when highest_word.length == 10 && word.length != 10 + highest_word + when word.length == 10 && highest_word.length == 10 + highest_word + when word.length < highest_word.length + highest_word = word + end + return highest_word end \ No newline at end of file From 5f6d37cd0c750f21654bdc810bc93343cd9d7edf Mon Sep 17 00:00:00 2001 From: Jessica Liang Date: Thu, 13 Feb 2020 14:44:22 -0800 Subject: [PATCH 12/14] Refactored some if statements in Wave 3 --- lib/adagrams.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index ab02a74..541e00c 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -41,16 +41,13 @@ def score_word(word) letters.each do |letter| score_table.each do |score_value, letter_array| - if letter_array.include?(letter) - score += score_value.to_i - end + score += score_value.to_i if letter_array.include?(letter) end end # If the chosen word is greater in length than 7 but less than 10, # 8 more points will be added to their score. - if letters.length >= 7 && letters.length <= 10 - score += 8 - end + score += 8 if (letters.length >= 7 && letters.length <= 10) + return score end From bdfd45e2d6580b91287c006edd86b54bf985feb1 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Thu, 13 Feb 2020 22:38:19 -0800 Subject: [PATCH 13/14] Added logic to check whether a word is in a dictionary that is located in a csv file. --- assets/dictionary-english.csv | 1 - lib/adagrams.rb | 28 ++++++++++++++++++++++++++-- test/adagrams_test.rb | 8 ++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/assets/dictionary-english.csv b/assets/dictionary-english.csv index ce59890..809ea6c 100644 --- a/assets/dictionary-english.csv +++ b/assets/dictionary-english.csv @@ -1,4 +1,3 @@ -Word a aa aaa diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 541e00c..ed71794 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,3 +1,5 @@ +require 'csv' + # Wave 1 # Draws ten letters for the user. def draw_letters @@ -16,11 +18,26 @@ def draw_letters # Checks if the word the user inputs is available in letters in hand. def uses_available_letters?(input, letters_in_hand) # Variable holding array of letters of chosen word. - letters = input.upcase.split('') + letters = (input.upcase.split('')) + # copy_hand = letters_in_hand. + + # copy_hand.each_with_index do |copy_letter, index| + # letters.each do |letter| + # if copy_letter == letter + # copy_hand.delete_at(index) + # end + # end + # end + # if (letters_in_hand.length - copy_hand.length) == letters.length + # return true + # else + # return false + # end check_overlap = letters_in_hand & letters - check_overlap == letters ? true : false + check_overlap.sort == letters.sort ? true : false end +puts uses_available_letters?("agar", ['A', 'B', 'A', 'F', 'R', 'G']) # Wave 3 # Scores the word that the user has inputted. def score_word(word) @@ -88,4 +105,11 @@ def tiebreaker(highest_word, word) highest_word = word end return highest_word +end + +# Wave 5 +# Checks if input is a valid word in the English Dictionary. +def is_in_english_dict?(input) + dictionary = File.open('assets/dictionary-english.csv').readlines.map(&:chomp) + dictionary.include?(input) ? true : false end \ No newline at end of file diff --git a/test/adagrams_test.rb b/test/adagrams_test.rb index 90ec44d..6600de2 100644 --- a/test/adagrams_test.rb +++ b/test/adagrams_test.rb @@ -178,4 +178,12 @@ expect(best_word[:score]).must_equal 18 end end + describe "is_in_english_dict method" do + it "checks if word is in english dictionary" do + #verify that 'anything' is in the dictionary + expect(is_in_english_dict?("anything")).must_equal true + #verify that our made up word is in the dictionary + expect(is_in_english_dict?("33aer")).must_equal false + end + end end From 4d9daa78de0f6c92095f0d372ec538dd52547b4e Mon Sep 17 00:00:00 2001 From: Jessica Liang Date: Fri, 14 Feb 2020 13:56:25 -0800 Subject: [PATCH 14/14] Corrected logic in Wave 2 and corrected logic in Wave 4. --- .DS_Store | Bin 0 -> 6148 bytes lib/adagrams.rb | 29 ++++++++++++++++------------- 2 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..84f56612f1a27cb61eff36a7ae198c6a185c30e1 GIT binary patch literal 6148 zcmeHKISv9b4733uBpOP}e1RWC2wuPkKmySqkhtotco$D&d^E7oL4(FCXA;Mg)lLzw zMMS5U^+aSOA_KUg+-zu@?VGo(lMw~NamHCr*Tr$aJM6cu?DqlV_GKj}JewE3?a`buj$yu{@0mP=P;C!0v|vH>`J2f3w#N;oIBhMbEjbNat!oxjD?lsu_r}du{ri@ VViV|e#GMZ0&w%MdqXNHH-~qms6~O=i literal 0 HcmV?d00001 diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 541e00c..dce8060 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -9,18 +9,25 @@ def draw_letters 10.times do hand << letters_pool.sample end + return hand end # Wave 2 # Checks if the word the user inputs is available in letters in hand. def uses_available_letters?(input, letters_in_hand) - # Variable holding array of letters of chosen word. - letters = input.upcase.split('') - check_overlap = letters_in_hand & letters - check_overlap == letters ? true : false + input_letters = input.upcase.split('') # Variable holding array of letters of chosen word. + + letters_in_hand.each do |letter| + if input_letters.include?(letter) + input_letters.delete_at(input_letters.index(letter)) + end + end + + input_letters.empty? ? true : false end +uses_available_letters?('riddle', ['R','I','Y','K','D','L','E','X']) # Wave 3 # Scores the word that the user has inputted. def score_word(word) @@ -54,16 +61,12 @@ def score_word(word) # Wave 4 # Chooses the highest scoring word from all words inputted. def highest_score_from(words) - # Variable tracking each word and its respective score. - words_hash = {} highest_word = "" # Variable tracking word that scores the highest. highest_scored = 0 # Variable tracking the highest score. words.each do |word| - words_hash[word] = score_word(word) - end + score = score_word(word) - words_hash.each do |word, score| if score > highest_scored highest_scored = score highest_word = word @@ -72,20 +75,20 @@ def highest_score_from(words) highest_word = tiebreaker(highest_word, word) end end + return {word: highest_word, score: highest_scored} end # Breaks the tie. def tiebreaker(highest_word, word) case - when word.length == 10 && highest_word.length != 10 - highest_word = word when highest_word.length == 10 && word.length != 10 highest_word - when word.length == 10 && highest_word.length == 10 - highest_word + when word.length == 10 && highest_word.length != 10 + highest_word = word when word.length < highest_word.length highest_word = word end + return highest_word end \ No newline at end of file