From a5c27a1feb64e6341c9cc8281ebb6ce12af94626 Mon Sep 17 00:00:00 2001 From: Becca Date: Mon, 10 Feb 2020 17:09:16 -0800 Subject: [PATCH 01/14] Creates first version of draw_letters method --- adagrams.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb new file mode 100644 index 0000000..1bcc828 --- /dev/null +++ b/adagrams.rb @@ -0,0 +1,45 @@ +letter_distribution = { + "A" => 9, + "B" => 2, + "C" => 2, + "D" => 4, + "E" => 1, + "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 +} + +#puts letter_distribution.values_at("R") +# first stage of draw_letters + +def draw_letters(letter_distribution) + pool = [] + letter_distribution.map do |letter, tiles| + + tiles.times do + pool << letter + end + end + return pool +end + + +p draw_letters(letter_distribution) \ No newline at end of file From 5804eab30563cdafc89a3e71f9af6ddbfbac5b99 Mon Sep 17 00:00:00 2001 From: Becca Date: Tue, 11 Feb 2020 16:04:59 -0800 Subject: [PATCH 02/14] Adds code exploring sorting method on two arrays --- lib/adagrams.rb | 144 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..a915139 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,144 @@ +letter_distribution = { + "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 +} + +# method that returns shuffled hand of 10 tiles according to letter distribution hash +# def draw_letters(letter_distribution) +# pool = [] +# letter_distribution.map do |letter, tiles| +# tiles.times do +# pool << letter +# end +# end +# pool = pool.shuffle.drop(88) +# return pool + + +# end + + +# Testing to see if sorting method will work for second wave +pool = %w[B A D C] +pool = pool.sort + +puts "Input a word" +player_word = gets.chomp.upcase +player_array = Array.new(player_word.chars).sort +# player_array = player_array.sort + +puts "Are these the same?" +puts player_array +puts pool + + + + + +# puts "Here is the pool: #{draw_letters(letter_distribution)}" + +# def uses_available_letters? + + +# end + + + +# Two parameters: +# input, the first parameter, describes some input word, and is a string +# letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter +# Returns either true or false +# Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand +# Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand + + + + + + + + + + + + + + + + + +# distro_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 +# } + +# def draw_letters (distro_of_letters) +# # creates a new array filled with each alphabet letter based on distrbution of letters +# letter_pool = [] +# distro_of_letters.each do |letters,tiles| +# tiles.times do +# letter_pool << letters +# end +# end +# # loops through the shuffled letter pool named "deck" + +# puts "Here is your hand: " +# 9.times do +# deck = letter_pool.shuffle +# #used to create a random index value to select index of the letter position in "deck" +# selector = rand(0...97) +# print deck[selector] + +# end +# end + +# draw_letters(distro_of_letters) + From 7a2f8b5fc3d6cfe39b832a07e39ea7d3da5a291b Mon Sep 17 00:00:00 2001 From: Becca Date: Tue, 11 Feb 2020 20:55:54 -0800 Subject: [PATCH 03/14] Added foundation for uses_available_letters? method --- lib/adagrams.rb | 94 ++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 60 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index a915139..a9574f2 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,4 +1,5 @@ -letter_distribution = { + +$LETTERS = { "A" => 9, "B" => 2, "C" => 2, @@ -27,66 +28,39 @@ "Z" => 1 } +# Wave 1 # method that returns shuffled hand of 10 tiles according to letter distribution hash -# def draw_letters(letter_distribution) -# pool = [] -# letter_distribution.map do |letter, tiles| -# tiles.times do -# pool << letter -# end -# end -# pool = pool.shuffle.drop(88) -# return pool - - -# end - - -# Testing to see if sorting method will work for second wave -pool = %w[B A D C] -pool = pool.sort - -puts "Input a word" -player_word = gets.chomp.upcase -player_array = Array.new(player_word.chars).sort -# player_array = player_array.sort - -puts "Are these the same?" -puts player_array -puts pool - - - - - -# puts "Here is the pool: #{draw_letters(letter_distribution)}" - -# def uses_available_letters? - - -# end - - - -# Two parameters: -# input, the first parameter, describes some input word, and is a string -# letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter -# Returns either true or false -# Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand -# Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand - - - - - - - - - - - - - +def draw_letters + pool = [] + $LETTERS.map do |letter, tiles| + tiles.times do + pool << letter + end + end + pool = pool.shuffle.drop(88) + return pool +end + +# Wave 2 +# method that returns true if every letter in the input word is available in correct quantities + +def uses_available_letters?(input, letters_in_hand) + input_array = Array.new(input.chars) + + input_array.map do |letter| + puts "The letter is #{letter}" + if letters_in_hand.index(letter) != nil + puts "Found the letter!" + puts "Here is the index: #{letters_in_hand.index(letter)}" + else + puts "Letter not found" + end + end +end + +my_letters = draw_letters +puts "Here are my letters: #{my_letters}" +puts uses_available_letters?("A", my_letters) From 71283efc4de03c6e3f492af41c8b3f61d1664684 Mon Sep 17 00:00:00 2001 From: Becca Date: Tue, 11 Feb 2020 21:10:49 -0800 Subject: [PATCH 04/14] Wave 2 tests passing --- lib/adagrams.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index a9574f2..327ccc3 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -46,16 +46,18 @@ def draw_letters def uses_available_letters?(input, letters_in_hand) input_array = Array.new(input.chars) - + current_hand = Array.new(letters_in_hand) input_array.map do |letter| puts "The letter is #{letter}" - if letters_in_hand.index(letter) != nil + if current_hand.index(letter) != nil puts "Found the letter!" - puts "Here is the index: #{letters_in_hand.index(letter)}" + puts "Here is the index: #{current_hand.index(letter)}" + current_hand.delete_at(current_hand.index(letter)) else - puts "Letter not found" + return false end end + return true end my_letters = draw_letters From 154473728ca5c4f10f09facab8d7590d5699ecf1 Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Wed, 12 Feb 2020 14:58:59 -0800 Subject: [PATCH 05/14] Passing Wave 3 score_word method tests --- lib/adagrams.rb | 93 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 327ccc3..6bde133 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -43,7 +43,6 @@ def draw_letters # Wave 2 # method that returns true if every letter in the input word is available in correct quantities - def uses_available_letters?(input, letters_in_hand) input_array = Array.new(input.chars) current_hand = Array.new(letters_in_hand) @@ -53,6 +52,7 @@ def uses_available_letters?(input, letters_in_hand) puts "Found the letter!" puts "Here is the index: #{current_hand.index(letter)}" current_hand.delete_at(current_hand.index(letter)) + puts "Here is the array now: #{current_hand}" else return false end @@ -62,11 +62,100 @@ def uses_available_letters?(input, letters_in_hand) my_letters = draw_letters puts "Here are my letters: #{my_letters}" -puts uses_available_letters?("A", my_letters) +puts uses_available_letters?("EAIIIIIAAARR", my_letters) + +# Wave 3 +# Method that calculates word score according to Adagrams rules + +$LETTER_VALUES = { + "A" => 1, + "B" => 3, + "C" => 3, + "D" => 2, + "E" => 1, + "F" => 4, + "G" => 2, + "H" => 4, + "I" => 1, + "J" => 8, + "K" => 5, + "L" => 1, + "M" => 3, + "N" => 1, + "O" => 1, + "P" => 3, + "Q" => 10, + "R" => 1, + "S" => 1, + "T" => 1, + "U" => 1, + "V" => 4, + "W" => 4, + "X" => 8, + "Y" => 4, + "Z" => 10 +} + +def score_word(word) + total_points = 0 + word_array = Array.new(word.upcase.chars) + points = word_array.map do |char| + $LETTER_VALUES.fetch_values(char) + end + total_points = points.flatten.sum + if word_array.length >= 7 + total_points += 8 + end + return total_points +end + +puts score_word("Shonda") + + +# NOT WORKING CODE: +# puts word_array +# puts "B: #{letter_values[:B]}" +# letter_values.map do |letter, val| +# char_value = 0 + # word_array.map do |char| + # sum = 0 + # puts "The first sum is: #{sum}" + # letter_values.map do |letter, value| + # letter = letter.to_s + # puts "Letter: #{letter} ; value: #{value}" + # if letter == char + # puts "The char and letter are the same!" + # puts "Curr val is: #{value}" + # sum = sum + value + # else + # puts "Not the same" + # end + # end + # return sum + # end +# try switching loop order +# sum = 0 +# letter_values.map do |letter, value| +# letter = letter.to_s +# puts "The first sum is: #{sum}" +# word_array.map do |char| +# puts "Letter: #{letter} ; value: #{value}" +# if letter == char +# puts "The char and letter are the same!" +# puts "Curr val is: #{value}" +# sum += value +# else +# puts "Not the same" +# end +# puts "Sum is: #{sum}" +# end +# return sum +# end + # distro_of_letters = { # "A": 9, # "B": 2, From b62c5e6d079340baf65de0181fd9898d57d91600 Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Wed, 12 Feb 2020 17:17:55 -0800 Subject: [PATCH 06/14] Experimenting with arrays, hashes --- lib/adagrams.rb | 186 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 124 insertions(+), 62 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 6bde133..9630be5 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,68 +1,68 @@ -$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 -} +# $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 +# } -# Wave 1 -# method that returns shuffled hand of 10 tiles according to letter distribution hash -def draw_letters - pool = [] - $LETTERS.map do |letter, tiles| - tiles.times do - pool << letter - end - end - pool = pool.shuffle.drop(88) - return pool -end +# # Wave 1 +# # method that returns shuffled hand of 10 tiles according to letter distribution hash +# def draw_letters +# pool = [] +# $LETTERS.map do |letter, tiles| +# tiles.times do +# pool << letter +# end +# end +# pool = pool.shuffle.drop(88) +# return pool +# end -# Wave 2 -# method that returns true if every letter in the input word is available in correct quantities -def uses_available_letters?(input, letters_in_hand) - input_array = Array.new(input.chars) - current_hand = Array.new(letters_in_hand) - input_array.map do |letter| - puts "The letter is #{letter}" - if current_hand.index(letter) != nil - puts "Found the letter!" - puts "Here is the index: #{current_hand.index(letter)}" - current_hand.delete_at(current_hand.index(letter)) - puts "Here is the array now: #{current_hand}" - else - return false - end - end - return true -end +# # Wave 2 +# # method that returns true if every letter in the input word is available in correct quantities +# def uses_available_letters?(input, letters_in_hand) +# input_array = Array.new(input.chars) +# current_hand = Array.new(letters_in_hand) +# input_array.map do |letter| +# puts "The letter is #{letter}" +# if current_hand.index(letter) != nil +# puts "Found the letter!" +# puts "Here is the index: #{current_hand.index(letter)}" +# current_hand.delete_at(current_hand.index(letter)) +# puts "Here is the array now: #{current_hand}" +# else +# return false +# end +# end +# return true +# end -my_letters = draw_letters -puts "Here are my letters: #{my_letters}" -puts uses_available_letters?("EAIIIIIAAARR", my_letters) +# my_letters = draw_letters +# puts "Here are my letters: #{my_letters}" +# puts uses_available_letters?("EAIIIIIAAARR", my_letters) # Wave 3 # Method that calculates word score according to Adagrams rules @@ -109,7 +109,69 @@ def score_word(word) return total_points end -puts score_word("Shonda") + + +# Wave 4 +# 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. + +# Has one parameter: words, which is an array of strings +# Returns a single hash that represents the data of a winning word and its score. The hash should have the following keys: +# :word, whose value is a string of a word +# :score, whose value is the score of that word +# 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 + +words = %w[shonda becca cat dog] + +def highest_score_from(words) + scores = [] + all_words = [] + highest_word = "" + our_hash = {} + + words.each do |word| + #scores << score_word(word) + #scores.map do |score| + our_hash[:word] = word + our_hash[:score] = score_word(word) + all_words << our_hash + #our_hash = Hash[words.zip(scores)] + + end + + puts "Our_hash: #{all_words}" + #puts "Winning_words: #{winning_words}" +end +highest_score_from(words) + + + + + # scores.each do |score| + # scores << {score: score} + # end + + # highest_word = scores.max + # our_hash = Hash[words.zip(scores)] + # our_hash[:score] + + +# winner = { +# word: "Shonda", +# score: 10 +# } + +# highest_word = scores.max + + + + + + + # NOT WORKING CODE: From a538ab01cdcedff7f85b702b285b3d1390e48e7f Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Wed, 12 Feb 2020 19:40:07 -0800 Subject: [PATCH 07/14] Returns hash of max with highest scoring word without tiebreaking logic --- lib/adagrams.rb | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 9630be5..29172a7 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -124,28 +124,33 @@ def score_word(word) # ...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 -words = %w[shonda becca cat dog] +words = %w[shonda rebecca cat dog justin] def highest_score_from(words) - scores = [] all_words = [] - highest_word = "" - our_hash = {} + max_score = 0 + max_hash = {} words.each do |word| - #scores << score_word(word) - #scores.map do |score| - our_hash[:word] = word - our_hash[:score] = score_word(word) - all_words << our_hash - #our_hash = Hash[words.zip(scores)] - + word_data = {} + word_data[:word] = word + word_data[:score] = score_word(word) + all_words << word_data + end + + all_words.each do |hash| + current_word = hash[:word] + current_score = hash[:score] + if current_score > max_score + max_score = current_score + max_hash = hash + end end + return max_hash - puts "Our_hash: #{all_words}" - #puts "Winning_words: #{winning_words}" end -highest_score_from(words) + +puts highest_score_from(words) From bb40fced8e8cd02ea0daab5ce0cc5309e3d8817f Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Wed, 12 Feb 2020 21:20:59 -0800 Subject: [PATCH 08/14] Tie-breaking logic applied --- lib/adagrams.rb | 292 +++++++++++++++++------------------------------- 1 file changed, 102 insertions(+), 190 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 29172a7..ec7c262 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,68 +1,68 @@ -# $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 -# } +$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 +} -# # Wave 1 -# # method that returns shuffled hand of 10 tiles according to letter distribution hash -# def draw_letters -# pool = [] -# $LETTERS.map do |letter, tiles| -# tiles.times do -# pool << letter -# end -# end -# pool = pool.shuffle.drop(88) -# return pool -# end +# Wave 1 +# method that returns shuffled hand of 10 tiles according to letter distribution hash +def draw_letters + pool = [] + $LETTERS.map do |letter, tiles| + tiles.times do + pool << letter + end + end + pool = pool.shuffle.drop(88) + return pool +end -# # Wave 2 -# # method that returns true if every letter in the input word is available in correct quantities -# def uses_available_letters?(input, letters_in_hand) -# input_array = Array.new(input.chars) -# current_hand = Array.new(letters_in_hand) -# input_array.map do |letter| -# puts "The letter is #{letter}" -# if current_hand.index(letter) != nil -# puts "Found the letter!" -# puts "Here is the index: #{current_hand.index(letter)}" -# current_hand.delete_at(current_hand.index(letter)) -# puts "Here is the array now: #{current_hand}" -# else -# return false -# end -# end -# return true -# end +# Wave 2 +# method that returns true if every letter in the input word is available in correct quantities +def uses_available_letters?(input, letters_in_hand) + input_array = Array.new(input.chars) + current_hand = Array.new(letters_in_hand) + input_array.map do |letter| + puts "The letter is #{letter}" + if current_hand.index(letter) != nil + puts "Found the letter!" + puts "Here is the index: #{current_hand.index(letter)}" + current_hand.delete_at(current_hand.index(letter)) + puts "Here is the array now: #{current_hand}" + else + return false + end + end + return true +end -# my_letters = draw_letters -# puts "Here are my letters: #{my_letters}" -# puts uses_available_letters?("EAIIIIIAAARR", my_letters) +my_letters = draw_letters +puts "Here are my letters: #{my_letters}" +puts uses_available_letters?("EAIIIIIAAARR", my_letters) # Wave 3 # Method that calculates word score according to Adagrams rules @@ -109,8 +109,6 @@ def score_word(word) return total_points end - - # Wave 4 # 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. @@ -124,13 +122,19 @@ def score_word(word) # ...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 -words = %w[shonda rebecca cat dog justin] +# So, in a nut shell: +# first look at scores +# if two scores are ==, then look at length +# if 10 letters long, choose this one as winner +# if same score and same length, pick first ordered as winner + +words = %w[cat dog] def highest_score_from(words) all_words = [] max_score = 0 - max_hash = {} + # Builds word info hashes and adds into all_words words.each do |word| word_data = {} word_data[:word] = word @@ -138,139 +142,47 @@ def highest_score_from(words) all_words << word_data end + +# Eliminates non-ties for highest score + tie_score = [] + all_words.each do |hash| - current_word = hash[:word] current_score = hash[:score] if current_score > max_score max_score = current_score - max_hash = hash + tie_score = [hash] + elsif current_score == max_score + tie_score << hash end end - return max_hash - -end - -puts highest_score_from(words) - - - - - # scores.each do |score| - # scores << {score: score} - # end - - # highest_word = scores.max - # our_hash = Hash[words.zip(scores)] - # our_hash[:score] - -# winner = { -# word: "Shonda", -# score: 10 -# } + # Eliminates non-ties for shortest length + # picking 11 to initialize smallest so that we're guaranteed to see smallest be replaced at least once + smallest = 11 + shortest_words = [] -# highest_word = scores.max - - - - - - - - - -# NOT WORKING CODE: -# puts word_array -# puts "B: #{letter_values[:B]}" - -# letter_values.map do |letter, val| -# char_value = 0 - - # word_array.map do |char| - # sum = 0 - # puts "The first sum is: #{sum}" - # letter_values.map do |letter, value| - # letter = letter.to_s - # puts "Letter: #{letter} ; value: #{value}" - # if letter == char - # puts "The char and letter are the same!" - # puts "Curr val is: #{value}" - # sum = sum + value - # else - # puts "Not the same" - # end - # end - # return sum - # end - - -# try switching loop order -# sum = 0 -# letter_values.map do |letter, value| -# letter = letter.to_s -# puts "The first sum is: #{sum}" -# word_array.map do |char| -# puts "Letter: #{letter} ; value: #{value}" -# if letter == char -# puts "The char and letter are the same!" -# puts "Curr val is: #{value}" -# sum += value -# else -# puts "Not the same" -# end -# puts "Sum is: #{sum}" -# end -# return sum -# end - -# distro_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 -# } - -# def draw_letters (distro_of_letters) -# # creates a new array filled with each alphabet letter based on distrbution of letters -# letter_pool = [] -# distro_of_letters.each do |letters,tiles| -# tiles.times do -# letter_pool << letters -# end -# end -# # loops through the shuffled letter pool named "deck" + # if only one element in tie_score, that's the winner + if tie_score.length == 1 + return tie_score[0] + end -# puts "Here is your hand: " -# 9.times do -# deck = letter_pool.shuffle -# #used to create a random index value to select index of the letter position in "deck" -# selector = rand(0...97) -# print deck[selector] - -# end -# end + tie_score.each do |tie| + # if 10 chars long, that's the winner + if tie[:word].length == 10 + return tie + # if smaller word is found, update smallest and create a new shortest_words array to collect any possible ties for shortest length + elsif tie[:word].length < smallest + smallest = tie[:word].length + shortest_words = [tie] + # if more than one word ties for smallest, put it into existing shortest_words array + elsif tie[:word].length == smallest + shortest_words << tie + end + end + + # if winner wasn't already found, the winner is simply the first item of shortest_words + return shortest_words[0] -# draw_letters(distro_of_letters) +end +puts highest_score_from(words) \ No newline at end of file From acad200c629e4798b14f1e3eb79b2aa198aed7eb Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Thu, 13 Feb 2020 16:10:54 -0800 Subject: [PATCH 09/14] Added wave 5 method and removed comments. --- lib/adagrams.rb | 60 ++++++++++++++----------------------------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index ec7c262..5f759b5 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,3 +1,5 @@ +require "csv" +require "awesome_print" $LETTERS = { "A" => 9, @@ -29,7 +31,6 @@ } # Wave 1 -# method that returns shuffled hand of 10 tiles according to letter distribution hash def draw_letters pool = [] $LETTERS.map do |letter, tiles| @@ -42,7 +43,6 @@ def draw_letters end # Wave 2 -# method that returns true if every letter in the input word is available in correct quantities def uses_available_letters?(input, letters_in_hand) input_array = Array.new(input.chars) current_hand = Array.new(letters_in_hand) @@ -60,13 +60,7 @@ def uses_available_letters?(input, letters_in_hand) return true end -my_letters = draw_letters -puts "Here are my letters: #{my_letters}" -puts uses_available_letters?("EAIIIIIAAARR", my_letters) - # Wave 3 -# Method that calculates word score according to Adagrams rules - $LETTER_VALUES = { "A" => 1, "B" => 3, @@ -103,38 +97,15 @@ def score_word(word) $LETTER_VALUES.fetch_values(char) end total_points = points.flatten.sum - if word_array.length >= 7 - total_points += 8 - end + total_points += 8 if word_array.length >= 7 return total_points end # Wave 4 -# 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. - -# Has one parameter: words, which is an array of strings -# Returns a single hash that represents the data of a winning word and its score. The hash should have the following keys: -# :word, whose value is a string of a word -# :score, whose value is the score of that word -# 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 - -# So, in a nut shell: -# first look at scores -# if two scores are ==, then look at length -# if 10 letters long, choose this one as winner -# if same score and same length, pick first ordered as winner - -words = %w[cat dog] - def highest_score_from(words) all_words = [] max_score = 0 - # Builds word info hashes and adds into all_words words.each do |word| word_data = {} word_data[:word] = word @@ -142,29 +113,23 @@ def highest_score_from(words) all_words << word_data end - -# Eliminates non-ties for highest score tie_score = [] - all_words.each do |hash| current_score = hash[:score] if current_score > max_score max_score = current_score tie_score = [hash] + tie_score << hash elsif current_score == max_score tie_score << hash end end - # Eliminates non-ties for shortest length - # picking 11 to initialize smallest so that we're guaranteed to see smallest be replaced at least once smallest = 11 shortest_words = [] # if only one element in tie_score, that's the winner - if tie_score.length == 1 - return tie_score[0] - end + return tie_score[0] if tie_score.length == 1 tie_score.each do |tie| # if 10 chars long, that's the winner @@ -180,9 +145,18 @@ def highest_score_from(words) end end - # if winner wasn't already found, the winner is simply the first item of shortest_words + # if winner wasn't already found, the winner is the first item of shortest_words return shortest_words[0] - end -puts highest_score_from(words) \ No newline at end of file +# Wave 5 +def is_in_english_dict?(input) + found_word = [] +dictionary = CSV.read("../assets/dictionary-english.csv", headers: true).each do |record| + if record["Word"] == input.downcase + puts "Your word was found." + return true + end + end + return false +end \ No newline at end of file From d2af9e0a99b071bfc8c91fa702f5c3c903fb6e1a Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Thu, 13 Feb 2020 16:12:12 -0800 Subject: [PATCH 10/14] adagrams with comments on method functionality --- lib/Adagrams_with_comments.rb | 175 ++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 lib/Adagrams_with_comments.rb diff --git a/lib/Adagrams_with_comments.rb b/lib/Adagrams_with_comments.rb new file mode 100644 index 0000000..328d0fc --- /dev/null +++ b/lib/Adagrams_with_comments.rb @@ -0,0 +1,175 @@ + +$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 +} + +# Wave 1 +# method that returns shuffled hand of 10 tiles according to letter distribution hash +def draw_letters + pool = [] + $LETTERS.map do |letter, tiles| + tiles.times do + pool << letter + end + end + pool = pool.shuffle.drop(88) + return pool +end + +# Wave 2 +# method that returns true if every letter in the input word is available in correct quantities +def uses_available_letters?(input, letters_in_hand) + input_array = Array.new(input.chars) + current_hand = Array.new(letters_in_hand) + input_array.map do |letter| + puts "The letter is #{letter}" + if current_hand.index(letter) != nil + puts "Found the letter!" + puts "Here is the index: #{current_hand.index(letter)}" + current_hand.delete_at(current_hand.index(letter)) + puts "Here is the array now: #{current_hand}" + else + return false + end + end + return true +end + +my_letters = draw_letters +puts "Here are my letters: #{my_letters}" +puts uses_available_letters?("EAIIIIIAAARR", my_letters) + +# Wave 3 +# Method that calculates word score according to Adagrams rules + +$LETTER_VALUES = { + "A" => 1, + "B" => 3, + "C" => 3, + "D" => 2, + "E" => 1, + "F" => 4, + "G" => 2, + "H" => 4, + "I" => 1, + "J" => 8, + "K" => 5, + "L" => 1, + "M" => 3, + "N" => 1, + "O" => 1, + "P" => 3, + "Q" => 10, + "R" => 1, + "S" => 1, + "T" => 1, + "U" => 1, + "V" => 4, + "W" => 4, + "X" => 8, + "Y" => 4, + "Z" => 10 +} + +def score_word(word) + total_points = 0 + word_array = Array.new(word.upcase.chars) + points = word_array.map do |char| + $LETTER_VALUES.fetch_values(char) + end + total_points = points.flatten.sum + total_points += 8 if word_array.length >= 7 + return total_points +end + +# Wave 4 +# 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. + +def highest_score_from(words) + all_words = [] + max_score = 0 + + # Builds word info hashes and adds into all_words + words.each do |word| + word_data = {} + word_data[:word] = word + word_data[:score] = score_word(word) + all_words << word_data + end + + +# Eliminates non-ties for highest score + tie_score = [] + + all_words.each do |hash| + current_score = hash[:score] + if current_score > max_score + max_score = current_score + tie_score = [hash] + tie_score << hash + elsif current_score == max_score + tie_score << hash + end + end + + # Eliminates non-ties for shortest length + smallest = 11 + shortest_words = [] + + # if only one element in tie_score, that's the winner + return tie_score[0] if tie_score.length == 1 + + tie_score.each do |tie| + # if 10 chars long, that's the winner + if tie[:word].length == 10 + return tie + # if smaller word is found, update smallest and create a new shortest_words array to collect any possible ties for shortest length + elsif tie[:word].length < smallest + smallest = tie[:word].length + shortest_words = [tie] + # if more than one word ties for smallest, put it into existing shortest_words array + elsif tie[:word].length == smallest + shortest_words << tie + end + end + + # if winner wasn't already found, the winner is simply the first item of shortest_words + return shortest_words[0] + +end + +puts highest_score_from(words) + + +# Wave 5: Add a method called is_in_english_dict? in adagrams.rb. + +# Has one parameter: input, which is a string +# Returns a boolean +# true, if input is in the provided English dictionary +# false, if input is not in the provided English dictionary +# Uses the English dictionary found in assets/dictionary-english.csv \ No newline at end of file From 0c685ddc311559283d8fc4e36fc44fc287cea161 Mon Sep 17 00:00:00 2001 From: Rebecca Roach <22301035+RebeccaRoach@users.noreply.github.com> Date: Thu, 13 Feb 2020 16:22:54 -0800 Subject: [PATCH 11/14] Delete adagrams.rb --- adagrams.rb | 45 --------------------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb deleted file mode 100644 index 1bcc828..0000000 --- a/adagrams.rb +++ /dev/null @@ -1,45 +0,0 @@ -letter_distribution = { - "A" => 9, - "B" => 2, - "C" => 2, - "D" => 4, - "E" => 1, - "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 -} - -#puts letter_distribution.values_at("R") -# first stage of draw_letters - -def draw_letters(letter_distribution) - pool = [] - letter_distribution.map do |letter, tiles| - - tiles.times do - pool << letter - end - end - return pool -end - - -p draw_letters(letter_distribution) \ No newline at end of file From d2366318b6574b6126eba862d6a577e854ecee48 Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Fri, 14 Feb 2020 14:47:37 -0800 Subject: [PATCH 12/14] Cleaned all waves, ready to submit --- lib/adagrams.rb | 99 ++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 5f759b5..743ac19 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,7 +1,8 @@ +# Defining requirements and constants require "csv" require "awesome_print" -$LETTERS = { +LETTERS = { "A" => 9, "B" => 2, "C" => 2, @@ -30,38 +31,7 @@ "Z" => 1 } -# Wave 1 -def draw_letters - pool = [] - $LETTERS.map do |letter, tiles| - tiles.times do - pool << letter - end - end - pool = pool.shuffle.drop(88) - return pool -end - -# Wave 2 -def uses_available_letters?(input, letters_in_hand) - input_array = Array.new(input.chars) - current_hand = Array.new(letters_in_hand) - input_array.map do |letter| - puts "The letter is #{letter}" - if current_hand.index(letter) != nil - puts "Found the letter!" - puts "Here is the index: #{current_hand.index(letter)}" - current_hand.delete_at(current_hand.index(letter)) - puts "Here is the array now: #{current_hand}" - else - return false - end - end - return true -end - -# Wave 3 -$LETTER_VALUES = { +POINT_VALUES = { "A" => 1, "B" => 3, "C" => 3, @@ -90,11 +60,40 @@ def uses_available_letters?(input, letters_in_hand) "Z" => 10 } +DICTIONARY = "../assets/dictionary-english.csv" + +# Wave 1 +def draw_letters + pool = [] + LETTERS.map do |letter, num_of_tiles| + num_of_tiles.times do + pool << letter + end + end + pool = pool.shuffle.drop(88) + return pool +end + +# Wave 2 +def uses_available_letters?(input, letters_in_hand) + input_array = Array.new(input.chars) + current_hand = Array.new(letters_in_hand) + input_array.map do |letter| + if current_hand.index(letter) != nil + current_hand.delete_at(current_hand.index(letter)) + else + return false + end + end + return true +end + +# Wave 3 def score_word(word) total_points = 0 word_array = Array.new(word.upcase.chars) points = word_array.map do |char| - $LETTER_VALUES.fetch_values(char) + POINT_VALUES.fetch_values(char) end total_points = points.flatten.sum total_points += 8 if word_array.length >= 7 @@ -113,34 +112,34 @@ def highest_score_from(words) all_words << word_data end - tie_score = [] + tie_scores = [] all_words.each do |hash| current_score = hash[:score] if current_score > max_score max_score = current_score - tie_score = [hash] - tie_score << hash + tie_scores = [hash] elsif current_score == max_score - tie_score << hash + tie_scores << hash end end - smallest = 11 + shortest_length = 11 shortest_words = [] - # if only one element in tie_score, that's the winner - return tie_score[0] if tie_score.length == 1 + # if only one element in tie_scores, that's the winner + return tie_scores[0] if tie_scores.length == 1 - tie_score.each do |tie| + tie_scores.each do |tie| # if 10 chars long, that's the winner if tie[:word].length == 10 return tie - # if smaller word is found, update smallest and create a new shortest_words array to collect any possible ties for shortest length - elsif tie[:word].length < smallest - smallest = tie[:word].length + # if smaller word is found, update shortest_length and create a new shortest_words array to collect + # any possible ties for shortest length + elsif tie[:word].length < shortest_length + shortest_length = tie[:word].length shortest_words = [tie] - # if more than one word ties for smallest, put it into existing shortest_words array - elsif tie[:word].length == smallest + # if more than one word ties for shortest_length, put it into existing shortest_words array + elsif tie[:word].length == shortest_length shortest_words << tie end end @@ -151,10 +150,8 @@ def highest_score_from(words) # Wave 5 def is_in_english_dict?(input) - found_word = [] -dictionary = CSV.read("../assets/dictionary-english.csv", headers: true).each do |record| - if record["Word"] == input.downcase - puts "Your word was found." + dictionary = CSV.read(DICTIONARY, headers: true).each do |word_entry| + if word_entry["Word"] == input.downcase return true end end From 33eab03a00da5dffb1b97eae9729929bf1811755 Mon Sep 17 00:00:00 2001 From: Rebecca Roach <22301035+RebeccaRoach@users.noreply.github.com> Date: Fri, 14 Feb 2020 14:52:06 -0800 Subject: [PATCH 13/14] Delete Adagrams_with_comments.rb --- lib/Adagrams_with_comments.rb | 175 ---------------------------------- 1 file changed, 175 deletions(-) delete mode 100644 lib/Adagrams_with_comments.rb diff --git a/lib/Adagrams_with_comments.rb b/lib/Adagrams_with_comments.rb deleted file mode 100644 index 328d0fc..0000000 --- a/lib/Adagrams_with_comments.rb +++ /dev/null @@ -1,175 +0,0 @@ - -$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 -} - -# Wave 1 -# method that returns shuffled hand of 10 tiles according to letter distribution hash -def draw_letters - pool = [] - $LETTERS.map do |letter, tiles| - tiles.times do - pool << letter - end - end - pool = pool.shuffle.drop(88) - return pool -end - -# Wave 2 -# method that returns true if every letter in the input word is available in correct quantities -def uses_available_letters?(input, letters_in_hand) - input_array = Array.new(input.chars) - current_hand = Array.new(letters_in_hand) - input_array.map do |letter| - puts "The letter is #{letter}" - if current_hand.index(letter) != nil - puts "Found the letter!" - puts "Here is the index: #{current_hand.index(letter)}" - current_hand.delete_at(current_hand.index(letter)) - puts "Here is the array now: #{current_hand}" - else - return false - end - end - return true -end - -my_letters = draw_letters -puts "Here are my letters: #{my_letters}" -puts uses_available_letters?("EAIIIIIAAARR", my_letters) - -# Wave 3 -# Method that calculates word score according to Adagrams rules - -$LETTER_VALUES = { - "A" => 1, - "B" => 3, - "C" => 3, - "D" => 2, - "E" => 1, - "F" => 4, - "G" => 2, - "H" => 4, - "I" => 1, - "J" => 8, - "K" => 5, - "L" => 1, - "M" => 3, - "N" => 1, - "O" => 1, - "P" => 3, - "Q" => 10, - "R" => 1, - "S" => 1, - "T" => 1, - "U" => 1, - "V" => 4, - "W" => 4, - "X" => 8, - "Y" => 4, - "Z" => 10 -} - -def score_word(word) - total_points = 0 - word_array = Array.new(word.upcase.chars) - points = word_array.map do |char| - $LETTER_VALUES.fetch_values(char) - end - total_points = points.flatten.sum - total_points += 8 if word_array.length >= 7 - return total_points -end - -# Wave 4 -# 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. - -def highest_score_from(words) - all_words = [] - max_score = 0 - - # Builds word info hashes and adds into all_words - words.each do |word| - word_data = {} - word_data[:word] = word - word_data[:score] = score_word(word) - all_words << word_data - end - - -# Eliminates non-ties for highest score - tie_score = [] - - all_words.each do |hash| - current_score = hash[:score] - if current_score > max_score - max_score = current_score - tie_score = [hash] - tie_score << hash - elsif current_score == max_score - tie_score << hash - end - end - - # Eliminates non-ties for shortest length - smallest = 11 - shortest_words = [] - - # if only one element in tie_score, that's the winner - return tie_score[0] if tie_score.length == 1 - - tie_score.each do |tie| - # if 10 chars long, that's the winner - if tie[:word].length == 10 - return tie - # if smaller word is found, update smallest and create a new shortest_words array to collect any possible ties for shortest length - elsif tie[:word].length < smallest - smallest = tie[:word].length - shortest_words = [tie] - # if more than one word ties for smallest, put it into existing shortest_words array - elsif tie[:word].length == smallest - shortest_words << tie - end - end - - # if winner wasn't already found, the winner is simply the first item of shortest_words - return shortest_words[0] - -end - -puts highest_score_from(words) - - -# Wave 5: Add a method called is_in_english_dict? in adagrams.rb. - -# Has one parameter: input, which is a string -# Returns a boolean -# true, if input is in the provided English dictionary -# false, if input is not in the provided English dictionary -# Uses the English dictionary found in assets/dictionary-english.csv \ No newline at end of file From e35448e3372ba7f897bedfeecb69da8838321869 Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Fri, 14 Feb 2020 15:44:57 -0800 Subject: [PATCH 14/14] Refactored wave 1, finalized for submission --- lib/adagrams.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 743ac19..7138490 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -64,12 +64,9 @@ # Wave 1 def draw_letters - pool = [] - LETTERS.map do |letter, num_of_tiles| - num_of_tiles.times do - pool << letter - end - end + pool = LETTERS.map do |letter, num_of_tiles| + [letter] * num_of_tiles + end.flatten pool = pool.shuffle.drop(88) return pool end