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

Ayesha and Ren's Adagrams project submission #7

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions .floo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"url": "https://floobits.com/ayesha/adagrams"
}
6 changes: 6 additions & 0 deletions .flooignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern
node_modules
tmp
vendor
.idea/workspace.xml
.idea/misc.xml
128 changes: 128 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# frozen_string_literal: true

# Wave 1
def draw_letters
letters_pool_to_draw_from = {
'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
}

ten_strings_array = []

letters_pool_keys = letters_pool_to_draw_from.keys
until ten_strings_array.length == 10
rand_letter = letters_pool_keys[rand(letters_pool_keys.size)]
if (letters_pool_to_draw_from[rand_letter]).zero?
next
else
Comment on lines +37 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

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

This runs, but it gives an equal probability of drawing a Z and an E.

A better strategy would be to use your hash to build an array of all the letters with 9 As, 2 Bs etc , shuffle the array and then draw the right number of letters.

letters_pool_to_draw_from[rand_letter] -= 1
end

ten_strings_array.push(rand_letter)
end
ten_strings_array
end

# WAVE 2
def uses_available_letters?(input, letters_in_hand)
Comment on lines +50 to +51
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

input = input.split('')
letters_in_hand_clone = letters_in_hand.clone
input.each do |element|
if letters_in_hand_clone.include?(element)
letters_in_hand_clone.delete(element)
else
return false
end
end
return true
end

# Wave 3
def score_word(word)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 , This works well.

score = 0
word = word.upcase
word.chars.each do |character|

case character
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
def highest_score_from(words)
Comment on lines +92 to +93
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

arr_of_scores = words.map do |word|
{ word: word, score: score_word(word) }
end

max_score = -1
max_pair = nil

arr_of_scores.each do |hash|
if hash[:score] > max_score
max_score = hash[:score]
max_pair = hash
elsif max_score == hash[:score]
if max_pair[:word].length != 10
if hash[:word].length < max_pair[:word].length || hash[:word].length == 10
max_pair = hash
max_score = hash[:score]
end
end
end
end
max_pair
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
max_pair
return max_pair

end

# Wave 5
def is_in_english_dict?(input)
Comment on lines +117 to +118
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 , Nice work with the optional.

dictionary = CSV.read('assets/dictionary-english.CSV', headers: true)

dictionary.each do |word|
if input == word[0]
return true
break
end
end
return false
end