Last active
December 28, 2015 06:48
-
-
Save unixmonkey/7459428 to your computer and use it in GitHub Desktop.
Indy.rb code review exercises 2013-11-13
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
class Anagram | |
def initialize(target) | |
@target = target.downcase | |
end | |
def match(word_list) | |
word_list.select do |word| | |
is_anagram?(word.downcase) | |
end | |
end | |
private | |
def is_anagram?(word) | |
has_same_number_of_letters?(word) && | |
different_from?(word) && | |
maps_cleanly?(word) | |
end | |
def maps_cleanly?(word) | |
@target.each_char do |char| | |
word = word.delete(char) | |
end | |
word == '' | |
end | |
def different_from?(word) | |
word.downcase != @target.downcase | |
end | |
def has_same_number_of_letters?(word) | |
word.chars.count == @target.chars.count | |
end | |
end |
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
class Bob | |
def hey(utterance) | |
@utterance = utterance | |
response | |
end | |
private | |
def response | |
case | |
when blank? then 'Fine. Be that way.' | |
when shouting? then 'Woah, chill out!' | |
when question? then 'Sure.' | |
else 'Whatever.' | |
end | |
end | |
def question? | |
(@utterance.chars.last == '?') && !shouting? | |
end | |
def shouting? | |
@utterance.upcase == @utterance | |
end | |
def blank? | |
@utterance.nil? || @utterance == '' | |
end | |
end |
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
class Phrase | |
def initialize(phrase) | |
@phrase = phrase | |
end | |
def word_count | |
count = Hash.new(0) | |
words.each do |word| | |
count[word] += 1 | |
end | |
count | |
end | |
private | |
def words | |
@phrase.downcase.gsub(/[^a-z0-9 ]/, ' ').split(' ') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment