Skip to content

Instantly share code, notes, and snippets.

@r00k
Created July 22, 2015 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r00k/42f28df2bd6da926fd68 to your computer and use it in GitHub Desktop.
Save r00k/42f28df2bd6da926fd68 to your computer and use it in GitHub Desktop.
commit 81a9375d4ecbd37ca9da7efaebbffa8cf150d6f6
Author: Ben Orenstein <ben.orenstein@gmail.com>
Date: Wed Jul 22 12:02:43 2015 -0400
code review ideas
diff --git a/app.rb b/app.rb
index 93bac6c..c9443f6 100644
--- a/app.rb
+++ b/app.rb
@@ -1,65 +1,73 @@
if __FILE__ == $PROGRAM_NAME
- puts "when in app.rb, it returns #{__FILE__} and #{$PROGRAM_NAME}"
- # make a hash of words
- @word_hash = {}
- dictionary = File.open('1000_families.txt', 'r')
- dictionary.each do |word|
- word = word.strip.downcase
- @word_hash[word] = true
- end
- dictionary.close
-
- # create a list of long words to choose
- # a game word from!
- long_word_list = []
- @word_hash.select do |k, v|
- if k.length > 7
- long_word_list.push(k)
- end
- end
- game_word = long_word_list.sample
- game_letters = game_word.split("")
- puts game_word
+ puts "when in app.rb, it returns #{__FILE__} and #{$PROGRAM_NAME}"
+ # make a hash of words
+ @word_hash = {}
+ dictionary = File.open('1000_families.txt', 'r')
+ dictionary.each do |word|
+ word = word.strip.downcase
+ @word_hash[word] = true
+ end
+ dictionary.close
+
+ # create a list of long words to choose
+ # a game word from!
+ long_word_list = []
+ @word_hash.select do |k, v|
+ if k.length > 7
+ long_word_list.push(k)
+ end
+ end
+ game_word = long_word_list.sample
+ game_letters = game_word.split("")
+ puts game_word
end
-# Rule 1: Every letter in player_word
-# appears in game_word
-def rule1(player_word, game_word)
- is_valid = true
- player_word.each do |letter|
- if game_word.include?(letter) == false
- is_valid = false
- break
- else
- end
- end
- return is_valid
+# Rule 1: Every letter in player_word appears in game_word
+def every_letter_included?(player_word, game_word)
+ player_word.each do |letter|
+ unless game_word.include?(letter)
+ is_valid = false
+ break
+ end
+ end
+end
+
+# Write a method that returns true when every element in
+# collection_1 is in collection_2.
+def every_element_included?(coll1, coll2)
+ coll1.to_set.subset?(coll2.to_set)
end
# Rule 2: Each unique letter in player_word may only appear
# in player_word less than or equal to the number of times
# the same letter appears in game_word
+#
+# Rule 2: The count of each letter in player_word must be
+# less than or equal to the count of that letter in game_word
def rule2(player_word, game_word)
- is_valid = true
- player_word.uniq.each do |letter|
- if player_word.count(letter) > game_word.count(letter)
- is_valid = false
- break
- else
- end
- end
- return is_valid
+ # is_valid = true
+ # player_word.uniq.each do |letter|
+ # if player_word.count(letter) > game_word.count(letter)
+ # is_valid = false
+ # break
+ # else
+ # end
+ # end
+ # return is_valid
+ #
+ # build a hash where the keys are letter and the values are the
+ # number of times the letter appears in the word
+ #
+ # letter_counts("hello") => { "h" => 1, "e" => 1, "l" => 2, "o" => 1 }
+ #
+ # { "h" => 1, "e" => 1, "l" => 2, "o" => 1 }
+ # { "h" => 1, "e" => 1, "l" => 2 }
+ #
+ # player_word_letter_count.all? { |(letter, count)| game_word_letter_count[letter] >= count }
end
-# Rule 3: player_word must be greater than or equal to
-# two letters long.
-def rule3(player_word)
- is_valid = true
- if player_word.length < 2
- is_valid = false
- else
- end
- return is_valid
+def sufficiently_long?(word)
+ word.length >= 2
end
# if the player_word returned true for all three rules,
@@ -67,48 +75,49 @@ end
# and return true
@valid_guesses = []
def is_valid_guess?(player_word)
- @valid_guesses.push(player_word)
- return true
+ @valid_guesses.push(player_word)
+ return true
end
# check if a word has already been guessed
def already_guessed?(player_word)
- is_valid = true
- if @valid_guesses.include?(player_word)
- is_valid = false
- end
- return is_valid
+ @valid_guesses.include?(player_word)
end
# creates a list of all possible anagrams in game_word
def all_possible_anagrams_of_game_word(game_word)
- @official_guess_list = []
- @word_hash.each_key do |k|
- word_split_and_sorted = k.split("").sort
- if rule1(word_split_and_sorted, game_word) && rule2(word_split_and_sorted, game_word) \
- && rule3(word_split_and_sorted)
- @official_guess_list.push(k)
- end
- end
+ @official_guess_list = []
+ @word_hash.each_key do |k|
+ word_split_and_sorted = k.split("").sort
+ if valid_anagram?(word_split_and_sorted, game_word)
+ @official_guess_list.push(k)
+ end
+ end
+end
+
+def valid_anagram?(word_split_and_sorted, game_word)
+ every_letter_included?(word_split_and_sorted, game_word) &&
+ rule2(word_split_and_sorted, game_word) &&
+ player_word_is_sufficiently_long?(word_split_and_sorted)
end
# play the game
all_possible_anagrams_of_game_word(game_word)
puts @official_guess_list.length
while @official_guess_list.length != @valid_guesses.length
- # get player word, recombobulate it into a sorted string
- player_word_string = $stdin.gets.chomp.downcase
- player_word = player_word_string.split("").sort
-
- if already_guessed?(player_word_string) == false
- puts "You've already guessed that word!"
- elsif @official_guess_list.include?(player_word_string)
- is_valid_guess?(player_word_string)
- puts "woo that's an actual word on the list!"
- else
- puts "That is not a valid word. Keep trying ok!"
- end
- puts @valid_guesses.length
+ # get player word, recombobulate it into a sorted string
+ player_word_string = $stdin.gets.chomp.downcase
+ player_word = player_word_string.split("").sort
+
+ if already_guessed?(player_word_string) == false
+ puts "You've already guessed that word!"
+ elsif @official_guess_list.include?(player_word_string)
+ is_valid_guess?(player_word_string)
+ puts "woo that's an actual word on the list!"
+ else
+ puts "That is not a valid word. Keep trying ok!"
+ end
+ puts @valid_guesses.length
end
diff --git a/foo.rb b/foo.rb
new file mode 100644
index 0000000..025faf1
--- /dev/null
+++ b/foo.rb
@@ -0,0 +1,9 @@
+def launch_spaceship
+ ensure_sufficient_fuel
+ check_with_mission_control
+end
+
+
+def ensure_sufficient_fuel
+ if fuel_tank.level > 200000
+end
diff --git a/notes.txt b/notes.txt
new file mode 100644
index 0000000..8841943
--- /dev/null
+++ b/notes.txt
@@ -0,0 +1,2 @@
+`return` is a bit of a smell, prefer to just return the last thing you evaluated implicitly
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment