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

Branches - Julia & Eve #14

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
122 changes: 122 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
require 'csv'

def draw_letters()
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
}

# WAVE 1

# convert distribution hash to letters array with all available letters
letters = []
distribution.each do |letter, quantity|
quantity.times { letters << letter.to_s }
end

# push 10 random letters to strings array, removing letter from letters array each time to prevent repitition
drawn_letters = []
10.times do
letters.shuffle!
drawn_letters << letters.pop
Copy link

Choose a reason for hiding this comment

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

Nice! Using .pop here is concise and a good call

end

return drawn_letters
end

# WAVE 2
def uses_available_letters?(input, letters_in_hand)
current_letters_in_hand = letters_in_hand.dup
# split user input into array of chararacters
characters = input.upcase.split('')
# for each character, save index number to index variable
characters.each do |char|
index = current_letters_in_hand.find_index(char)
# if index number is truthy, delete element at index
if index
Copy link

Choose a reason for hiding this comment

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

Nice logic here that relies on truthiness and understanding what find_index does!

current_letters_in_hand.delete_at(index)
# if index number is falsey, return false
else
return false
Copy link

Choose a reason for hiding this comment

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

Nice logic; putting a return false here will break the method out as soon as it needs to

end
end
# will return true if all indices are accounted for
return true
end

# WAVE 3
def score_word(word)
characters = word.upcase.split('')
# if word length is greater than or equal to 7, score gains additional 8 points for word
score = (characters.length >= 7) ? 8 : 0
characters.each do |letter|
case letter
when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
score += 1
when "D", "G"
score += 2
when "B", "C", "M", "P"
score += 3
when "F", "H", "V", "W", "Y"
score += 4
when "K"
score += 5
when "J", "X"
score += 8
when "Q", "Z"
score += 10
end
end
return score
end

# WAVE 4
def highest_score_from(words)
# map each word and score as key value pairs to an array (named results) of hashes
results = words.map { |word| { word: word, score: score_word(word)} }
best_result = results.max_by { |obj| obj[:score] }

high_scores = []
results.each { |obj| high_scores << obj[:word] if obj[:score] == best_result[:score] }

# save words with length of 10 to winner array
winner = high_scores.select { |element| element.length == 10 }

# if there are no words with length of 10, assign first, shortest word to winner variable
# if there are words with length of 10, assign first word to winner variable
winner = winner.empty? ? high_scores.min_by { |element| element.length } : winner[0]

return { word: winner, score: best_result[:score] }
end
Copy link

Choose a reason for hiding this comment

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

I'm obsessed with y'alls implementation of this method! It's so clever and so concise! It looks so good! The only thought for improvement is maybe using a different variable name besides obj. Otherwise, I love how it reads!


# WAVE 5

def is_in_english_dict?(input)
Copy link

Choose a reason for hiding this comment

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

Great work on the optional! :D ...Tests? ;)

csv_from_file = CSV.read('assets/dictionary-english.csv').flatten

return csv_from_file.include? input
end