diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..591f8b0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,11 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + + null + ] +} \ No newline at end of file diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..97b8aa4 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,104 @@ +require 'csv' +require 'awesome_print' + +def draw_letters + letter_pool = [] + letter_choices = Hash( 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) + + letter_choices.map do |letter, number| + letter_pool << (letter.to_s * number).split(//) + end + letter_pool = letter_pool.flatten + user_hand = letter_pool.sample(10) + + return user_hand +end + +def uses_available_letters?(input, letters_in_hand) + split_input = input.upcase.split(//) + + letters_in_hand.each do |letter| + split_input.delete_at(split_input.index(letter)) unless split_input.index(letter).nil? + end + + return split_input.empty? +end + +def score_word(word) + score_count = 0 + split_input = word.upcase.split(//) + + split_input.each do |letter| + case letter + when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" + score_count += 1 + when "D", "G" + score_count += 2 + when "B", "C", "M", "P" + score_count += 3 + when "F", "H", "V", "W", "Y" + score_count += 4 + when "K" + score_count += 5 + when "J", "X" + score_count += 8 + when "Q", "Z" + score_count += 10 + end + end + + score_count += 8 if word.length > 6 + return score_count +end + +def highest_score_from(words) + word_score_hash = Hash.new + highest_score = 0 + winning_word = "" + shortest_word_length = 0 + + words.each do |word| + score = score_word(word) + word_score_hash[word] = score + end + + highest_score = word_score_hash.values.max + + tie = word_score_hash.select { |k, v| v == highest_score} + if tie.length > 1 + tie.each do |word, score| + if word.length > shortest_word_length + if shortest_word_length == 0 + shortest_word_length = word.length + winning_word = word + elsif word.length == 10 + shortest_word_length = word.length + winning_word = word + end + + elsif word.length < shortest_word_length + if shortest_word_length == 10 + nil + else + winning_word = word + end + + elsif word.length == shortest_word_length + winning_word = word_score_hash.key(highest_score) + end + end + else + winning_word = word_score_hash.key(highest_score) + end + return winning_hash = {:word => winning_word, :score => highest_score} +end + +def is_in_english_dict?(input) + dictionary = CSV.read('assets/dictionary-english.csv') + dictionary = dictionary.flatten + raise ArgumentError if input == "" + raise ArgumentError if input.nil? + raise ArgumentError if input.class != String + return dictionary.include?(input) +end \ No newline at end of file diff --git a/test/adagrams_test.rb b/test/adagrams_test.rb index 90ec44d..9fb26e2 100644 --- a/test/adagrams_test.rb +++ b/test/adagrams_test.rb @@ -178,4 +178,31 @@ expect(best_word[:score]).must_equal 18 end end + + describe 'is_in_english_dict?' do + it 'checks if input word is in english dictionary' do + input = 'hello' + expect(is_in_english_dict?(input)).must_equal true + end + + it 'checks if input is not in the english dictionary' do + input = 'asdf' + expect(is_in_english_dict?(input)).must_equal false + end + + it 'calls ArgumentError on empty string' do + input = "" + expect{is_in_english_dict?(input)}.must_raise ArgumentError + end + + it 'calls ArgumentError if input is nil' do + input = nil + expect{is_in_english_dict?(input)}.must_raise ArgumentError + end + + it "calls ArgumentError on non string inputs" do + input = 234 + expect{is_in_english_dict?(input)}.must_raise ArgumentError + end + end end