-
Notifications
You must be signed in to change notification settings - Fork 27
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
Adagrams Submission - Danielle & Alice #2
base: master
Are you sure you want to change the base?
Changes from all commits
34a7bb8
e5717f5
11f3d90
e104c7d
d4f4e76
e424681
78f7d1c
e26d1ba
38dcca9
8605898
9a00698
a584cd9
74eeeb7
c0f5792
7e53377
c28de89
b144848
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#Adagrams | ||
#Danielle & Alice | ||
|
||
require 'csv' | ||
|
||
#Wave 1 | ||
#Picks 10 letters from the letter pool | ||
def draw_letters | ||
letters = { | ||
"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 | ||
} | ||
|
||
#Creates the array of all the letters available to pick | ||
letter_probability = [] | ||
letters.each do |key,value| | ||
until value == 0 | ||
letter_probability << key | ||
value -= 1 | ||
end | ||
end | ||
|
||
#Select letters and replace with 0 once they are picked | ||
your_hand = [] | ||
count = 0 | ||
while count < 10 do | ||
index = rand(0...letter_probability.length) | ||
if letter_probability[index] != 0 | ||
your_hand << letter_probability [index] | ||
count += 1 | ||
letter_probability[index] = 0 | ||
end | ||
end | ||
return your_hand | ||
end | ||
|
||
#Wave 2 | ||
#Checks that the letters for the word you picked are in your hand | ||
def uses_available_letters?(input, letters_in_hand) | ||
input = input.upcase.split("") | ||
value = [] | ||
if input.length > letters_in_hand.length | ||
return false | ||
else | ||
input.each do |letter| | ||
input.count(letter) > letters_in_hand.count(letter) ? value << false : value << true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are doing more work than you need to! As soon as one of these checks comes up false, you can return false, right? The only case that requires you to complete the loop is if every letter matches! |
||
end | ||
end | ||
value.all? true ? true : false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
|
||
#Wave 3 | ||
#Returns score of the word | ||
def score_word (word) | ||
word = word.upcase.split("") | ||
score = 0 | ||
word.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 | ||
|
||
score += 8 if word.length >= 7 | ||
|
||
return score | ||
end | ||
|
||
#Wave 4 | ||
#Creates the hash for the winning word | ||
def highest_score (word, score) | ||
highest_score = {} | ||
highest_score[:word] = word | ||
highest_score[:score] = score | ||
return highest_score | ||
end | ||
|
||
#Returns the hash of the winning word (:word & :score) and takes into account word length and ties for an array of words | ||
def highest_score_from words | ||
scores = {} | ||
words.each do |word| | ||
scores[word] = score_word(word) | ||
end | ||
|
||
#Determines maximum score for all words | ||
max_word = scores.max_by{|key,value| value}[0] | ||
max_score = scores.max_by{|key,value| value}[1] | ||
|
||
|
||
if scores.values.count(max_score) == 1 | ||
return highest_score(max_word, max_score) | ||
else | ||
max_scores = scores.select {|key,value| value==max_score} | ||
|
||
min_length = max_scores.min_by{|key,value| key.length}[0].length | ||
|
||
best_words = max_scores.select {|key,value| key.length == min_length || key.length == 10} | ||
|
||
if best_words.length == 1 | ||
return highest_score(best_words.keys[0],best_words.values[0]) | ||
elsif best_words.length > 1 | ||
best_words.each do |word, score| | ||
if word.length == 10 | ||
return highest_score(word, score) | ||
end | ||
end | ||
return highest_score(best_words.keys[0],best_words.values[0]) | ||
end | ||
end | ||
end | ||
|
||
#Wave 5 | ||
#Checks that the word is in the english dictionary | ||
def is_in_english_dict? (input) | ||
dictionary = CSV.read("assets/dictionary-english.csv") | ||
new_dictionary = [] | ||
dictionary.each do |element| | ||
new_dictionary << element[0] | ||
end | ||
new_dictionary.include?(input.downcase) ? true : false | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require_relative 'lib/adagrams' | ||
|
||
|
||
def display_welcome_message | ||
puts "Welcome to Adagrams!" | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this works, but you end up with an array that becomes more and more likely to "bounce" as you pick letters from the pile. Instead of nulling the value at the index, why not remove just that element from the array? That way, you don't waste loops through on no-ops (no operation).