From 454645c9376300c0e764f80bb748ecb76df1e765 Mon Sep 17 00:00:00 2001 From: Catherina Date: Mon, 10 Feb 2020 16:37:24 -0800 Subject: [PATCH 1/7] wave 1 draw_letters method created --- adagrams.rb | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb new file mode 100644 index 0000000..57d9a64 --- /dev/null +++ b/adagrams.rb @@ -0,0 +1,60 @@ +# Wave 1 + +def draw_letters + pool = { + A: Array.new(9, "A"), + B: Array.new(2, "B"), + C: Array.new(2, "C"), + D: Array.new(4, "D"), + E: Array.new(12, "E"), + F: Array.new(2, "F"), + G: Array.new(3, "G"), + H: Array.new(2, "H"), + I: Array.new(9, "I"), + J: Array.new(1, "J"), + K: Array.new(1, "K"), + L: Array.new(4, "L"), + M: Array.new(2, "M"), + N: Array.new(6, "N"), + O: Array.new(8, "O"), + P: Array.new(2, "P"), + Q: Array.new(1, "Q"), + R: Array.new(6, "R"), + S: Array.new(4, "S"), + T: Array.new(6, "T"), + U: Array.new(4, "U"), + V: Array.new(2, "V"), + W: Array.new(2, "W"), + X: Array.new(1, "X"), + Y: Array.new(2, "Y"), + Z: Array.new(1, "Z") + } + + pool_values = pool.map do |key, value| + value + end + + pool_final = pool_values.flat_map do |letter| + letter + end + + user_took = [] + + 10.times do |i| + pool_final.shuffle! + user_took << pool_final.slice!(0) + end + + return user_took +end + +user_1 = draw_letters +user_2 = draw_letters + +p user_1 +p user_2 + + +# p pool_final.length +# p user_took +# p pool_final.sort From c327d2e51e4e154e63198b4f06410232257e1bb9 Mon Sep 17 00:00:00 2001 From: Catherina Date: Tue, 11 Feb 2020 15:29:32 -0800 Subject: [PATCH 2/7] Wave 2 completed --- adagrams.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/adagrams.rb b/adagrams.rb index 57d9a64..48fe6f7 100644 --- a/adagrams.rb +++ b/adagrams.rb @@ -58,3 +58,20 @@ def draw_letters # p pool_final.length # p user_took # p pool_final.sort + + +# Wave 2. +def uses_available_letters?(input, letters_in_hand) + input = input.split("") + + letters_in_hand.each do |letter| + i = 0 + if i == input.index(letter) + input.slice!(i) + end + + i += 1 + end + + return input.empty? +end \ No newline at end of file From 495051f4b2386c4c36e16df5881dd618b9f45479 Mon Sep 17 00:00:00 2001 From: Catherina Date: Tue, 11 Feb 2020 15:52:36 -0800 Subject: [PATCH 3/7] wave 3 done --- adagrams.rb | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/adagrams.rb b/adagrams.rb index 48fe6f7..19329c9 100644 --- a/adagrams.rb +++ b/adagrams.rb @@ -74,4 +74,38 @@ def uses_available_letters?(input, letters_in_hand) end return input.empty? -end \ No newline at end of file +end + +# Wave 3 +def score_word(word) + + score_table = { + "1" => %w[A B E I O L N R S T], + "2" => %w[D G], + "3" => %w[B C M P], + "4" => %w[F H V W Y], + "5" => %w[K], + "8" => %w[J X], + "10" => %w[Q Z] + } + + word_array = word.split("") + word_score = [] + + word_array.each do |char| + score_table.each do |key,value| + if value.include?(char) + word_score << key.to_i + end + end + end + + calculated_points = word_score.reduce(:+) + + if word_array.length >= 7 + calculated_points += 8 + end + + return calculated_points +end + From 353c6f3a995c2669dc3bc8b250a02b394a530f24 Mon Sep 17 00:00:00 2001 From: Catherina Date: Wed, 12 Feb 2020 09:31:39 -0800 Subject: [PATCH 4/7] Moved adagram file to lib, and updated waves 2 and 3. --- adagrams.rb | 111 ------------------------------ lib/adagrams.rb | 178 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 111 deletions(-) delete mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb deleted file mode 100644 index 19329c9..0000000 --- a/adagrams.rb +++ /dev/null @@ -1,111 +0,0 @@ -# Wave 1 - -def draw_letters - pool = { - A: Array.new(9, "A"), - B: Array.new(2, "B"), - C: Array.new(2, "C"), - D: Array.new(4, "D"), - E: Array.new(12, "E"), - F: Array.new(2, "F"), - G: Array.new(3, "G"), - H: Array.new(2, "H"), - I: Array.new(9, "I"), - J: Array.new(1, "J"), - K: Array.new(1, "K"), - L: Array.new(4, "L"), - M: Array.new(2, "M"), - N: Array.new(6, "N"), - O: Array.new(8, "O"), - P: Array.new(2, "P"), - Q: Array.new(1, "Q"), - R: Array.new(6, "R"), - S: Array.new(4, "S"), - T: Array.new(6, "T"), - U: Array.new(4, "U"), - V: Array.new(2, "V"), - W: Array.new(2, "W"), - X: Array.new(1, "X"), - Y: Array.new(2, "Y"), - Z: Array.new(1, "Z") - } - - pool_values = pool.map do |key, value| - value - end - - pool_final = pool_values.flat_map do |letter| - letter - end - - user_took = [] - - 10.times do |i| - pool_final.shuffle! - user_took << pool_final.slice!(0) - end - - return user_took -end - -user_1 = draw_letters -user_2 = draw_letters - -p user_1 -p user_2 - - -# p pool_final.length -# p user_took -# p pool_final.sort - - -# Wave 2. -def uses_available_letters?(input, letters_in_hand) - input = input.split("") - - letters_in_hand.each do |letter| - i = 0 - if i == input.index(letter) - input.slice!(i) - end - - i += 1 - end - - return input.empty? -end - -# Wave 3 -def score_word(word) - - score_table = { - "1" => %w[A B E I O L N R S T], - "2" => %w[D G], - "3" => %w[B C M P], - "4" => %w[F H V W Y], - "5" => %w[K], - "8" => %w[J X], - "10" => %w[Q Z] - } - - word_array = word.split("") - word_score = [] - - word_array.each do |char| - score_table.each do |key,value| - if value.include?(char) - word_score << key.to_i - end - end - end - - calculated_points = word_score.reduce(:+) - - if word_array.length >= 7 - calculated_points += 8 - end - - return calculated_points -end - diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..fa59502 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,178 @@ +# Wave 1 + +def draw_letters + pool = { + A: Array.new(9, "A"), + B: Array.new(2, "B"), + C: Array.new(2, "C"), + D: Array.new(4, "D"), + E: Array.new(12, "E"), + F: Array.new(2, "F"), + G: Array.new(3, "G"), + H: Array.new(2, "H"), + I: Array.new(9, "I"), + J: Array.new(1, "J"), + K: Array.new(1, "K"), + L: Array.new(4, "L"), + M: Array.new(2, "M"), + N: Array.new(6, "N"), + O: Array.new(8, "O"), + P: Array.new(2, "P"), + Q: Array.new(1, "Q"), + R: Array.new(6, "R"), + S: Array.new(4, "S"), + T: Array.new(6, "T"), + U: Array.new(4, "U"), + V: Array.new(2, "V"), + W: Array.new(2, "W"), + X: Array.new(1, "X"), + Y: Array.new(2, "Y"), + Z: Array.new(1, "Z") + } + + pool_values = pool.map do |key, value| + value + end + + pool_final = pool_values.flat_map do |letter| + letter + end + + user_took = [] + + 10.times do |i| + pool_final.shuffle! + user_took << pool_final.slice!(0) + end + + return user_took +end + +user_1 = draw_letters +user_2 = draw_letters + +p user_1 +p user_2 + + +# p pool_final.length +# p user_took +# p pool_final.sort + + +# Wave 2. +def uses_available_letters?(input, letters_in_hand) + input = input.split("") + + letters_in_hand.each do |letter| + if input.index(letter) + input.slice!(input.index(letter)) + end + end + + return input.empty? +end + +# Wave 3 +def score_word(word) + + word = word.upcase + + score_table = { + "1" => %w[A E I O U L N R S T], + "2" => %w[D G], + "3" => %w[B C M P], + "4" => %w[F H V W Y], + "5" => %w[K], + "8" => %w[J X], + "10" => %w[Q Z] + } + + if word == "" + return 0 + end + + word_array = word.split("") + word_score = [] + + word_array.each do |char| + score_table.each do |key,value| + if value.include?(char) + word_score << key.to_i + end + end + end + + calculated_points = word_score.reduce(:+) + + if word_array.length >= 7 + calculated_points += 8 + end + + return calculated_points +end + +# Wave 4: + +# def highest_score_from(words) +# end + +# words = ["CAT", "CD", "AA"] + +# word_score_list = [] + +# words.each do |word| +# word_score_list << score_word(word) +# end + +# winner_index = [] + +# word_score_list.each_with_index do |score, index| +# if word_score_list.max == score +# winner_index << index +# end +# end + +# winning_word = {} + +# if winner_index.length > 1 + +# winner_index.each do |index_location| +# if words[index_location].length == 10 +# winning_word[:word] = words[index_location] +# winning_word[:score] = word_score_list[index_location] +# end +# end + + +# end + + + +# p winner_index + + +# p word_score_list + +# p word_score_list.max + +# p word_score_list.index(word_score_list.max) + + + +# ############ + +# # words = ["CAT", "MOUSE", "MICE"] + +# # words_with_scores = [] +# # words.each do |word| +# # current_word = {} +# # current_word[:word] = word +# # current_word[:score] = score_word(word) +# # words_with_scores << current_word +# # end + +# # p words_with_scores + + + From bc8294a7f250c8984e427515447723bdfdfc0984 Mon Sep 17 00:00:00 2001 From: Catherina Date: Wed, 12 Feb 2020 15:10:49 -0800 Subject: [PATCH 5/7] Wave 4 added --- lib/adagrams.rb | 78 +++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 52 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index fa59502..3d7bc48 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -113,66 +113,40 @@ def score_word(word) end # Wave 4: +def highest_score_from(words) + + data = [] + words.each do |word| + info = {} + info[:word] = word + info[:score] = score_word(word) + info[:word_length] = word.length + data << info + end -# def highest_score_from(words) -# end - -# words = ["CAT", "CD", "AA"] - -# word_score_list = [] - -# words.each do |word| -# word_score_list << score_word(word) -# end - -# winner_index = [] - -# word_score_list.each_with_index do |score, index| -# if word_score_list.max == score -# winner_index << index -# end -# end - -# winning_word = {} - -# if winner_index.length > 1 - -# winner_index.each do |index_location| -# if words[index_location].length == 10 -# winning_word[:word] = words[index_location] -# winning_word[:score] = word_score_list[index_location] -# end -# end - - -# end - - - -# p winner_index - - -# p word_score_list - -# p word_score_list.max + max_score_hash = data.max_by { |word_info| word_info[:score] } + max_score = max_score_hash[:score] -# p word_score_list.index(word_score_list.max) + ties_candidate = data.select { |word_info| word_info[:score] == max_score } + if ties_candidate.length > 1 + word_length_10_candidates = ties_candidate.select { |word_info| word_info[:word_length] == 10} -# ############ + if word_length_10_candidates.length > 0 -# # words = ["CAT", "MOUSE", "MICE"] + return word_length_10_candidates[0].slice(:word, :score) + else + winner = ties_candidate.min_by { |word_info| word_info[:word_length]} -# # words_with_scores = [] -# # words.each do |word| -# # current_word = {} -# # current_word[:word] = word -# # current_word[:score] = score_word(word) -# # words_with_scores << current_word -# # end + return winner.slice(:word, :score) + end -# # p words_with_scores + else + winner = max_score_hash.slice(:word, :score) + return winner + end +end From bade3ad1247648ac2faf59ea6fed06efbe9a8bf4 Mon Sep 17 00:00:00 2001 From: Catherina Date: Thu, 13 Feb 2020 15:16:48 -0800 Subject: [PATCH 6/7] Wave 5 working and wave 1 updated with upcase --- lib/adagrams.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 3d7bc48..0f09336 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,3 +1,7 @@ + +require "awesome_print" +require "csv" + # Wave 1 def draw_letters @@ -62,7 +66,7 @@ def draw_letters # Wave 2. def uses_available_letters?(input, letters_in_hand) - input = input.split("") + input = input.upcase.split("") letters_in_hand.each do |letter| if input.index(letter) @@ -150,3 +154,18 @@ def highest_score_from(words) end +# Wave 5. + + + +def is_in_english_dict?(input) + + dictionary = CSV.read("../assets/dictionary-english.csv") + input = input.downcase.split + + return dictionary.include?(input) +end + + +# test = is_english_dict?('Cat') +# p test \ No newline at end of file From 97b5c88b15f0bb891495016544f51db7efff681c Mon Sep 17 00:00:00 2001 From: Catherina Date: Thu, 13 Feb 2020 16:33:01 -0800 Subject: [PATCH 7/7] Wave 5 fixed, added dictionary path. --- lib/adagrams.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 0f09336..3340c04 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -156,11 +156,10 @@ def highest_score_from(words) # Wave 5. - - def is_in_english_dict?(input) - dictionary = CSV.read("../assets/dictionary-english.csv") + dictionary_path = File.join(File.dirname(__FILE__), "../assets/dictionary-english.csv") + dictionary = CSV.read(dictionary_path) input = input.downcase.split return dictionary.include?(input)