From fda43807224d4aa24c77a2876a7bea99e5748643 Mon Sep 17 00:00:00 2001 From: Lauren Date: Wed, 12 Feb 2020 16:47:58 -0800 Subject: [PATCH 1/5] terrible commit bc we've done a fuck ton already --- lib/adagrams.rb | 139 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..2a3f70c 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,139 @@ +# Wave 1 + +# create method draw_letters +# no parameters, returns array of 10 strings, each 1 letter player has drawn randomly + + +def draw_letters + rand_letters = [] + letters = ["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"] + rand_letters = letters.sample(10) + return rand_letters +end + +# print draw_letters + +# 1st attempt wave 2 +# def uses_available_letters?(input, letters_in_hand) # input = what the user enters; letters_in_hand = random letters from previous method +# # if letters in hand includes all letters in input +# # and the counts of letters in inputs is less than +# # or equal to the counts of letters in letters_in_hand +# # returns true +# split_input = input.downcase.split(//) + +# split_input.each do |letter| +# letter_count = letter.count +# letter_in_hand_count = letter.count +# if letter_count <= letter_in_hand_count +# return true +# else +# return false +# end +# end + + +# check_input_letters = (split_input - letters_in_hand) +# if check_input_letters.empty? # && check_input_quantity +# return true +# end + + + +# end + +# Wave 2 +def uses_available_letters?(input, letters_in_hand) + letters_in_hand_copy = letters_in_hand.dup + input.split('').each do |letter| + if letters_in_hand_copy.include? (letter) + letters_in_hand_copy.slice!(letters_in_hand_copy.index(letter)) + else + return false + end + end + return true +end + + +# Wave 3 + +def score_word(word) + score = 0 + if word.length > 6 + score += 8 + end + word = word.upcase.split('') + # split word into characters + word.each do |letter| + # use a c statement to add points to score variable + case letter + 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 + return score +end + +# Wave 4 +def highest_score_from(words) +high_score_hash = {} +high_score = 0 +winning_word = "" +tie_breaking_array = [] + + words.each do |word| + if score_word(word) > high_score + high_score = score_word(word) + winning_word = word + end + end + + words.each do |word| + if score_word(word) == high_score + tie_breaking_array << word + end + end + + ten_letter_words = [] + shortest_words = [] + tie_breaking_array.each do |word| + + if word.length == 10 + ten_letter_words << word + + shortest_words << shortest_words.min + end + end + + if ten_letter_words.empty? + winning_word = shortest_words.first + end + + high_score_hash[:score] = high_score + high_score_hash[:word] = winning_word + return high_score_hash +end + + + +# if tie_breaking_array[0].length > tie_breaking_array[1].length +# winning_word = tie_breaking_array[1] +# elsif tie_breaking_array[1].length > tie_breaking_array[0].length +# winning_word = tie_breaking_array[0] +# elsif tie_breaking_array[0].length == 10 +# winning_word = tie_breaking_array[0] +# elsif tie_breaking_array[1].length == 10 +# winning_word = tie_breaking_array[1] +# end \ No newline at end of file From 5b6f78fa92d539db3e3cac9cece9754c7edf2e4d Mon Sep 17 00:00:00 2001 From: Leah Hughes Date: Thu, 13 Feb 2020 16:35:36 -0800 Subject: [PATCH 2/5] WE FUCKING DID IT --- lib/adagrams.rb | 208 ++++++++++++++++++++++++++++++------------------ 1 file changed, 131 insertions(+), 77 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 2a3f70c..46de092 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,9 +1,4 @@ -# Wave 1 - -# create method draw_letters -# no parameters, returns array of 10 strings, each 1 letter player has drawn randomly - - +# Wave 1 - creates letter hand def draw_letters rand_letters = [] letters = ["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"] @@ -11,37 +6,7 @@ def draw_letters return rand_letters end -# print draw_letters - -# 1st attempt wave 2 -# def uses_available_letters?(input, letters_in_hand) # input = what the user enters; letters_in_hand = random letters from previous method -# # if letters in hand includes all letters in input -# # and the counts of letters in inputs is less than -# # or equal to the counts of letters in letters_in_hand -# # returns true -# split_input = input.downcase.split(//) - -# split_input.each do |letter| -# letter_count = letter.count -# letter_in_hand_count = letter.count -# if letter_count <= letter_in_hand_count -# return true -# else -# return false -# end -# end - - -# check_input_letters = (split_input - letters_in_hand) -# if check_input_letters.empty? # && check_input_quantity -# return true -# end - - - -# end - -# Wave 2 +# Wave 2 - checks that input is valid def uses_available_letters?(input, letters_in_hand) letters_in_hand_copy = letters_in_hand.dup input.split('').each do |letter| @@ -54,9 +19,7 @@ def uses_available_letters?(input, letters_in_hand) return true end - -# Wave 3 - +# Wave 3 - scores input def score_word(word) score = 0 if word.length > 6 @@ -86,13 +49,52 @@ def score_word(word) return score end -# Wave 4 -def highest_score_from(words) -high_score_hash = {} -high_score = 0 -winning_word = "" -tie_breaking_array = [] +# returns a hash of word scores +# may be useful in determining when to run tie_breaker +# if score_storage_hash.values != score_storage_hash.values.uniq, +# then run tie_breaker? +def word_scores(words) + score_storage_hash = {} + words.each do |word| + score_storage_hash[word] = score_word(word) + end + return score_storage_hash +end + +# possible tie break method to invoke in highest_score_from method? +# ALL THE SCORES IN HERE ARE THE SAME SO THE SCORE DOES NOT MATTER +def tie_breaker(words) + ten_letter_word_array = [] + tie_scores_array = [] + winning_hash = {} + + word_scores(words).each do |word, score| + if word.length == 10 + ten_letter_word_array << word + else + tie_scores_array << word + end + + if ten_letter_word_array.count > 0 + winning_hash[:word] = ten_letter_word_array[0] + winning_hash[:score] = score + else + winning_hash[:word] = tie_scores_array.min_by { |word| word.length } + winning_hash[:score] = score + end + end + + return winning_hash +end +# Wave 4 - determines winning word, accounting for ties +def highest_score_from(words) + high_score_hash = {} + high_score = 0 + winning_word = "" + tie_breaking_array = [] + + # look at each word, invoke score_word method, set highest score as winning_word words.each do |word| if score_word(word) > high_score high_score = score_word(word) @@ -100,40 +102,92 @@ def highest_score_from(words) end end - words.each do |word| - if score_word(word) == high_score - tie_breaking_array << word - end + if word_scores(words).values != word_scores(words).values.uniq + # invoke tie_breaking method + # if there are tied scores, + # invoke the tie score method on the words hash + tie_winner = tie_breaker(words) # this is a hash + winning_word = tie_winner[:word] + high_score = tie_winner[:score] end - - ten_letter_words = [] - shortest_words = [] - tie_breaking_array.each do |word| - - if word.length == 10 - ten_letter_words << word - shortest_words << shortest_words.min - end - end - - if ten_letter_words.empty? - winning_word = shortest_words.first - end - high_score_hash[:score] = high_score - high_score_hash[:word] = winning_word + high_score_hash[:word] = winning_word return high_score_hash end + +# words = ['WWW', 'MMMM'] +# best_word = highest_score_from words - - -# if tie_breaking_array[0].length > tie_breaking_array[1].length -# winning_word = tie_breaking_array[1] -# elsif tie_breaking_array[1].length > tie_breaking_array[0].length -# winning_word = tie_breaking_array[0] -# elsif tie_breaking_array[0].length == 10 -# winning_word = tie_breaking_array[0] -# elsif tie_breaking_array[1].length == 10 -# winning_word = tie_breaking_array[1] -# end \ No newline at end of file +# puts best_word + + + + + # if tie_breaking_array[0].length > tie_breaking_array[1].length + # winning_word = tie_breaking_array[1] + # elsif tie_breaking_array[1].length > tie_breaking_array[0].length + # winning_word = tie_breaking_array[0] + # elsif tie_breaking_array[0].length == 10 + # winning_word = tie_breaking_array[0] + # elsif tie_breaking_array[1].length == 10 + # winning_word = tie_breaking_array[1] + # end + + # print draw_letters + + # 1st attempt wave 2 + # def uses_available_letters?(input, letters_in_hand) # input = what the user enters; letters_in_hand = random letters from previous method + # # if letters in hand includes all letters in input + # # and the counts of letters in inputs is less than + # # or equal to the counts of letters in letters_in_hand + # # returns true + # split_input = input.downcase.split(//) + + # split_input.each do |letter| + # letter_count = letter.count + # letter_in_hand_count = letter.count + # if letter_count <= letter_in_hand_count + # return true + # else + # return false + # end + # end + + + # check_input_letters = (split_input - letters_in_hand) + # if check_input_letters.empty? # && check_input_quantity + # return true + # end + + + + # end + + #Things we deleted that were or were not working :) + # look at each word again, if there are any words that have a + # score equal to the high score, store those in a tie breaking array + # should we make a new "tie break" method? + # words.each do |word| + # if score_word(word) == high_score + # tie_breaking_array << word + # end + # end + + # # need to add logic that only runs this if tie_breaking_array + # # length is greater than 1 (which may be a good reason to make another method! + # ten_letter_words = [] + # shortest_words = [] # this would be more clear as tie_score_words + # tie_breaking_array.each do |word| + + # if word.length == 10 + # ten_letter_words << word + + # shortest_words << shortest_words.min + # end + # end + + # if ten_letter_words[0] (# is truthy) + # winning_word = ten_letter_word[0] + # else winning_word = shortest_words.min_by { |word| word.length } + # end \ No newline at end of file From 21b7e40c327bea9f8a90dc427cbff8674361f09f Mon Sep 17 00:00:00 2001 From: Leah Hughes Date: Fri, 14 Feb 2020 14:35:29 -0800 Subject: [PATCH 3/5] correcting comments --- lib/adagrams.rb | 112 ++++++++---------------------------------------- 1 file changed, 19 insertions(+), 93 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 46de092..7ad7b93 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -26,9 +26,9 @@ def score_word(word) score += 8 end word = word.upcase.split('') - # split word into characters + word.each do |letter| - # use a c statement to add points to score variable + case letter when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" score += 1 @@ -46,13 +46,11 @@ def score_word(word) score += 10 end end + return score end -# returns a hash of word scores -# may be useful in determining when to run tie_breaker -# if score_storage_hash.values != score_storage_hash.values.uniq, -# then run tie_breaker? +# Wave 4 - method for storing all user input def word_scores(words) score_storage_hash = {} words.each do |word| @@ -61,8 +59,7 @@ def word_scores(words) return score_storage_hash end -# possible tie break method to invoke in highest_score_from method? -# ALL THE SCORES IN HERE ARE THE SAME SO THE SCORE DOES NOT MATTER +# Wave 4 - method for breaking ties def tie_breaker(words) ten_letter_word_array = [] tie_scores_array = [] @@ -87,21 +84,13 @@ def tie_breaker(words) return winning_hash end -# Wave 4 - determines winning word, accounting for ties +# Wave 4 - determines highest score, and if needed invokes tie_breaker def highest_score_from(words) high_score_hash = {} - high_score = 0 - winning_word = "" - tie_breaking_array = [] - - # look at each word, invoke score_word method, set highest score as winning_word - words.each do |word| - if score_word(word) > high_score - high_score = score_word(word) - winning_word = word - end - end + high_score = word_scores(words).max_by { |word, score| score }[1] + winning_word = word_scores(words).max_by { |word, score| score }[0] + if word_scores(words).values != word_scores(words).values.uniq # invoke tie_breaking method # if there are tied scores, @@ -116,78 +105,15 @@ def highest_score_from(words) return high_score_hash end -# words = ['WWW', 'MMMM'] -# best_word = highest_score_from words - -# puts best_word - - - - - # if tie_breaking_array[0].length > tie_breaking_array[1].length - # winning_word = tie_breaking_array[1] - # elsif tie_breaking_array[1].length > tie_breaking_array[0].length - # winning_word = tie_breaking_array[0] - # elsif tie_breaking_array[0].length == 10 - # winning_word = tie_breaking_array[0] - # elsif tie_breaking_array[1].length == 10 - # winning_word = tie_breaking_array[1] - # end - # print draw_letters - - # 1st attempt wave 2 - # def uses_available_letters?(input, letters_in_hand) # input = what the user enters; letters_in_hand = random letters from previous method - # # if letters in hand includes all letters in input - # # and the counts of letters in inputs is less than - # # or equal to the counts of letters in letters_in_hand - # # returns true - # split_input = input.downcase.split(//) - - # split_input.each do |letter| - # letter_count = letter.count - # letter_in_hand_count = letter.count - # if letter_count <= letter_in_hand_count - # return true - # else - # return false - # end - # end - - - # check_input_letters = (split_input - letters_in_hand) - # if check_input_letters.empty? # && check_input_quantity - # return true - # end - - - - # end - - #Things we deleted that were or were not working :) - # look at each word again, if there are any words that have a - # score equal to the high score, store those in a tie breaking array - # should we make a new "tie break" method? - # words.each do |word| - # if score_word(word) == high_score - # tie_breaking_array << word - # end - # end - - # # need to add logic that only runs this if tie_breaking_array - # # length is greater than 1 (which may be a good reason to make another method! - # ten_letter_words = [] - # shortest_words = [] # this would be more clear as tie_score_words - # tie_breaking_array.each do |word| - - # if word.length == 10 - # ten_letter_words << word - - # shortest_words << shortest_words.min - # end - # end + high_score = 0 + winning_word = "" + tie_breaking_array = [] - # if ten_letter_words[0] (# is truthy) - # winning_word = ten_letter_word[0] - # else winning_word = shortest_words.min_by { |word| word.length } - # end \ No newline at end of file + look at each word, invoke score_word method, set highest score as winning_word + words.each do |word| + if score_word(word) > high_score + high_score = score_word(word) + winning_word = word + end + end From 84b920d02ec947edee6e14420ad309a45e7f2fe3 Mon Sep 17 00:00:00 2001 From: Lauren Date: Fri, 14 Feb 2020 14:45:11 -0800 Subject: [PATCH 4/5] deleted lines 95-97 of comments, refactored part of highest_score_from --- lib/adagrams.rb | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 7ad7b93..9418a4d 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -92,10 +92,7 @@ def highest_score_from(words) winning_word = word_scores(words).max_by { |word, score| score }[0] if word_scores(words).values != word_scores(words).values.uniq - # invoke tie_breaking method - # if there are tied scores, - # invoke the tie score method on the words hash - tie_winner = tie_breaker(words) # this is a hash + tie_winner = tie_breaker(words) winning_word = tie_winner[:word] high_score = tie_winner[:score] end @@ -106,14 +103,14 @@ def highest_score_from(words) end - high_score = 0 - winning_word = "" - tie_breaking_array = [] + # high_score = 0 + # winning_word = "" + # tie_breaking_array = [] - look at each word, invoke score_word method, set highest score as winning_word - words.each do |word| - if score_word(word) > high_score - high_score = score_word(word) - winning_word = word - end - end + # look at each word, invoke score_word method, set highest score as winning_word + # words.each do |word| + # if score_word(word) > high_score + # high_score = score_word(word) + # winning_word = word + # end + # end From d01fbb19211ccab5db6ebb1a42df96852632fc21 Mon Sep 17 00:00:00 2001 From: Leah Hughes Date: Fri, 14 Feb 2020 14:49:42 -0800 Subject: [PATCH 5/5] Final cleaned up version with no unnecessary comments and no swearing. --- lib/adagrams.rb | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 9418a4d..912c75f 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -100,17 +100,4 @@ def highest_score_from(words) high_score_hash[:score] = high_score high_score_hash[:word] = winning_word return high_score_hash -end - - - # high_score = 0 - # winning_word = "" - # tie_breaking_array = [] - - # look at each word, invoke score_word method, set highest score as winning_word - # words.each do |word| - # if score_word(word) > high_score - # high_score = score_word(word) - # winning_word = word - # end - # end +end \ No newline at end of file