Last active
August 29, 2015 13:57
-
-
Save michaelavila/9777401 to your computer and use it in GitHub Desktop.
Simple Unique Exercism Iterations Algorithm
This file contains hidden or 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
require 'similar_text' | |
def unique_iterations(iterations) | |
unique_enough = [iterations.first] | |
unique_enough << iterations[1..-1].select do |iteration_filename| | |
iteration = File.read iteration_filename | |
similar_iterations = unique_enough.select do |unique_filename| | |
similarity = iteration.similar(File.read(unique_filename)) | |
similarity < 50 | |
end | |
similar_iterations.count != 0 | |
end | |
end | |
iterations = (1..6).map do |num| | |
"iteration#{num}.rb" | |
end | |
puts unique_iterations iterations | |
# OUTPUT | |
# iteration1.rb | |
# iteration6.rb |
This file contains hidden or 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
source "https://rubygems.org" | |
gem 'similar_text' |
This file contains hidden or 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(message) | |
if message.match(/^\s*\Z/) | |
'Fine. Be that way!' | |
elsif message.match(/[A-Z]/) and message.upcase == message | |
'Woah, chill out!' | |
elsif message.match(/\?\Z/) | |
'Sure.' | |
else | |
'Whatever.' | |
end | |
end | |
end |
This file contains hidden or 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(message) | |
# address without saying anything | |
if message.match(/^\s*\Z/) | |
'Fine. Be that way!' | |
# yell at | |
elsif message.match(/[A-Z]/) and message.upcase == message | |
'Woah, chill out!' | |
# ask a question | |
elsif message.match(/\?\Z/) | |
'Sure.' | |
# everything else | |
else | |
'Whatever.' | |
end | |
end | |
end |
This file contains hidden or 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(message) | |
# address without saying anything | |
if message =~ /^\s*\Z/ | |
'Fine. Be that way!' | |
# yell at | |
elsif message =~ /[A-Z]/ and message.upcase == message | |
'Woah, chill out!' | |
# ask a question | |
elsif message =~ /\?\Z/ | |
'Sure.' | |
# everything else | |
else | |
'Whatever.' | |
end | |
end | |
end |
This file contains hidden or 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(message) | |
# address without saying anything | |
if message.strip.empty? | |
'Fine. Be that way!' | |
# yell at | |
elsif message =~ /[A-Z]/ and message.upcase == message | |
'Woah, chill out!' | |
# ask a question | |
elsif message.end_with?('?') | |
'Sure.' | |
# everything else | |
else | |
'Whatever.' | |
end | |
end | |
end |
This file contains hidden or 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(message) | |
# address without saying anything | |
if message.strip.empty? | |
'Fine. Be that way!' | |
# yell at | |
elsif message.downcase != message and message.upcase == message | |
'Woah, chill out!' | |
# ask a question | |
elsif message.end_with?('?') | |
'Sure.' | |
# everything else | |
else | |
'Whatever.' | |
end | |
end | |
end |
This file contains hidden or 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 | |
attr_reader :cerebrum | |
def initialize | |
@cerebrum = Cerebrum.new | |
end | |
def hey(message) | |
cerebrum.process(message) | |
end | |
end | |
class Cerebrum | |
def process(input) | |
case detect_sentiment(input) | |
when :silence | |
'Fine. Be that way!' | |
when :yelling | |
'Woah, chill out!' | |
when :questioning | |
'Sure.' | |
when :whocares | |
'Whatever.' | |
end | |
end | |
def detect_sentiment(input) | |
if input.strip.empty? | |
:silence | |
elsif input.downcase != input and input.upcase == input | |
:yelling | |
elsif input.end_with?('?') | |
:questioning | |
else | |
:whocares | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment