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

Fire - Noor and Anna #17

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
.env
.idea
.floo
.flooignore
Comment on lines +4 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

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

Smart!

129 changes: 129 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@

LETTERS = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice way to set up the distribution of 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
}

# WAVE 1
def draw_letters()
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

all_letters = []

# Creates an array of letters that matches the frequency listed in LETTERS
LETTERS.each do |letter, frequency|
frequency.times do
all_letters << letter.to_s
end
end

strings = all_letters.shuffle.first(10)
return strings
end


# WAVE 2

def uses_available_letters?(input, letters_in_hand)
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.chars

if (input - letters_in_hand).size > 0
return false
else
input.each do |letter|
if input.count(letter) > letters_in_hand.count(letter)
return false
end
end
return true
end
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.

👍

points = 0

if word.length > 6
points += 8
end

word.each_char do |letter|
letter.upcase!
if ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'].include? (letter)
points += 1
elsif ['D', 'G'].include? (letter)
points += 2
elsif ['B', 'C', 'M', 'P'].include? (letter)
points += 3
elsif ['F', 'H', 'V', 'W', 'Y'].include?(letter)
points += 4
elsif ['K'].include? (letter)
points += 5
elsif ['J', 'X'].include? (letter)
points += 8
elsif ['Q', 'Z'].include? (letter)
points += 10
end
end

return points
end

# Wave 4

def highest_score_from(words)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am uncertain that this method will work in all cases. See my comments below.

hash = {}
points = words.map { |word| score_word(word) }

tie = []
points.each do |point|
if point == points.max
tie << points.index(point)
end
end

word_lengths = words.map { |word| word.chars.count }

if tie.length > 1
word_lengths.each do |length|
if length == 10
hash[:word] = words[word_lengths.index(length)]
hash[:score] = points[word_lengths.index(length)]
return hash
Comment on lines +113 to +115
Copy link
Collaborator

Choose a reason for hiding this comment

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

You don't know if the word with length 10 is tied for the highest score here.

elsif length < 10
min_length = word_lengths.min
hash[:word] = words[word_lengths.index(min_length)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might not work in all cases. What if there is a non-winning word with an equal length

Copy link
Author

Choose a reason for hiding this comment

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

Ah I see! Because we are checking through all the words that are passed as an argument, when we should be looking at the subset that has the highest points - is that right?

hash[:score] = points[word_lengths.index(min_length)]
end
end
else
hash[:word] = words[tie[0]]
hash[:score] = points[tie[0]]
end

return hash
end