From 7a1a37137e134f48d519dd66111ab191c564fba4 Mon Sep 17 00:00:00 2001 From: Hannah Date: Mon, 13 Aug 2018 16:38:09 -0700 Subject: [PATCH 1/5] wave 1 complete --- lib/adagrams.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..3a47962 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,21 @@ +def draw_letters + bag_of_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", "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"] + + individual_hand = Array.new + + 10.times do |x| + bag_of_letters = bag_of_letters.shuffle + letter = bag_of_letters.pop + individual_hand[x] = letter + end + return individual_hand +end + +print draw_letters From 0cd6acd83f86a107d9afbb9dfaa1716dd012a27c Mon Sep 17 00:00:00 2001 From: Hannah Date: Tue, 14 Aug 2018 16:07:55 -0700 Subject: [PATCH 2/5] wave 2 and 3 complete --- lib/adagrams.rb | 33 ++++++++++++++++++++++++++++++--- specs/adagrams_spec.rb | 2 +- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 3a47962..f0b6e39 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -8,14 +8,41 @@ def draw_letters "t", "t", "t", "t", "t", "u", "u", "u", "u", "v", "v", "w", "w", "x", "y", "y", "z"] - individual_hand = Array.new + individual_hand = Array.new(10) - 10.times do |x| + individual_hand.each.with_index do |letter, x| bag_of_letters = bag_of_letters.shuffle letter = bag_of_letters.pop individual_hand[x] = letter end + return individual_hand end -print draw_letters +def uses_available_letters?(word_input, individual_letters) + individual_letters = individual_letters.map {|x| x.downcase} + input_divided = word_input.downcase.scan(/\w/) + # puts in + input_divided.each do |letter| + if individual_letters.include?(letter) + individual_letters.delete_at(individual_letters.index(letter)) + else + return false + end + end + return true +end + +def score_word(word) + 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} + word_divided = word.downcase.chars + values = word_divided.map {|letter| letter_values[letter]} + total_score = values.sum + if word.length >= 7 && word.length <= 10 + total_score += 8 + end + return total_score +end diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index ae2ccd0..d6a74a7 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -81,7 +81,7 @@ end end - describe 'highest_score_from method' do + xdescribe 'highest_score_from method' do it 'returns a hash that contains the word and score of best word in an array' do words = ['X', 'XX', 'XXX', 'XXXX'] best_word = highest_score_from words From b69dece709644102de0e11941262e39a5edbf2bd Mon Sep 17 00:00:00 2001 From: Hannah Date: Wed, 15 Aug 2018 15:11:09 -0700 Subject: [PATCH 3/5] wave 4 and 5 completed --- .DS_Store | Bin 0 -> 6148 bytes lib/adagrams.rb | 41 +++++++++++++++++++++++++++++++++++++++++ specs/adagrams_spec.rb | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..cc5ed43aaef75aedbf6846c0b4db0176d664011a GIT binary patch literal 6148 zcmeHK!AiqG5Z!I7ZYyFBf<5Nqt%np#iw7aZdhjMh^q|rvwAesQN|RbNM)Di_M}C37 ze~|%vcbm**F+3wye1GrWB=F}!M-Xyz<%h#)Zb!3l7|VGaH6TP) zRQvTtnjV>*rqS-Sv!;=DyC&F&oh+-0{e#x=+2H>1DSggfUKO((K0+zG8W->eMq!f= z&NK|7@DBWooJA}lF+dCu18cy5KKjJ=8tj#}NDL4IKVtyT2Llw*(wHlhM+Y?c`-tNe zL=>>`E`exhv^3@lVFZM$R6v!=^%aAwbnqKG&eE7GROyV%m0=#eGS@d0E>{P?A;THB z6jDnJ5CcU9%Bow(`hWWU{lAz*Jz{_u_*V?@O3&%puq9nvS2l;W)&L!VqF`LEaG3&z iEX5FurMLkq1^fmYfR@HwA$UOOM?lg*4KeVm4159+f>NXa literal 0 HcmV?d00001 diff --git a/lib/adagrams.rb b/lib/adagrams.rb index f0b6e39..b0fec20 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -33,6 +33,17 @@ def uses_available_letters?(word_input, individual_letters) return true end +def is_in_english_dict?(input) +check = false +File.foreach("assets/dictionary-english.csv") do |line| + if line.chomp == input.downcase + check = true + return check + end +end +return check +end + def score_word(word) 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, @@ -46,3 +57,33 @@ def score_word(word) end return total_score end + +def highest_score_from(words) + word_scores = words.map do |word| + {word: word.upcase, score: score_word(word)} + end + + max_score = word_scores.max_by {|x| x[:score]}[:score] + all_max_scores = word_scores.select do |word| + word[:score] == max_score + end + + shortest_word_length = all_max_scores.min_by { |word| word[:word].length}[:word].length + + shortest_words = all_max_scores.select do |word| + word[:word].length == shortest_word_length + end + + max_equals_ten = all_max_scores.select do |x| + x[:word].length == 10 + end + + return max_equals_ten.length == 0? shortest_words.first : max_equals_ten.first + # same as above^: + # if max_equals_ten.length == 0 + # return shortest_words.first + # else + # return max_equals_ten.first + # end + +end diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index d6a74a7..7c0777a 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -26,6 +26,7 @@ end end + describe 'uses_available_letters? method' do it 'returns true if the submitted letters are valid against the drawn letters' do @@ -81,7 +82,7 @@ end end - xdescribe 'highest_score_from method' do + describe 'highest_score_from method' do it 'returns a hash that contains the word and score of best word in an array' do words = ['X', 'XX', 'XXX', 'XXXX'] best_word = highest_score_from words @@ -168,4 +169,37 @@ expect(best_word[:score]).must_equal 18 end end + + describe 'is in english dict? method' do + it 'returns true if submitted word is valid in dictionary' do + #Arrange + input = "horse" + #act + in_dictionary = is_in_english_dict?(input) + #Assert + expect(in_dictionary).must_equal true + end + it 'returns false if submitted word is not located in dictionary' do + input = "cealpit" + + in_dictionary = is_in_english_dict?(input) + + expect(in_dictionary).must_equal false + end + it 'returns false if submitted word is not located in dictionary' do + input = "ab$" + + in_dictionary = is_in_english_dict?(input) + + expect(in_dictionary).must_equal false + end + it 'accounts for input being upcase or downcase' do + input = "DolPhiN" + + in_dictionary = is_in_english_dict?(input) + + expect(in_dictionary).must_equal true + end + + end end From f038ee9cf4f382c9fddd6e10dc2d3cb66b1c98db Mon Sep 17 00:00:00 2001 From: Hannah Date: Wed, 15 Aug 2018 15:20:52 -0700 Subject: [PATCH 4/5] clean up code (refactor) --- lib/adagrams.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index b0fec20..1b08cd0 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -34,14 +34,12 @@ def uses_available_letters?(word_input, individual_letters) end def is_in_english_dict?(input) -check = false -File.foreach("assets/dictionary-english.csv") do |line| - if line.chomp == input.downcase - check = true - return check - end -end -return check + File.foreach("assets/dictionary-english.csv") do |line| + if line.chomp == input.downcase + return true + end + end + return false end def score_word(word) @@ -51,10 +49,11 @@ def score_word(word) "u" => 1, "v" => 4,"w" => 4, "x"=>8, "y" => 4,"z" => 10} word_divided = word.downcase.chars values = word_divided.map {|letter| letter_values[letter]} + total_score = values.sum - if word.length >= 7 && word.length <= 10 - total_score += 8 - end + + total_score += 8 if word.length >= 7 && word.length <= 10 + return total_score end From 7bdb153df5e9289ae56e4d5dec2048849ef2b2f6 Mon Sep 17 00:00:00 2001 From: Hannah Date: Wed, 15 Aug 2018 15:41:25 -0700 Subject: [PATCH 5/5] final completion after wave 5 tests --- specs/adagrams_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index 7c0777a..027d951 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -200,6 +200,19 @@ expect(in_dictionary).must_equal true end + it 'accounts for first word in dictionary' do + input = "a" + in_dictionary = is_in_english_dict?(input) + + expect(in_dictionary).must_equal true + end + it 'accounts for last word in dictionary' do + input = "zwitterion" + + in_dictionary = is_in_english_dict?(input) + + expect(in_dictionary).must_equal true + end end end