Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divya and Daniela's Adagrams #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'csv'


def draw_letters
all_letters = %w[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 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 ]

return (all_letters.shuffle).first(10)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this. Nice one liner!

end


def uses_available_letters?(input,letters_in_hand)
value = true
input_letters = (input.upcase).split('')
hand_copy = letters_in_hand.map do |letter|
letter
end

input_letters.each do |alphabet|
if hand_copy.include?(alphabet)
hand_copy.delete_at(hand_copy.index(alphabet))
else
value = false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wait until the loop is finished to return this false? If the first element fails, is there any reason to keep checking?

end
end

return value
end

def score_word(word)
score_hash = {"A" => 1, "E" => 1, "I" => 1,"O" => 1,"U" => 1,"L" => 1,"N" => 1,"R" => 1,"S" => 1,"T" => 1,\
"D" => 2, "G" => 2, "B" => 3, "C" => 3,"M" => 3,"P" => 3, "F" => 4, "H" => 4,"V" => 4,"W" => 4,"Y" => 4,\
"K" => 5, "J" => 8, "X" => 8, "Q" => 10,"Z" => 10}

word_array = word.upcase.split('')
scores = word_array.map do |letter|
score_hash[letter]
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A different enumerable would allow you to do this with only one loop! Think about how inject/reduce could be used here.


score = scores.sum
if word.length > 6
score += 8
end

return score
end

def highest_score_from(words)
words_with_scores = {}

words.each do |word|
words_with_scores["#{word}"] = score_word(word)
end

best_score_array = words_with_scores.max_by {|word, score| score}

best_score = best_score_array[1]

best_word_list = words_with_scores.select{|word, score| score == best_score}

best_word = 'reallybigtestword'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't stay in your final code! Write a case to handle when there is not a current best word, rather than relying on the first entry in best_word_list being overwritten.


best_word_list.each do |word, score|
if word.length == 10
best_word = word
return {word: best_word, score: best_score}
elsif word.length < best_word.length
best_word = word
end
end

return {word: best_word, score: best_score}
end

def is_in_english_dict?(input)

truth_value = false

CSV.foreach('assets/dictionary-english.csv') do |row|
if input == row[0]
truth_value = true
return truth_value
end
end

return truth_value
end