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

Ports - Elle & Sopheary #20

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ To do so, add a method called `uses_available_letters?` in `adagrams.rb`. This m
- Returns `true` if every letter in the `input` word is available (in the right quantities) in the `letters_in_hand`
- Returns `false` if not; if there is a letter in `input` that is not present in the `letters_in_hand` or has too much of compared to the `letters_in_hand`

<!---### Wave 3
### Wave 3

We want a method that returns the score of a given word as defined by the Adagrams game.

Expand Down Expand Up @@ -192,7 +192,7 @@ Add a method called `is_in_english_dict?` in `adagrams.rb`. This method should h
There are no unit tests provided for this wave, but there is driver code found in `wave-5-game.rb`. Feel free to alter `specs/adagrams_sepc.rb` and add some!

Nota Bene: The original data for all of the alpha words of the English dictionary was found freely available at [`dwyl/english-word`'s repo](https://github.com/dwyl/english-words), and was modified to only include words under 10 characters.
--->


## What Instructors Are Looking For
Check out the [feedback template](feedback.md) which lists the items instructors will be looking for as they evaluate your project.
151 changes: 151 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
letter_quantities = {
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_pool = []
letter_quantities.each do |letter, quantity|
quantity.times do
$letter_pool << letter.to_s
end
end
Copy link

Choose a reason for hiding this comment

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

You all populated a variable representing the letter pool as a global variable! In some ways, this makes sense, but in general, we'll avoid using global variables. Our hope was top put the above few lines within the draw_letters method, since that's the only time you'll use this variable.


$letter_scores = {
1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2 => ["D", "G"],
3 => ["B", "C", "M", "P"],
4 => ["F", "H", "V", "W", "Y"],
5 => ["K"],
8 => ["J", "X"],
10 => ["Q", "Z"],
}
Copy link

Choose a reason for hiding this comment

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

Similar to my previous comment: we'll try to avoid using global variables in the future, and the only method that uses this variable $letter_scores is in the score_word method, so we could move this variable there.


# Wave 1
def draw_letters
letters_in_hand = $letter_pool.sample(10)
return letters_in_hand
end

# Wave 2
def uses_available_letters?(input, letters_in_hand)
input = input.upcase.split("")
# Make a copy of the input array and keep the original
# array unchanged. The original input needs to remain in tact so
# that Ruby can continue to iterate through it the
# uses_available_letters methods.
Copy link

Choose a reason for hiding this comment

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

This is a great use of a comment: to add detail about something that feels a little weird and needs explanation!

input_copy = input.dup

# Check if letters in the input are all in the letters_in_hand array
input.each_with_index do |letter, input_index|

if letters_in_hand.include?(letter)
hand_index = 0
letters_in_hand.each_with_index do |letter_in_hand, index|
if letter == letter_in_hand
hand_index = index
break
end
hand_index += 1
end
Copy link

Choose a reason for hiding this comment

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

Nice use of each_with_index! It seems to me that the purpose of this loop is to find the index of the letter you're looking for. There is a method that works on strings for this: letters_in_hand.index(letter)... which will give you the first index of a matching element (letter) in the array (letters_in_hand). It's assuming to find the first index of a matching element, but you can safely make that assumption since you're iterating through input in order :) (Actually, it kind of feels like you implemented the .index method, so great job on that :D)


letters_in_hand.delete_at(hand_index)
Copy link

Choose a reason for hiding this comment

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

In conjunction with what I mentioned above, you could replace this line with: letters_in_hand.delete_at(letters_in_hand.index(letter))


i = 0
input_copy.each_with_index do |input_copy_letter, input_copy_index|
if input_copy_letter == letter
i = input_copy_index
break
end
end
input_copy.delete_at(i)
Copy link

Choose a reason for hiding this comment

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

This loop ends up solving a similar problem to above, and therefore, has a similar solution. With that, if you wanted to use the .index method, you could replace this line with: input_copy.delete_at(input_copy.index(letter))

end
end

if input_copy.length == 0
return true
else
return false
end
end

# Calculate a score for the word the user submits.
def score_word(word)
total_score = 0
if word.length > 6
total_score += 8
end
this_word = word.upcase.split("")
this_word.each do |this_letter|
$letter_scores.each do |score, letter_array|
if letter_array.include?(this_letter)
total_score += score
end
end
end
return total_score
end

# Find the highest score from all submitted words.
def highest_score_from(words)
words_scores_array = []
words.each do |this_word|
this_score = score_word(this_word)
words_scores_array << {
word: this_word,
score: this_score,
}
end

scores_array = words_scores_array.map do |this_hash|
this_hash[:score]
end
max_score = scores_array.max
winning_words = []
winning_word = {}
words_scores_array.each do |this_word_score|
if max_score == this_word_score[:score]
winning_words << this_word_score[:word]
end
end
# Detect and handle ties
if winning_words.length > 1
winning_words.each do |this_word|
if this_word.length > 9
winning_word[:word] = this_word
break
else
winning_word[:word] = winning_words.min_by do |nested_word|
nested_word.length
end
end
end
else
winning_word[:word] = winning_words[0]
end

winning_word[:score] = max_score.to_i
return winning_word
end
Copy link

Choose a reason for hiding this comment

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

This method ended up being pretty long -- if methods get this long, feel free to break sections up into smaller helper methods