From fe2b17cd67380517773a534806c4101d6b4f9702 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Mon, 12 Aug 2019 15:32:59 -0700 Subject: [PATCH 1/9] Completed wave-1 in adagrams.rb --- lib/adagrams.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..9e363f1 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,56 @@ +# Ga-Young Jin, Raisah Vesteinsdottir Cohort 12 +# Monday, August 12th, 2019 +# Week 2, Adagram Project + +# wave-1 + +SIZE_OF_HAND = 10 + +def draw_letters + letter_dist = { # given distribution of 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 + } + + # putting all available letters into one array + all_letters = [] + + letter_dist.each do |letter, quantity| + quantity.times do + all_letters << letter.to_s + end + end + + # create randomized hand + shuffled_all_letters = all_letters.shuffle + hand = [] + + SIZE_OF_HAND.times do |i| + hand << shuffled_all_letters[i] + end + return hand +end From c2f0ad7e6a9110c02252315ce729760cf0d61308 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Mon, 12 Aug 2019 17:15:38 -0700 Subject: [PATCH 2/9] Attempt at wave-2 in adagrams.rb --- lib/adagrams.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 9e363f1..3658069 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -54,3 +54,35 @@ def draw_letters end return hand end + +# wave-2 +def uses_available_letters?(input, letters_in_hand) + input_as_array = input.upcase.split("") + if input_as_array.length > letters_in_hand.length + return false + else + input_as_array.each do |c| + comparison = false + index = 0 + while comparison == false + if letters_in_hand[index] == c + letters_in_hand.delete_at(index) + comparison = true + elsif index == letters_in_hand.length - 1 + comparison = true + end + index += 1 + end + end + if letters_in_hand.length == SIZE_OF_HAND - input_as_array.length + return true + else + return false + end + end +end +# letters = ["A", "B", "C", "D", "E", "A"] +# string = "abz" + +# puts uses_available_letters?(string, letters) +# print letters From 81aed00b9caf0f7a5e84cc922ba00a185a1dabcb Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Tue, 13 Aug 2019 15:51:16 -0700 Subject: [PATCH 3/9] Completed wave-2 --- lib/adagrams.rb | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 3658069..ad429fe 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -58,31 +58,35 @@ def draw_letters # wave-2 def uses_available_letters?(input, letters_in_hand) input_as_array = input.upcase.split("") - if input_as_array.length > letters_in_hand.length + hand_in_hash = {} + + # determines if string is longer than size of hand + if input_as_array.length > SIZE_OF_HAND return false - else - input_as_array.each do |c| - comparison = false - index = 0 - while comparison == false - if letters_in_hand[index] == c - letters_in_hand.delete_at(index) - comparison = true - elsif index == letters_in_hand.length - 1 - comparison = true - end - index += 1 - end + end + + # converts letters_in_hand array to hash + letters_in_hand.each do |char| + if hand_in_hash.has_key?(char) == true + hand_in_hash[char] += 1 + else + hand_in_hash[char] = 1 end - if letters_in_hand.length == SIZE_OF_HAND - input_as_array.length - return true + end + + # determines if letters in string are present in hand + input_as_array.each do |char| + if hand_in_hash.has_key?(char) == true + hand_in_hash[char] -= 1 else return false end end + + if hand_in_hash.values.min < 0 + return false + else + return true + end end -# letters = ["A", "B", "C", "D", "E", "A"] -# string = "abz" -# puts uses_available_letters?(string, letters) -# print letters From 1ca9f10dce5e94f0ae9f38bb625c5261ea25b95f Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Tue, 13 Aug 2019 16:40:31 -0700 Subject: [PATCH 4/9] Completed wave-3 for adagrams.rb --- lib/adagrams.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index ad429fe..bdee068 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -90,3 +90,46 @@ def uses_available_letters?(input, letters_in_hand) end end +# wave-3 +def score_word(word) + word_as_array = word.upcase.split("") + + score_chart = { + A: 1, + E: 1, + I: 1, + O: 1, + U: 1, + L: 1, + N: 1, + R: 1, + S: 1, + T: 1, + D: 2, + G: 2, + B: 3, + C: 3, + M: 3, + P: 3, + F: 4, + H: 4, + V: 4, + W: 4, + Y: 4, + K: 5, + J: 8, + X: 8, + Q: 10, + Z: 10 + } + + # calculate score based on letters in word + score = 0 + word_as_array.each do |char| + score += score_chart[char.to_sym] + end + if word.length > 6 + score += 8 + end + return score +end \ No newline at end of file From fb5d59f7b5ded831536b0001cba9914da4671253 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Wed, 14 Aug 2019 08:54:43 -0700 Subject: [PATCH 5/9] Attempt at wave-4 --- lib/adagrams.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index bdee068..31cb851 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -132,4 +132,20 @@ def score_word(word) score += 8 end return score -end \ No newline at end of file +end + +# wave-4 + +def highest_score_from(words) + highest_score = {word: "", score: 0} + + scores = words.map do |word| + score_word(word) + end + + return scores.max +end + +words_array = ["cat", "cat", "a"] + +print highest_score_from(words_array) \ No newline at end of file From d03b7052c683e22b3fc763823afdda04ef117283 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Wed, 14 Aug 2019 14:06:02 -0700 Subject: [PATCH 6/9] Completed wave-4 in adagrams.rb --- lib/adagrams.rb | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 31cb851..fa4e4c4 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -137,15 +137,32 @@ def score_word(word) # wave-4 def highest_score_from(words) - highest_score = {word: "", score: 0} + winning_word_score = {word: "", score: 0} + highest_score = {word: [], score: 0} - scores = words.map do |word| - score_word(word) + # save highest score and word(s) into hash + words.each do |word| + score_of_word = score_word(word) + if highest_score[:score] < score_of_word + highest_score[:score] = score_of_word + highest_score[:word] = [word] + elsif highest_score[:score] == score_of_word + highest_score[:word] << word + end + end + + winning_word_score[:score] = highest_score[:score] + + if highest_score[:word].length == 1 + winning_word_score[:word] = highest_score[:word][0] + elsif highest_score[:word].length > 1 + if highest_score[:word].max_by{|x| x.length}.length == 10 + winning_word_score[:word] = highest_score[:word].max_by{|x| x.length} + else + winning_word_score[:word] = highest_score[:word].min_by{|x| x.length} + end end - return scores.max + return winning_word_score end -words_array = ["cat", "cat", "a"] - -print highest_score_from(words_array) \ No newline at end of file From 6012e5106a1d7ea0be1aee67f90178728d8e6c86 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Wed, 14 Aug 2019 14:06:59 -0700 Subject: [PATCH 7/9] Recommit completed wave-4 --- lib/adagrams.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index fa4e4c4..a6bcf5c 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -153,6 +153,7 @@ def highest_score_from(words) winning_word_score[:score] = highest_score[:score] + # determines winning word in case of score tie if highest_score[:word].length == 1 winning_word_score[:word] = highest_score[:word][0] elsif highest_score[:word].length > 1 From a9aa956d99d4ce0245254187707b97d4f12fecb2 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Wed, 14 Aug 2019 14:28:39 -0700 Subject: [PATCH 8/9] Refactored adagrams.rb --- lib/adagrams.rb | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index a6bcf5c..3798db7 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -61,9 +61,7 @@ def uses_available_letters?(input, letters_in_hand) hand_in_hash = {} # determines if string is longer than size of hand - if input_as_array.length > SIZE_OF_HAND - return false - end + return false if input_as_array.length > SIZE_OF_HAND # converts letters_in_hand array to hash letters_in_hand.each do |char| @@ -83,11 +81,7 @@ def uses_available_letters?(input, letters_in_hand) end end - if hand_in_hash.values.min < 0 - return false - else - return true - end + return hand_in_hash.values.min < 0 ? false : true end # wave-3 @@ -128,9 +122,9 @@ def score_word(word) word_as_array.each do |char| score += score_chart[char.to_sym] end - if word.length > 6 - score += 8 - end + + score += 8 if word.length > 6 + return score end From 87f81c730f187edbcb72c1bc5ec32b6af4fce4d6 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Wed, 14 Aug 2019 15:13:35 -0700 Subject: [PATCH 9/9] Completed wave-5 in adagrams.rb --- lib/adagrams.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 3798db7..7ea7005 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -2,10 +2,10 @@ # Monday, August 12th, 2019 # Week 2, Adagram Project -# wave-1 - +require 'csv' SIZE_OF_HAND = 10 +# wave-1 def draw_letters letter_dist = { # given distribution of letters A: 9, @@ -129,7 +129,6 @@ def score_word(word) end # wave-4 - def highest_score_from(words) winning_word_score = {word: "", score: 0} highest_score = {word: [], score: 0} @@ -161,3 +160,12 @@ def highest_score_from(words) return winning_word_score end +# wave-5 +# checking to see if word input is a real word +def is_in_english_dict?(input) + CSV.open("assets/dictionary-english.csv").each do |word| + return true if input == word[0] + end + + return false +end