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

Sockets - Nidhi and Riyo #9

Open
wants to merge 9 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
112 changes: 112 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require "csv"

def draw_letters
letter_pool = []
number_of_letters = [9, 2, 2, 3, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1]
letters = ("A".."Z").to_a
i = 0
while i < 26
number_of_letters[i].times do
letter_pool.push(letters[i])

Choose a reason for hiding this comment

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

I like how you did this. It shows a good understanding of the language!

end
i = i + 1
end

return letter_pool.sample(10)

Choose a reason for hiding this comment

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

Sample is a good choice here!

end

puts "Using the following letters, create an Adagram"
hand = draw_letters
puts hand

Choose a reason for hiding this comment

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

I'm not sure how exactly you were testing your code, but these lines in between the methods kept me from being able to use the rake command to test your code.


puts "What is your Adagram?:"
adagram_input = gets.chomp.to_s.upcase

def uses_available_letters?(input, letters_in_hand)
letters_array = input.chars

Choose a reason for hiding this comment

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

There doesn't seem to me a good reason to make this re-assignment. In your while you could have just as easily done input.chars.length.

i = 0
result = true
while i < letters_array.length
if letters_in_hand.include?(input[i]) == true
letters_in_hand.delete(input[i])
else
result = false

Choose a reason for hiding this comment

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

I'll leave this as a "vague thing" to think about: I think that while you iterate through input.chars, you can actually determine that uses_available_letters? can return false before the loop finishes.

Remember, when you use the return keyword, it will kick the program execution out of the method early, and return whatever value you give it.

end
i += 1
end
return result
end

puts uses_available_letters?(adagram_input, hand)

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}

def score_word(word)
score = 0
if score == ""
points = 0
else
letters = word.upcase.chars
score = 0
letters.each do |letter|
score += SCORE_HASH[letter]
end

if letters.length > 6
score += 8
end
end

return score
end

puts score_word(adagram_input)

def highest_score_from(words)
word_scores = words.map do |word|
score_word(word)
end

words_scores_hash = {}
i = 0
while i < words.length
words_scores_hash[words[i]] = word_scores[i]
i += 1
end
highest_score = word_scores.max
highest_score_hash = words_scores_hash.select { |k, v| v == highest_score }

if highest_score_hash.length > 1
tie_winner = {}
keys = highest_score_hash.keys
lengths = keys.map { |key| key.length }

if lengths.include?(10) == true
tie_winner = {keys[lengths.find_index(10)] => highest_score}
else
shortest_word = keys.min_by { |key| key.length }
tie_winner = {shortest_word => highest_score}
end

highest_score_hash = tie_winner
end
# reformats hash to match the results in test
winning_word = highest_score_hash.keys
win_formatted = {word: winning_word[0], score: highest_score}

return win_formatted
end

def is_in_english_dict?(input)
input_in_dictionary = false
CSV.foreach("../assets/dictionary-english.csv") do |word_array|
word_array.each do |word|
if word == input.downcase
input_in_dictionary = true
end

Choose a reason for hiding this comment

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

As above, you could leverage the return keyword here to just bail from the loop as soon as you have the answer.

end
end
return input_in_dictionary
end

puts is_in_english_dict?(adagram_input)