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- cloudy and mina #2

Open
wants to merge 6 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
55 changes: 55 additions & 0 deletions adagram.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Wave 1 [Question 1 - Creating Data Structure]

# POOL_OF_LETTERS = [
# {A: 9}, {N: 6},
# {B: 2}, {O: 8},
# {C: 2}, {P: 2},
# {D: 4}, {Q: 1},
# {E: 12}, {R: 6},
# {F: 2}, {S: 4},
# {G: 3}, {T: 6},
# {H: 2}, {U: 4},
# {I: 9}, {V: 2},
# {J: 1}, {W: 2},
# {K: 1}, {X: 1},
# {L: 4}, {Y: 2},
# {M: 2}, {Z: 1}
# ]

$POOL_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", "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"
]

def draw_letters
my_letters = []
10.times do
random_letter = $POOL_OF_LETTERS.shuffle
my_letters.push(random_letter[-1])
random_letter.pop
end
return my_letters
end

def uses_available_letters?(input, letters_in_hand)
word = input.split(//)

if word.length <= 10
word.each do |each_letter_in_word|
if !letters_in_hand.include?(each_letter_in_word)
return false
end
letters_in_hand.delete(each_letter_in_word)
end
return true
else
return false
end
end

drawn_letters = draw_letters
print "Your 10 letters are #{ drawn_letters }."

puts "Please make the best work with the letters you were dealt."
new_word = gets.chomp.to_s.upcase

uses_available_letters?(new_word, drawn_letters)
Copy link

Choose a reason for hiding this comment

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

In the future, feel free to delete any unused files before submitting!

70 changes: 70 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Wave 1 [Question 1 - Creating Data Structure]

def draw_letters
pool_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", "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"
]
my_letters = []
random_letter = pool_of_letters.shuffle
10.times do
my_letters.push(random_letter[-1])
Copy link

Choose a reason for hiding this comment

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

Just to make it clear what's going on: You have the pool_of_letters array, and then shuffle it, and put the shuffled array into a variable called random_letter. Then, you do this ten times: you push into your array my_letters the value of the item that is last in the random_letter array (the thing at index of -1 is the last item). And then you call random_letter.pop, which removes the last item from the array. This strategy is great! It just feels a little unreadable. I think that the name random_letter feels inaccurate, since it doesn't represent one single random letter, but the array of the shuffled pool of letters. Also, instead of maybe using the syntax random_letter[-1], even though it's very short and concise, it might be more readable to use random_letter.last.

There are also interesting things that .pop does that you'll learn about in the future :)

random_letter.pop
end
return my_letters
end

def uses_available_letters?(input, letters_in_hand)
word = input.upcase.split(//)
Copy link

Choose a reason for hiding this comment

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

I just remembered that I was supposed to get in touch with Cloudy so that her VS Code indented with 2 spaces instead of 4 :)

if word.length <= 10
word.each do |each_letter_in_word|
if !letters_in_hand.include?(each_letter_in_word)
return false
end
letters_in_hand.delete_at(letters_in_hand.index(each_letter_in_word))
end
return true
else
return false
end
end


def score_word(word)
letter_points = {
"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_to_score = word.upcase.split(//)
score = word_to_score.map do |each_letter|
letter_points[each_letter]
end
my_score = score.inject(0) do |total_score, letter_value|
total_score + letter_value
end
if word_to_score.length > 6
my_score += 8
end

return my_score
Copy link

Choose a reason for hiding this comment

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

Nice method implementation, and nice use of Enumerable methods!

end

# Wave 4 - Here we goooo.

def highest_score_from(words)
winning_hash = {
word: "",
score: 0
}
words.each do |word|
if score_word(word) > winning_hash[:score]
winning_hash[:word] = word
winning_hash[:score] = score_word(word)
elsif score_word(word) == winning_hash[:score]
if word.length == 10 && winning_hash[:word].length != 10
winning_hash[:word] = word
elsif word.length < winning_hash[:word].length && winning_hash[:word].length != 10
winning_hash[:word] = word
end
end
Copy link

Choose a reason for hiding this comment

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

This complex logic is really readable-- good work on this wave!

end
return winning_hash
end