From f7156d75caf94586e6a5d7b3efa2f2b74ad3145d Mon Sep 17 00:00:00 2001 From: Maryam Heshmati Date: Tue, 11 Feb 2020 09:05:16 -0800 Subject: [PATCH 1/7] wrote the method for wave-1, gives the user 10 random letters --- lib/adagrams.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..305fd88 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,44 @@ +#make the data structure for all the letters and the number of letters availble + +def draw_letters + sample = { + "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, + } + pool_letters = [] + sample.each do |letter, quantity| + quantity.times do + pool_letters << letter + end + end + #randomize the pool of letters + pool_letters = pool_letters.sample(pool_letters.length) + #display the 10 letters they have drawn + return pool_letters.first(10) +end +#Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z +puts "😎=> control, command, space" \ No newline at end of file From 115b6deb88f46c3c073488eb6847dc2681c84569 Mon Sep 17 00:00:00 2001 From: Maryam Heshmati Date: Tue, 11 Feb 2020 17:54:03 -0800 Subject: [PATCH 2/7] created wave2 and wave3 --- lib/adagrams.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 305fd88..3cda193 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,4 +1,5 @@ #make the data structure for all the letters and the number of letters availble +#wave 1:) def draw_letters sample = { @@ -40,5 +41,49 @@ def draw_letters #display the 10 letters they have drawn return pool_letters.first(10) end -#Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z -puts "😎=> control, command, space" \ No newline at end of file + +# puts "😎=> control, command, space" + +#wave 2 +def uses_available_letters?(input, letters_in_hand) + input_splitted = input.split "" + selected_letters = [] + input_splitted.each do |letter| + if letters_in_hand.include? letter + selected_letters << letter + letters_in_hand = letters_in_hand - selected_letters + else + return false + end + end + return true +end + +#wave 3 +def score_word(word) + score = { + "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), + } + word_splitted = word.upcase.split "" + + total_score = 0 + word_splitted.each do |letter| + score.each do |value, letters_with_value| + if letters_with_value.include? letter + total_score = total_score + value.to_i + end + end + end + if word_splitted.length > 7 && word_splitted.length < 10 + total_score+=8 + end + +end + + From 363562ca9bd70d9eab23073c3694496720dbe6c9 Mon Sep 17 00:00:00 2001 From: Maryam Heshmati Date: Wed, 12 Feb 2020 11:28:30 -0800 Subject: [PATCH 3/7] wave1-3 complete --- lib/adagrams.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 3cda193..bfd8b21 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -86,4 +86,3 @@ def score_word(word) end - From 862125d5c85c3e21c1530d5601370bc0db3eb351 Mon Sep 17 00:00:00 2001 From: Maryam Heshmati Date: Wed, 12 Feb 2020 21:51:45 -0800 Subject: [PATCH 4/7] wave 4 complete, determine which word has the highest score --- lib/adagrams.rb | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index bfd8b21..a6f985d 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -42,8 +42,6 @@ def draw_letters return pool_letters.first(10) end -# puts "😎=> control, command, space" - #wave 2 def uses_available_letters?(input, letters_in_hand) input_splitted = input.split "" @@ -80,9 +78,37 @@ def score_word(word) end end end - if word_splitted.length > 7 && word_splitted.length < 10 - total_score+=8 + if word_splitted.length >= 7 && word_splitted.length <= 10 + total_score += 8 end - + return total_score end +#wave 4 +def highest_score_from(words) + word_and_score = {} + words.each do |word| + word_and_score[word] = score_word(word) + end + + highest_score = 0 + highest_scored_name = nil + word_and_score.each do |word, score| + if score > highest_score + highest_score = score + highest_scored_name = word + end + + if highest_score == score + if highest_scored_name.length < 10 && word.length < 10 && highest_scored_name.length > word.length + highest_scored_name = word + elsif (highest_scored_name.length >= 10 || word.length >= 10) && word.length > highest_scored_name.length + highest_scored_name = word + end + end + end + winning_word_score = {} + winning_word_score[:word] = highest_scored_name + winning_word_score[:score] = highest_score + return winning_word_score +end From f4ebf2aeadbc0fae4bf92f879fded1fbd184ffdd Mon Sep 17 00:00:00 2001 From: Maryam Heshmati Date: Thu, 13 Feb 2020 15:08:40 -0800 Subject: [PATCH 5/7] wave 4 --- lib/adagrams.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index a6f985d..926b0e3 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -112,3 +112,11 @@ def highest_score_from(words) winning_word_score[:score] = highest_score return winning_word_score end + +#wave 5 +require 'csv' +require 'awesome_print' + +# is_in_english_dict?(input) +words = CSV.read('../assets/dictionary-english.csv') +ap words \ No newline at end of file From c6d95f4e8f6d417f440e2c5f55869465f19d4996 Mon Sep 17 00:00:00 2001 From: Maryam Heshmati Date: Thu, 13 Feb 2020 16:59:57 -0800 Subject: [PATCH 6/7] wave5: determine if input is an actual English word --- lib/adagrams.rb | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 926b0e3..8079ee1 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -45,11 +45,14 @@ def draw_letters #wave 2 def uses_available_letters?(input, letters_in_hand) input_splitted = input.split "" - selected_letters = [] + + selected_letters = [].replace(letters_in_hand) + input_splitted.each do |letter| - if letters_in_hand.include? letter - selected_letters << letter - letters_in_hand = letters_in_hand - selected_letters + if selected_letters.include? letter + # selected_letters << letter + where_is_my_letter = selected_letters.find_index(letter) + selected_letters.delete_at(where_is_my_letter) else return false end @@ -57,6 +60,11 @@ def uses_available_letters?(input, letters_in_hand) return true end +uses_available_letters?("apple", ["a", "p", "a", "p", "e", "l", "z", "k", "a", "a"]) + + + + #wave 3 def score_word(word) score = { @@ -114,9 +122,16 @@ def highest_score_from(words) end #wave 5 -require 'csv' -require 'awesome_print' +require "csv" +require "awesome_print" + +def is_in_english_dict?(input) + words = CSV.read("./assets/dictionary-english.csv").each do |word| + if word.include? input + return true + end + end + return false +end + -# is_in_english_dict?(input) -words = CSV.read('../assets/dictionary-english.csv') -ap words \ No newline at end of file From eaa724a2ea7554d4200c9440b924ace369138da8 Mon Sep 17 00:00:00 2001 From: Maryam Heshmati Date: Fri, 14 Feb 2020 16:32:13 -0800 Subject: [PATCH 7/7] completed wave4, 5. will calculated the highest score word and analyze if the word is an English word from pulling data from the dictionary --- lib/adagrams.rb | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 8079ee1..0b4ff15 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -30,14 +30,17 @@ def draw_letters "y" => 2, "z" => 1, } + #create an array with all the availble letters pool_letters = [] sample.each do |letter, quantity| quantity.times do pool_letters << letter end end + #randomize the pool of letters pool_letters = pool_letters.sample(pool_letters.length) + #display the 10 letters they have drawn return pool_letters.first(10) end @@ -45,14 +48,13 @@ def draw_letters #wave 2 def uses_available_letters?(input, letters_in_hand) input_splitted = input.split "" - + #make a copy of letters in hand so it remains un-changed + #[].replace will replace the contents of selected_letters array which is empty with the contents from letters_in_hand selected_letters = [].replace(letters_in_hand) - input_splitted.each do |letter| if selected_letters.include? letter - # selected_letters << letter - where_is_my_letter = selected_letters.find_index(letter) - selected_letters.delete_at(where_is_my_letter) + where_is_my_letter = selected_letters.find_index(letter) #using .find_index to see where that letter from the word is in the array that contain the 10 availble letters + selected_letters.delete_at(where_is_my_letter) #modify the duplicated array (selected_letters) to remove that item from the array, this will ensure that only the letter with that index is removed instead of all the same-letters in the hand else return false end @@ -60,11 +62,6 @@ def uses_available_letters?(input, letters_in_hand) return true end -uses_available_letters?("apple", ["a", "p", "a", "p", "e", "l", "z", "k", "a", "a"]) - - - - #wave 3 def score_word(word) score = { @@ -76,14 +73,12 @@ def score_word(word) "8" => %w(J X), "10" => %w(Q Z), } + #converting the word into upcase so it would be independent of the users input word_splitted = word.upcase.split "" - total_score = 0 word_splitted.each do |letter| score.each do |value, letters_with_value| - if letters_with_value.include? letter - total_score = total_score + value.to_i - end + total_score += value.to_i if letters_with_value.include? letter #add the associated value with that letter (converted to integer) to the total score end end if word_splitted.length >= 7 && word_splitted.length <= 10 @@ -96,42 +91,42 @@ def score_word(word) def highest_score_from(words) word_and_score = {} words.each do |word| - word_and_score[word] = score_word(word) + word_and_score[word] = score_word(word) #calling the method score_word to determine the score of that word end highest_score = 0 highest_scored_name = nil word_and_score.each do |word, score| - if score > highest_score + if score > highest_score #determine the word with the highest score highest_score = score highest_scored_name = word end - if highest_score == score - if highest_scored_name.length < 10 && word.length < 10 && highest_scored_name.length > word.length + if highest_score == score #in case there is a tie of scores + if highest_scored_name.length < 10 && word.length < 10 && highest_scored_name.length > word.length #if the length of the word is less than 10 letters, the word with the least letters will win highest_scored_name = word - elsif (highest_scored_name.length >= 10 || word.length >= 10) && word.length > highest_scored_name.length + elsif (highest_scored_name.length >= 10 || word.length >= 10) && word.length > highest_scored_name.length #unless a word is 10 letters or longer, the that word will be the winner highest_scored_name = word end end end - winning_word_score = {} - winning_word_score[:word] = highest_scored_name - winning_word_score[:score] = highest_score + winning_word_score = { + word: highest_scored_name, + score: highest_score + } return winning_word_score end #wave 5 require "csv" -require "awesome_print" +#requiring the English dictionary that is in a CSV file to see if the user's input is an actual English word def is_in_english_dict?(input) - words = CSV.read("./assets/dictionary-english.csv").each do |word| + words = CSV.read("./assets/dictionary-english.csv") + words.each do |word| if word.include? input return true end end return false end - -