forked from AdaGold/adagrams
-
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
Trang & Cassy - Adagram #7
Open
tfrego
wants to merge
19
commits into
Ada-C10:master
Choose a base branch
from
tfrego:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
90a2020
Create letters data structure.
tfrego 1b72b8a
Created outline for drawn letters method. Created array of available …
tfrego e81b911
Removed draw_random_letter method, added times loop to draw random le…
tfrego a4e902f
Created outline of uses_available_letters? method
tfrego 00326d0
create hash of anagram letters as key and qty as value
tfrego 8c707e1
Created method to return false if letter is not in hand
tfrego 33830e8
validated user input against letters in hand
tfrego c74bc44
Made an outline of score_word
tfrego 8359668
add case statement for scoring and bonus points for long word
tfrego 8ed855e
create hash of word scores
tfrego 688204e
Updated method to select all words with max score
tfrego 03dfc16
determine if there's a tie, add pseudocode for applying tie-breaking …
tfrego 17e9058
Revised program so potential winning hashes include key/value pair fo…
tfrego bf9239e
Adjusted method to select winning words with length of 10
tfrego 2bd1aed
add if statement to assign winner to first word of length 10
tfrego 82128e0
add min_by for potential winners
tfrego 0717e47
Refactored code and revised comments
tfrego 7c95153
outlined method for is_in_english_dict?
tfrego a764b00
placed input into array and used .any? to check if in dictionary
tfrego File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
|
||
require 'awesome_print' | ||
require 'csv' | ||
|
||
# Draws letters and returns an array of 10 strings | ||
def draw_letters | ||
# Empty array to hold drawn letters | ||
drawn_letters = [] | ||
# Array of all the letters available to be drawn | ||
available_letters = %w(A A A A A A A A A B B C C D D D D E E E E E E E E E E E E | ||
F F G G G H H I I I I I I I I I J K L L L L M M N N N N N N O O O O O O O O P P | ||
Q R R R R R R S S S S T T T T T T U U U U V V W W X Y Y Z) | ||
# Randomly draw letter | ||
10.times do | ||
# Assign random letter | ||
letter = available_letters.sample | ||
# Push random letter | ||
drawn_letters << letter | ||
# Delete drawn letter from available_letters | ||
available_letters.delete_at(available_letters.index(letter)) | ||
end | ||
return drawn_letters | ||
end | ||
|
||
# Validates input against letters_in_hand | ||
def uses_available_letters?(input, letters_in_hand) | ||
# Capitalizing input | ||
input.upcase! | ||
# Empty array to hold results | ||
letter_results = [] | ||
# Validating quantitiy | ||
input.chars.each do |letter| | ||
if input.count(letter) > letters_in_hand.count(letter) | ||
letter_results << false | ||
else | ||
letter_results << true | ||
end | ||
end | ||
# Returns true if input quantity is valid | ||
return letter_results.all?(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. This is hiding a second loop from you! |
||
end | ||
|
||
# Scores word | ||
def score_word(word) | ||
# Starting score at 0 | ||
score = 0 | ||
# Capitalizing word | ||
word.upcase! | ||
|
||
# Updating score based on letter | ||
word.chars.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 | ||
# Adding bonus points based on word length | ||
score += 8 if word.chars.length.between?(7,10) | ||
return score | ||
end | ||
|
||
# Returning winning word/score | ||
def highest_score_from(words) | ||
# Makes an array of words and associated score | ||
word_scores = words.map { |word| {:word => word, :score => score_word(word)} } | ||
|
||
# Assignign the top score value | ||
top_score = word_scores.max_by{ |k| k[:score] } [:score] | ||
|
||
# Selecting hashes in word_scores that have winning score | ||
potential_winners = word_scores.select { |h| h[:score] == top_score } | ||
|
||
# Checking length of potential winners array | ||
if potential_winners.length > 1 | ||
|
||
# If greater than one, there's a tie | ||
potential_winners.each do |potential_winner| | ||
# Adding word lengths to potential winner hash | ||
potential_winner[:word_length] = potential_winner[:word].length | ||
end | ||
|
||
# Creating an array if word length is 10 | ||
length_of_10 = potential_winners.select { |h| h[:word_length] == 10 } | ||
|
||
# Checking if length_of_10 is not empty | ||
if length_of_10.length > 0 | ||
# Returning first word/score in the length_of_10 array | ||
return { :word => length_of_10[0][:word], :score => length_of_10[0][:score]} | ||
else | ||
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. The logic after here is kind of hairy, could have benefited from a longer comment. |
||
# Returning first word/score and rejecting word_length key/value pair | ||
return potential_winners.min_by { |potential_winner| potential_winner[:word_length]}\ | ||
.reject! { |k| k == :word_length | ||
} | ||
end | ||
else | ||
# Returning the value of potential_winners if there is no tie | ||
return potential_winners[0] | ||
end | ||
end | ||
|
||
# Verify if word exists in English dictionary | ||
def is_in_english_dict?(input) | ||
# makes input an array of one element | ||
input.downcase! | ||
word = [input] | ||
# return array of arrays of dictionary words | ||
dictionary = CSV.read('assets/dictionary-english.csv') | ||
# checks dictionary for word | ||
return dictionary.any?(word) | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As soon as you know one is false, do you need to see the rest of the letters?