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

Chantelle/Semret - C10 Edges - Adagrams #19

Open
wants to merge 14 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
166 changes: 166 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Chantelle and Semret (Pair-Programing)
# Adagram project
# 8/13/18
require 'csv'

# WAVE 1 SYNTAX
def draw_letters

random_draw = []

letter_pool = ('A'..'Z').map do |char|
case char
when 'A', 'I'
letter = char * 9
when 'B', 'C', 'F', 'H', 'M', 'P', 'V', 'W', 'Y'
letter = char * 2
when 'D', 'L', 'S', 'U'
letter = char * 4
when 'E'
letter = char * 12
when 'G'
letter = char * 3
when 'J', 'K', 'Q', 'X', 'Z'
letter = char * 1
when 'N', 'R', 'T'
letter = char * 6
when 'O'
letter = char * 8
end
end

split_letter_pool = letter_pool.map do |chars|
chars.split("")
end

#random draw of 10 from array and flatten the letter pool
random_draw = split_letter_pool.flatten.sample(10)
return random_draw

end
Copy link

Choose a reason for hiding this comment

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

I think overall, your solution to this method is good-- it shows that you all know how to use iteration and specifically map to create an interesting array. My only concern is that I feel like maybe map forced you all to need to make a lot of arrays and a lot of loops-- I don't recommend any changes at this moment, but if you all ever take a minute to look at this problem with fresh eyes, there may be other approaches where it doesn't feel like "fighting" with map


# puts "Random draw: #{draw_letters}"

def uses_available_letters?(word, letter_in_hand)
temp_hand = letter_in_hand.dup
inputted_word = []
inputted_word << word.upcase.split("")
inputted_word.flatten!
Copy link

@tildeee tildeee Aug 25, 2018

Choose a reason for hiding this comment

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

Actually, calling word.upcase.split("") will return an array of letters split by each character...

aka, if word is "example", word.upcase.split("") will be ["E", "X", "A", "M", "P", "L", "E"]! You don't need to shovel this array into the empty array inputted_word so it's [["E", "X", "A", "M", "P", "L", "E"]] and then flatten it... you can just assign the result of split into inputted_word!

Therefore, you can turn the above code:

inputted_word = []
inputted_word << word.upcase.split("")
inputted_word.flatten!

to

inputted_word = word.upcase.split("")

Let me know if there are any questions on this


check_letter = true

inputted_word.each do |letter|
index = 0
Copy link

Choose a reason for hiding this comment

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

Scope question: What is the smallest scope (the most specific/local block of code) that index needs to be in?


if !temp_hand.include?(letter)
check_letter = false
end

if temp_hand.include?(letter)
index = temp_hand.index(letter)
temp_hand.delete_at(index)
Copy link

Choose a reason for hiding this comment

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

I think that index doesn't get used until this if block, so you may not need to declare index = 0 before this if block. Try deleting the index = 0 above and run the tests to see if it still works!

end
end

return check_letter

end

# WAVE 3 SYNTAX
# Scoring the word based on above parameters of scoring chart
def score_word (word)

this_letter = []
this_letter << word.upcase.split("")
this_letter.flatten!

score = 0
this_letter.each do |value|

case value
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


if word.length > 6 && word.length < 11
score += 8
end

return score
end

# WAVE 4 SYNTAX

def highest_score_from(words)

# creating a hash to store highest scoring word with its score value
highest_scoring_hash = {
:score => 0,
:word => words.first
Copy link

Choose a reason for hiding this comment

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

You declare that the initial value of highest_scoring_hash has a value 0 for score and a value of the first word for word. You may want to make the initial value consistent and make the initial value of score equal to score_word(words.first)

}

# loop thur the array and test for score and word length to identify highest scoring word
words.each do |word|
current_score = score_word(word)
current_word = word

# word that has the highest score wins
if current_score > highest_scoring_hash[:score]

highest_scoring_hash[:score] = current_score
highest_scoring_hash[:word] = current_word

# in case of a score tie
elsif current_score == highest_scoring_hash[:score]

#f ewer letter wins
if highest_scoring_hash[:word].length > current_word.length &&
(highest_scoring_hash[:word].length != 10 && current_word.length !=10)

highest_scoring_hash[:score] = current_score
highest_scoring_hash[:word] = current_word

# word length of 10 wins
elsif current_word.length == 10 && highest_scoring_hash[:word].length < 10
highest_scoring_hash[:score] = current_score
highest_scoring_hash[:word] = current_word
# when lengths are equal picking first (the one already stored in hash)
elsif current_word.length == highest_scoring_hash[:word].length ||
highest_scoring_hash[:word].length == 10
# do nothing
Copy link

Choose a reason for hiding this comment

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

If you need to do nothing, then you probably don't need to have this elsif exist :)

end
end
end
return highest_scoring_hash
end

# WAVE 5 SYNTAX
# is it a valid dictionary word?
def is_in_english_dict?(input)


results = false
dictionary = CSV.read("assets/dictionary-english.csv",headers: true, header_converters: :symbol)
Copy link

Choose a reason for hiding this comment

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

good job including headers on this!

dictionary.each do |word|
word.each do |k, v|
if v.downcase == input.downcase
results = true
end
end
end
return results
end
10 changes: 6 additions & 4 deletions specs/adagrams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@

describe 'uses_available_letters? method' do

it 'returns true if the submitted letters are valid against the drawn letters' do
it 'returns true if the submitted letters are valid against the drawn letters regardless of case sensitivety' do
drawn_letters = ['D', 'O', 'G', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
test_word = 'DOG'
test_words = ['dog', 'DoG', 'DOG']

is_valid = uses_available_letters? test_word, drawn_letters
test_words.each do |test_word|
is_valid = uses_available_letters? test_word, drawn_letters

expect(is_valid).must_equal true
expect(is_valid).must_equal true
end
end

it 'returns false word contains letters not in the drawn letters' do
Expand Down